0
votes

I try to get result from this part of my powershell scrip into Clixml.
I'm just beginner in powershell so i have kind of problem using arrays.

I'm unable to get result of this script into file.

$groupname = "Domain Admins"
$users = Get-ADGroupMember -Identity $groupname | ? {$_.objectclass -eq "user"}
foreach ($activeusers in $users) { Get-ADUser -Identity $activeusers | ? {$_.enabled -eq $true} | 
select-object SamAccountName  | Sort-Object -Descending | select-object SamAccountName }

Here is code used for export to Clixml

Export-Clixml -Path 'C:\TEMP\CurrentDomainAdmins3.xml'
1
replace you 2nd [duplicate and unneeded] Select-Object section of your pipeline with the Export-CliXml call. - Lee_Dailey

1 Answers

0
votes

You aren't assigning the result of foreach () {} to any variable, e.g. $results = foreach () {} and you cannot pipe the output of that style of loop to another cmdlet.

I don't think you need a loop for it at all; you could rewrite it like this:

$groupName = 'Domain Admins'
Get-AdGroupMember -Identity $groupName | 
    where-Object -Property ObjectClass -eq -Value User | 
    Get-AdUser |
    Where-Object -Property Enabled |
    Export-Clixml -Path 'C:\TEMP\CurrentDomainAdmins3.xml'

or for a more interactive style:

AdGroupMember 'Domain Admins' | ? ObjectClass -eq User | AdUser | ? Enabled