VMware Cloud Community
zenivox
Hot Shot
Hot Shot
Jump to solution

PowerCLI/PowerShell script to retrieve local admins on Windows servers

Hello, I hope I'm not too cheeky to ask the question here... I'm trying to retrieve the local admins group members of all Windows VM servers and export them to a csv. I wrote this code but it only exports to csv one server. So the loop is not correctly set, however I'm new to this -scriptblock feature and I don't know how to append all VMs data:

$winVMS = Get-VM  | ?{$_.Guest.GuestFamily -like "win*" -and $_.PowerState -eq "PoweredOn"  }

$Credential = Get-Credential

ForEach($vm in $winVMS){

    $Computer = $vm + "<domainName>"

    Invoke-Command -ComputerName $Computer -Credential $Credential -ScriptBlock {

        Get-LocalGroupMember -Group "Administrators"

       

    } | Export-Csv -Path 'C:\Users\xxxxxx\Desktop\localAdmins.csv' -NoTypeInformation

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$Credential = Get-Credential 

Get-VM  | Where-Object{$_.Guest.GuestFamily -like "win*" -and $_.PowerState -eq "PoweredOn"  } |

ForEach-Object -Process {

    Invoke-Command -ComputerName "$($_.Name).<domainname>" -Credential $Credential -ScriptBlock { 

        Get-LocalGroupMember -Group "Administrators" 

    }

} | Export-Csv -Path 'C:\Users\xxxxxx\Desktop\localAdmins.csv' -NoTypeInformation  -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$Credential = Get-Credential 

Get-VM  | Where-Object{$_.Guest.GuestFamily -like "win*" -and $_.PowerState -eq "PoweredOn"  } |

ForEach-Object -Process {

    Invoke-Command -ComputerName "$($_.Name).<domainname>" -Credential $Credential -ScriptBlock { 

        Get-LocalGroupMember -Group "Administrators" 

    }

} | Export-Csv -Path 'C:\Users\xxxxxx\Desktop\localAdmins.csv' -NoTypeInformation  -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

ehi Master Luc.. thanks first of all! I get

Select-Object : Property "ScriptOutput" cannot be found

I'm on PS 5.1 and PowerCLI 6.5.1 build 5377412, could it be because I need to update something on my side?

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

I ran your script as is except for the fact that after Get-VM I added two VMs to do a quick test. We have a big environment and to do just a test it would take too long. The precise error is:

Select-Object : Property "ScriptOutput" cannot be found.

At line:10 char:9

+     } | Select-Object -ExpandProperty ScriptOutput | ConvertFrom-Csv

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (xxxx\Domain Admins:PSObject) [Select-Object], PSArgumentException

    + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I misread, I thought you were using Invoke-VMScript.
I corrected the code above, please give it another try


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

Thanks again Luc! Flawless..

Reply
0 Kudos