Looks like a scope issue with the results variable. Try the append like this.
$script:results += [PSCustomObject]@{
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
my script returns the "Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'." error. how do i correct this? I am trying to export members of security groups as well as nested groups and their members.
$csvPath = "C:\temp\SecGroups.csv"
$securityGroups = @("Group1", "Group2", "Group3") # Replace with your security group names
$results = @()
function Get-GroupMembers {
param (
[string]$groupName
)
# Get the group members
$members = Get-ADGroupMember -Identity $groupName -Recursive
foreach ($member in $members) {
# Check if the member is a group
if ($member.objectClass -eq "group") {
# Recursively get the members of the nested group
Get-GroupMembers -groupName $member.SamAccountName
} else {
# Add the member to the results array
$results += [PSCustomObject]@{
GroupName = $groupName
SamAccountName = $member.SamAccountName
Name = $member.Name
ObjectClass = $member.objectClass
}
}
}
}
foreach ($group in $securityGroups) {
Get-GroupMembers -groupName $group
}
$results | Export-Csv -Path $csvPath -NoTypeInformation
Write-Output "Export completed. The CSV file is located at $csvPath"
Looks like a scope issue with the results variable. Try the append like this.
$script:results += [PSCustomObject]@{