Forum Discussion
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 @mailto:%7Buserprincipalname=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
2 Replies
Hi,
The problem might be the way you are getting the UPN value and the single quotes
Maybe you want to try with
Get-ADUser -Filter "userPrincipalName -eq '$_.UPN'"
You can also take a look at this script of mine where I do a similar thing
https://github.com/get-itips/M365EnterpriseDemoCustomizer/blob/dev/Customize-M365EnterpriseDemo.ps1
Do you have a column name in the CSV named UPN right?Edit: Typo 😉
- Skipster311-1Iron ContributorYes its upn in the import .csv file. This is finding the users, but What this code appears to be doing is writing whatever it finds in the import .csv file to the WhoAreThesePeople.csv . What i need are the *differences* between what is in the import .csv file and what is found in the OU's to be written to the WhoAreThesePeople.csv file and all matching accounts found in the .csv import and OU's should have their accountexpires + 90 days
$OUNames = "OU=FMI,OU=Cognizant,OU=FM Users,OU=Corp,DC=ip-tech,DC=com", "OU=BPO and RPA,OU=Cognizant,OU=Consultants,OU=Users,OU=Corp,DC=ip-tech,DC=com"
Import-Csv C:\temp\test2.csv |
ForEach-Object{
get-aduser -Filter "userPrincipalName -eq '$($_.upn)'"
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:\temp\WhoAreThesePeople.csv -NoTypeInformation