Naser Gabaj
asked on
Need powershell script
I have excel csv file that has one column (User ID) in OU in Active directory, I need to create powershell script that going to check and verify on each one of them if they are already exist in Active directory or not and if it's exist write the group name that he belong to
ASKER
yes. exactly
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
to be able to use my csv file as an input to this script I should replace the part where it says "$users = "jacobd","talonssss"" to be :
Import-Csv -Path "Y:\MissingMembers.csv" | ForEach-Object {<YOUR SCRIPT>}...right?
Import-Csv -Path "Y:\MissingMembers.csv" | ForEach-Object {<YOUR SCRIPT>}...right?
Yes
You might need to use $user.userid depending on how the CSV looks.
$users = import-csv c:\path\to\file.csv
You might need to use $user.userid depending on how the CSV looks.
ASKER
I feel I'm almost there but the result is not as I expect, please see attached, is there a way I can direct my search to look into a specific OU name?
Should I replace the part
" $ADUser = Get-ADUser -Identity $user -Properties primarygroup -ErrorAction Stop"
with the
" $ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=XYZ' -ErrorAction Stop"
Please confirm
2018-10-23_15-01-31.jpg
Should I replace the part
" $ADUser = Get-ADUser -Identity $user -Properties primarygroup -ErrorAction Stop"
with the
" $ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=XYZ' -ErrorAction Stop"
Please confirm
2018-10-23_15-01-31.jpg
Give me the entirety of the code.
You're not piping anything into the identity parameter which means one of the variables is off.
You're not piping anything into the identity parameter which means one of the variables is off.
ASKER
$users = import-csv R:\Groups\Members.csv
foreach ($user in $users)
{
try {
$ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=<interestedGroup>,DC=< DOMAIN>,DC =com' -ErrorAction Stop
}
catch {
if ($_ -like "*Cannot find an object with identity: '$user'*") {
"User '$user' does not exist."
}
else {
"An error occurred: $_"
}
continue
}
"User '$($ADUser.SamAccountName) ' exists and his primary group is $(($aduser.Primarygroup -replace "CN=|,.*") -join ", ")."
}
foreach ($user in $users)
{
try {
$ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=<interestedGroup>,DC=<
}
catch {
if ($_ -like "*Cannot find an object with identity: '$user'*") {
"User '$user' does not exist."
}
else {
"An error occurred: $_"
}
continue
}
"User '$($ADUser.SamAccountName)
}
As Jacob mentioned, you need to use $user.userid. Like this:
IF your CSV file's header is actually 'User ID' then you will need to use $user."User ID"
$ADUser = Get-ADUser $user.userid ...
IF your CSV file's header is actually 'User ID' then you will need to use $user."User ID"
$ADUser = Get-ADUser $user."User ID" ...
ASKER
Thank you Jacob for your help and I apologize for the delay to reply to you.
userid
----------
user1
user2