Custom PSObject output issues

Good Afternoon,

I have some code that selects a number of users to add them into an AD security group, if I use -whatif on 'Add-QADGroupMember' the PSObject I create is correct and just writes the SAMAccountName to a CSV. If I remove the -whatif parameter and actually carry out the action to add the user to the AD security group then it writes the entire user object (all attributes) to the CSV but I am not sure why?

Please could you help, here is the code:

Add-PSSnapin Quest.ActiveRoles.ADManagement | Out-Null
Connect-QADService "Domain" | Out-Null

$GroupName = 'MyADGroupName'
$CurrentGroupMembers = Get-QADGroupMember -Identity $GroupName | ? {$_.ObjectClass -eq 'user'} | Select-Object -ExpandProperty SamAccountName

# Get Random List of Users (100)

$Items = Get-QADUser -SearchRoot "Domain/OU" -SizeLimit 0 | ? {$CurrentGroupMembers -notcontains $_.SamAccountName} | Sort-Object -Property Name | Sort-Object{Get-Random} | Select-Object -First 100

$CustomOutput = ForEach ($Item in $Items) {
$User = Get-QADUser -Identity $Item.SAMAccountName
Add-QADGroupMember -Identity $GroupName -Member $User -proxy
New-Object PSObject -Property @{
SamAccountName = $Item.samAccountName

$CustomOutput | Export-Csv c:\users\myuser\desktop\$(Get-Date -Format 'ddMMyyyy').csv -NoClobber -NoTypeInformation

Thanks very much for any help you can provide

No Data
  • I got this answered on - reason for this is that $CustomOutput contains anything that a command outputs as part of it running (this includes Add-QADGroupMember).  The way to resolve this is to pipe the 'Add-QADGroupMember' to Out-Null to ensure that $CustomObject does not grab its output.

No Data