Help with script
Hello all
I have the below script. What i am trying to accomplish is this. below.
csv file contains list of users (upn)
PowerShell script reads all users from csv file (step1.) does a compare or hash table against users in three specific OU's in AD using (upn). The three specific OU's contain all of our vendor accounts
If a match is found, extend account expiration + 90 days
if a match is not found write the non matched accounts to a separate .csv file
However i am getting the following error
"Get-ADUser : Error parsing query: 'userPrincipalName -eq @{userprincipalname=Akhil.Gattu@mydomain.com}.UPN' Error Message: 'syntax error' at position: '23'.
At line:4 char:15
+ ... $u = Get-ADUser -Filter "userPrincipalName -eq $_.UPN" -Proper ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Script
- $OUNames = "OU=1,OU=X,DC=domain,DC=tld", "OU=2,OU=Y,DC=domain,DC=tld", "OU=3,OU=X,DC=domain,DC=tld"
- Import-Csv C:\Junk\AllHands.csv |
- ForEach-Object{
- $u = Get-ADUser -Filter "userPrincipalName -eq $_.UPN" -Properties AccountExpires,distinguishedName
- if ($u){
- $OU = ( $u.DistinguishedName.Substring($u.DistinguishedName).IndexOf('OU=',[System.StringComparison]::CurrentCultureIgnoreCase) )
- if ($OUNames -contains $OU){
- Set-ADAccountExpiration -Identity $u.distinguishedName -DateTime ([datetime]::fromfiletime($_.properties.accountexpires[0])).AddDays(90)
- }
- else{
- $_
- }
- }
- else {
- $_
- }
- } | Export-Csv C:\Junk\WhoAreThesePeople.csv -NoTypeInformation