Hey All,
I have an issue with a PS script that I am trying to get functional. I have a User.csv file that I am importing and I want to compare it against all known users in Active Directory and tell me if there AD Account is flagged to:
Enabled: True
or
Enabled: False
If not found, I would like it to output the unknown account to an excel Spreadsheet (-Append).
I have the scripting for the first portion working, but I can’t seem to get it to do the second part.
-
Import-CSV
-
Check if account exists.
2.a If Account exists, check if enabled is true/false
2.b If Account doesn’t exist, notate in another CSV that the account was not found.
$Path = Read-Host "Input the path for the csv to pull from." $Application = Read-Host "What application is this for?" $Users = Import-CSV -Path $Path Foreach ($User in $Users) { Try { # Use "EA Stop" to ensure the exception is caught. Get-ADUser -server <domain> -Filter "SamAccountName -like '*$($_.samaccountname)*'" -Properties SamAccountName, Name, EmailAddress, Enabled, LastLogonDate | select SamAccountName, Name, EmailAddress, Enabled, LastLogonDate -Erroraction Continue | Export-CSV "c:usersuseriddesktop$Application-results.csv" -NoTypeInformation -Append } Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { # The user was not found! Write-Warning "$($User.SamAccountName) was not found in this domain!" | Export-CSV "c:usersuseriddesktop$Application-notfound.csv" -NoTypeInformation -Append } Catch { # Some other terrible error occured! Write-Error "OHSHI" } }
submitted by /u/Yellow_Odd_Fellow
[link] [comments]
The post Compare CSV to AD – Export Unknown Accounts to Another CSV appeared first on How to Code .NET.