简体   繁体   中英

Comparing AD Group and a CSV in Powershell

Im quite new to Powershell and I've hit a brickwall finding information from a CSV. I have an AD Group of users and im trying to find entries that include their samAccountName from a CSV.

The CSV has the headings samAccountname, IP, Subnet and Hostname. For example here is an extract: neilp,10.1.52.22,10.1.52.0,Hostname01

I ultimately need to find all the Hostnames of any samAccountnames in the CSV that are also in the AD Group.

I've been trying the Compare-Object cmdlet but have had no success:

Import-Module ActiveDirectory

$Test_Users = Get-ADGroupMember Test_Users
$Data = Import-csv .\Data.csv

$Data | ForEach-Object {Compare-Object $_ $Test_Users-ExcludeDifferent }

Can anyone help?

My way of solving such a problem is to concatenate the two arrays of objects and then use Group-Object CmdLet. Just be carefull to have a column header in your CVS with the same name of the propery in the Get-ADGroupMember CmdLet.

Import-Module ActiveDirectory

$a =  Import-csv .\Data.csv
$b =  Get-ADGroupMember Test_Users

$c = $a + $b | group-object -property XXXX

Then you can loop through $c array to build a new object.

Try this:

Import-Module ActiveDirectory

$Test_Users = Get-ADGroupMember Test_Users
$Data = Import-Csv .\Data.csv

Compare-Object $Data $Test_Users -Prop sAMAccountName -Include -Exclude | % {
  $account = $_.sAMAccountName
  $Data | ? { $_.sAMAccountName -eq $account } | % { $_.Hostname }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM