I have written this powershell script but i get an error when I run it. I’m sure the syntax is off on something but staring at it has yielded no eureka moments. I believe the issue is in the “if not null” statement but I’m not sure. The error is “Get-ADuser : The search filter cannot be recognized” if(Get-ADUser -filter $filter) The CSV’s fields are pretty obvious except maybe GR = grade level<\/p>\n
#begin fie processing\nImport-Module activedirectory\n\n#Store the data from ADUsers.csv in the $ADUsers variable\n$ADUsers = Import-csv \"C:\\sftp\\Student.csv\"\n\n#Set Current School Year Variable\n$currentschoolyr = 2016\n\n#Loop through each row containing user details in the CSV file \nforeach ($User in $ADUsers)\n{\n if ($User.StateIDNumber -ne $null -or $User.GR -ne $null)\n {\n #Read user data from each field in each row and assign the data to a variable as below\n\n $stFirstorg = $User.FName\n $stLastorg = $User.LName\n $stMiddleorg = $User.MName\n $stoffice = $User.GR\n $stPassword = $User.StateIDNumber\n\t\n\t#Calculate Graduation Year\n\tif ($User.GR -match '^\\d+$')\n\t\t{\n $stGrad = 13-$User.GR+$currentschoolyr\n\t\t}\n\telse\n\t\t{\n\t\t$stGrad = $currentschoolyr + 13\n\t\t}\n\t\t\n\t#Manipulate Text length and contents\t\n [string]$stGraduation = $stGrad\n $stFirstname = $stFirstorg.ToLower()\n $stLastname = $stLastorg.ToLower() \n $stMiddlename = $stMiddleorg.ToLower()\n $stmidname = $stMiddlename.substring(0,[System.Math]::Min(6, $stMiddlename.Length))\n $stfirstint = $stFirstname.substring(0,1)\n\n if (-not $stMiddleorg)\n {\n $stMidint = $null\n $dispname =$stFirstname + \" \" + $stLastname\n }\n else\n {\n $stMidint = $stMiddlename.substring(0,1)\n $dispname =$stFirstname + \" \" + $stMiddlename + \" \" + $stLastname\n }\n\n $stlastman1 = $stLastname.replace(' ','')\n\t$stlastman2 = $stlastman1.replace('-','')\n\t$stlastclean = $stlastman2\n $stgradyr =$stGraduation.substring(2,2)\n\t\n $userFML=$stfirstint+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))\n $stUser=$userFML+$stgradyr\n\n\t#Check it user account exists\n if (Get-ADUser -F {SamAccountName -eq $stUser})\n { \n # Write-Warning \"User account exists, add second char to first name\"\n $stfirst2 = $stFirstname.substring(0,2)\n $stUser2=$stfirst2+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))\n $stUsername = $stUser2\n }\n else\n {\n #Username $stUser is Good!\"\n $stUsername = $stUser\n }\n \n $OU = \"OU=imported,DC=mydomain,DC=local\"\n $stdisplayname = $dispname\n $sttitle = $User.StateIDNumber\n $stdepartment = \"Student\"\n $Domain=\"@ADDomain.local\"\n $emaildomain=\"@Myemaildomain.com\"\n $UPN=$stUsername+$Domain\n $STEmail=$stUsername+$emaildomain\n\n #Check to see if the user already exists in AD\n $filter = \"Title -eq '$($sttitle)'\"\n #Write-Host \"Filter is $filter\"\n if (Get-ADUser -filter $filter)\n {\n #If user does exist, give a warning\n #Write-Warning \"A user account with name $stdisplayname already exists in Active Directory.\"\n #Write-hostName $stdisplayname \n #Write-hostSamAccountName $stUsername \n #Write-hostUserPrincipalName $UPN \n #Write-hostGivenName $stFirstname \n #Write-hostSurname $stLastname \n #Write-hostInitials $stmidname \n #Write-hostDepartment $stdepartment \n #Write-hostDisplayName $stdisplayname \n #Write-hostDescription $stGraduation \n #Write-hosttitle $stPassword \n #Write-hostoffice $stoffice \n #Write-hostEmailAddress $STEmail \n }\n else\n {\n Write-Warning \"Creating account for user $stdisplayname.\"\n #Write-host\n #Write-host Name $stdisplayname \n #Write-host SamAccountName $stUsername \n #Write-host UserPrincipalName $UPN \n #Write-host GivenName $stFirstname \n #Write-host Surname $stLastname \n #Write-host Initials $stmidname \n #Write-host Department $stdepartment \n #Write-host DisplayName $stdisplayname \n #Write-host Description $stGraduation \n #Write-host title $stPassword \n #Write-host office $stoffice \n #Write-host EmailAddress $STEmail\n \n #User does not exist then proceed to create the new user account\n #Account will be created in the OU provided by the $OU variable read from the CSV file\n #New-ADUser -Name $stdisplayname -SamAccountName $stUsername -UserPrincipalName $UPN -GivenName $stFirstname -Surname $stLastname -Initials $stmidname -Department $stdepartment -DisplayName $stdisplayname -Description $stGraduation -title $stPassword -office $stoffice -EmailAddress $STEmail -Path $OU -Enable $True -AccountPassword (convertto-securestring $stPassword -AsPlainText -Force) \n }\n }\n else\n {\n Write-Warning \"$User.LName ------- Missing INFO!!!!\"\n }\n \n \n\n} #end function\n\n<\/code><\/pre>\n
Thanks in advanced<\/p>","upvoteCount":4,"answerCount":8,"datePublished":"2016-08-16T18:57:23.000Z","author":{"@type":"Person","name":"jonathangeiselman","url":"https://community.spiceworks.com/u/jonathangeiselman"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n
jonathangeiselman:<\/div>\n
\n
The csv file is now (sam,GR,StateIDNumber) and if the last column is empty i get the error: \nGet-ADUser : The search filter cannot be recognized \nAt C:\\sftp\\logic test.ps1:25 char:13<\/p>\n
\nif (Get-ADUser -filter { Title -eq $sttitle } )<\/li>\n \n<\/code><\/pre>\n<\/li>\nCategoryInfo : NotSpecified: ( [Get-ADUser], ADException<\/li>\n FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman \nds.GetADUser<\/li>\n<\/ul>\nSo the line: if ($User.StateIDNumber -ne $null -or $User.GR -ne $null) is not working. its not seeing the column as being null/empty. if there is something in the column then it run great. Thanks again in advanced<\/p>\n<\/blockquote>\n<\/aside>\n
That’s because you’re using an -or statement - this expression will evaluate to true if either of the fields is not $null. Do you need to check if the grade level is set here? If not then you can change the expression to<\/p>\n
if( $User.StateIDNumber ){\n # Code here\n }\n<\/code><\/pre>\nIf you do have to check that field here, then the -and operator would be more appropriate.<\/p>\n
if( $User.StateIDNumber -and $User.GR ){\n # Code here\n }\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2016-08-17T15:00:10.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/7","author":{"@type":"Person","name":"gungnir","url":"https://community.spiceworks.com/u/gungnir"}},"suggestedAnswer":[{"@type":"Answer","text":"I have written this powershell script but i get an error when I run it. I’m sure the syntax is off on something but staring at it has yielded no eureka moments. I believe the issue is in the “if not null” statement but I’m not sure. The error is “Get-ADuser : The search filter cannot be recognized” if(Get-ADUser -filter $filter) The CSV’s fields are pretty obvious except maybe GR = grade level<\/p>\n
#begin fie processing\nImport-Module activedirectory\n\n#Store the data from ADUsers.csv in the $ADUsers variable\n$ADUsers = Import-csv \"C:\\sftp\\Student.csv\"\n\n#Set Current School Year Variable\n$currentschoolyr = 2016\n\n#Loop through each row containing user details in the CSV file \nforeach ($User in $ADUsers)\n{\n if ($User.StateIDNumber -ne $null -or $User.GR -ne $null)\n {\n #Read user data from each field in each row and assign the data to a variable as below\n\n $stFirstorg = $User.FName\n $stLastorg = $User.LName\n $stMiddleorg = $User.MName\n $stoffice = $User.GR\n $stPassword = $User.StateIDNumber\n\t\n\t#Calculate Graduation Year\n\tif ($User.GR -match '^\\d+$')\n\t\t{\n $stGrad = 13-$User.GR+$currentschoolyr\n\t\t}\n\telse\n\t\t{\n\t\t$stGrad = $currentschoolyr + 13\n\t\t}\n\t\t\n\t#Manipulate Text length and contents\t\n [string]$stGraduation = $stGrad\n $stFirstname = $stFirstorg.ToLower()\n $stLastname = $stLastorg.ToLower() \n $stMiddlename = $stMiddleorg.ToLower()\n $stmidname = $stMiddlename.substring(0,[System.Math]::Min(6, $stMiddlename.Length))\n $stfirstint = $stFirstname.substring(0,1)\n\n if (-not $stMiddleorg)\n {\n $stMidint = $null\n $dispname =$stFirstname + \" \" + $stLastname\n }\n else\n {\n $stMidint = $stMiddlename.substring(0,1)\n $dispname =$stFirstname + \" \" + $stMiddlename + \" \" + $stLastname\n }\n\n $stlastman1 = $stLastname.replace(' ','')\n\t$stlastman2 = $stlastman1.replace('-','')\n\t$stlastclean = $stlastman2\n $stgradyr =$stGraduation.substring(2,2)\n\t\n $userFML=$stfirstint+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))\n $stUser=$userFML+$stgradyr\n\n\t#Check it user account exists\n if (Get-ADUser -F {SamAccountName -eq $stUser})\n { \n # Write-Warning \"User account exists, add second char to first name\"\n $stfirst2 = $stFirstname.substring(0,2)\n $stUser2=$stfirst2+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))\n $stUsername = $stUser2\n }\n else\n {\n #Username $stUser is Good!\"\n $stUsername = $stUser\n }\n \n $OU = \"OU=imported,DC=mydomain,DC=local\"\n $stdisplayname = $dispname\n $sttitle = $User.StateIDNumber\n $stdepartment = \"Student\"\n $Domain=\"@ADDomain.local\"\n $emaildomain=\"@Myemaildomain.com\"\n $UPN=$stUsername+$Domain\n $STEmail=$stUsername+$emaildomain\n\n #Check to see if the user already exists in AD\n $filter = \"Title -eq '$($sttitle)'\"\n #Write-Host \"Filter is $filter\"\n if (Get-ADUser -filter $filter)\n {\n #If user does exist, give a warning\n #Write-Warning \"A user account with name $stdisplayname already exists in Active Directory.\"\n #Write-hostName $stdisplayname \n #Write-hostSamAccountName $stUsername \n #Write-hostUserPrincipalName $UPN \n #Write-hostGivenName $stFirstname \n #Write-hostSurname $stLastname \n #Write-hostInitials $stmidname \n #Write-hostDepartment $stdepartment \n #Write-hostDisplayName $stdisplayname \n #Write-hostDescription $stGraduation \n #Write-hosttitle $stPassword \n #Write-hostoffice $stoffice \n #Write-hostEmailAddress $STEmail \n }\n else\n {\n Write-Warning \"Creating account for user $stdisplayname.\"\n #Write-host\n #Write-host Name $stdisplayname \n #Write-host SamAccountName $stUsername \n #Write-host UserPrincipalName $UPN \n #Write-host GivenName $stFirstname \n #Write-host Surname $stLastname \n #Write-host Initials $stmidname \n #Write-host Department $stdepartment \n #Write-host DisplayName $stdisplayname \n #Write-host Description $stGraduation \n #Write-host title $stPassword \n #Write-host office $stoffice \n #Write-host EmailAddress $STEmail\n \n #User does not exist then proceed to create the new user account\n #Account will be created in the OU provided by the $OU variable read from the CSV file\n #New-ADUser -Name $stdisplayname -SamAccountName $stUsername -UserPrincipalName $UPN -GivenName $stFirstname -Surname $stLastname -Initials $stmidname -Department $stdepartment -DisplayName $stdisplayname -Description $stGraduation -title $stPassword -office $stoffice -EmailAddress $STEmail -Path $OU -Enable $True -AccountPassword (convertto-securestring $stPassword -AsPlainText -Force) \n }\n }\n else\n {\n Write-Warning \"$User.LName ------- Missing INFO!!!!\"\n }\n \n \n\n} #end function\n\n<\/code><\/pre>\nThanks in advanced<\/p>","upvoteCount":4,"datePublished":"2016-08-16T18:57:23.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/1","author":{"@type":"Person","name":"jonathangeiselman","url":"https://community.spiceworks.com/u/jonathangeiselman"}},{"@type":"Answer","text":"
Seems like that part of the code should work, from my tests. Give it a try like this and see if it makes a difference.<\/p>\n
$Filter = { Title -eq $STTitle }\n\nif( Get-ADUser -Filter $Filter ){\n # Code here\n }\n<\/code><\/pre>\nOr, instead of the extra variable, try putting it all in a single line.<\/p>\n
if( Get-ADUser -Filter { Title -eq $STTitle } ){\n # Code here\n }\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2016-08-16T19:28:38.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/2","author":{"@type":"Person","name":"gungnir","url":"https://community.spiceworks.com/u/gungnir"}},{"@type":"Answer","text":"Consider stripping down the script to a much fewer lines. It is not easy to debug such long scripts. Doing so may help you see the issue!<\/p>","upvoteCount":0,"datePublished":"2016-08-16T19:53:00.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/3","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"
try {\n if (!(get-aduser -Filter {samaccountname -eq \"$stUser\"})){\n New-ADUser blah blah\n else {\n Write-Warning \"[WARNING] Samaccount for username [$($stUser)] already exists\"\n \n }\n<\/code><\/pre>\ntry with sam<\/p>","upvoteCount":0,"datePublished":"2016-08-16T21:40:10.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"
Sorry about the length, I’ve narrowed it down to if the last column is empty ex: data,data,data, it errors out. I’ll test the suggestions tomorrow. Thanks<\/p>","upvoteCount":0,"datePublished":"2016-08-17T13:32:54.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/5","author":{"@type":"Person","name":"jonathangeiselman","url":"https://community.spiceworks.com/u/jonathangeiselman"}},{"@type":"Answer","text":"
Gungnir,<\/p>\n
Making the change to the -filter statement yielded the same result: \nGet-ADUser : The search filter cannot be recognized \nAt C:\\sftp\\INow Student Import.ps1:86 char:9<\/p>\n
\nif (Get-ADUser -filter { Title -eq $sttitle } )<\/li>\n \n<\/code><\/pre>\n<\/li>\nCategoryInfo : NotSpecified: ( [Get-ADUser], ADException<\/li>\n FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman \nds.GetADUser<\/li>\n<\/ul>\ntfl:<\/p>\n
I thinned down the script to:<\/p>\n
#begin fie processing\nImport-Module activedirectory\n\n#Store the data from ADUsers.csv in the $ADUsers variable\n$ADUsers = Import-csv \"C:\\sftp\\test.csv\"\n\n#Loop through each row containing user details in the CSV file \nforeach ($User in $ADUsers)\n{\n if ($User.StateIDNumber -ne $null -or $User.GR -ne $null)\n {\n $stUser = $User.sam\n $sttitle = $User.StateIDNumber\n\t #Check it user account exists\n if (Get-ADUser -F {SamAccountName -eq $stUser})\n { \n Write-Warning \"$stUser found\"\n }\n else\n {\n Write-Warning \"$stUser not found\"\n }\n \n #Check to see if the user already exists in AD\n if (Get-ADUser -filter { Title -eq $sttitle } )\n {\n Write-Warning \"$sttitle found\"\n }\n else\n {\n Write-Warning \"$sttitle not found\"\n }\n }\n else\n {\n Write-Warning \"$User.LName ------- Missing INFO!!!!\"\n }\n \n \n\n} #end function\n<\/code><\/pre>\nThe csv file is now (sam,GR,StateIDNumber) and if the last column is empty i get the error: \nGet-ADUser : The search filter cannot be recognized \nAt C:\\sftp\\logic test.ps1:25 char:13<\/p>\n
\nif (Get-ADUser -filter { Title -eq $sttitle } )<\/li>\n \n<\/code><\/pre>\n<\/li>\nCategoryInfo : NotSpecified: ( [Get-ADUser], ADException<\/li>\n FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman \nds.GetADUser<\/li>\n<\/ul>\nSo the line: if ($User.StateIDNumber -ne $null -or $User.GR -ne $null) is not working. its not seeing the column as being null/empty. if there is something in the column then it run great. Thanks again in advanced<\/p>","upvoteCount":0,"datePublished":"2016-08-17T14:07:11.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/6","author":{"@type":"Person","name":"jonathangeiselman","url":"https://community.spiceworks.com/u/jonathangeiselman"}},{"@type":"Answer","text":"
Thanks Gungnir! I was over complicating it, that did the trick.<\/p>","upvoteCount":0,"datePublished":"2016-08-17T15:53:59.000Z","url":"https://community.spiceworks.com/t/powershell-csv-import-error/518738/8","author":{"@type":"Person","name":"jonathangeiselman","url":"https://community.spiceworks.com/u/jonathangeiselman"}}]}}
I have written this powershell script but i get an error when I run it. I’m sure the syntax is off on something but staring at it has yielded no eureka moments. I believe the issue is in the “if not null” statement but I’m not sure. The error is “Get-ADuser : The search filter cannot be recognized” if(Get-ADUser -filter $filter) The CSV’s fields are pretty obvious except maybe GR = grade level
#begin fie processing
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "C:\sftp\Student.csv"
#Set Current School Year Variable
$currentschoolyr = 2016
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
if ($User.StateIDNumber -ne $null -or $User.GR -ne $null)
{
#Read user data from each field in each row and assign the data to a variable as below
$stFirstorg = $User.FName
$stLastorg = $User.LName
$stMiddleorg = $User.MName
$stoffice = $User.GR
$stPassword = $User.StateIDNumber
#Calculate Graduation Year
if ($User.GR -match '^\d+$')
{
$stGrad = 13-$User.GR+$currentschoolyr
}
else
{
$stGrad = $currentschoolyr + 13
}
#Manipulate Text length and contents
[string]$stGraduation = $stGrad
$stFirstname = $stFirstorg.ToLower()
$stLastname = $stLastorg.ToLower()
$stMiddlename = $stMiddleorg.ToLower()
$stmidname = $stMiddlename.substring(0,[System.Math]::Min(6, $stMiddlename.Length))
$stfirstint = $stFirstname.substring(0,1)
if (-not $stMiddleorg)
{
$stMidint = $null
$dispname =$stFirstname + " " + $stLastname
}
else
{
$stMidint = $stMiddlename.substring(0,1)
$dispname =$stFirstname + " " + $stMiddlename + " " + $stLastname
}
$stlastman1 = $stLastname.replace(' ','')
$stlastman2 = $stlastman1.replace('-','')
$stlastclean = $stlastman2
$stgradyr =$stGraduation.substring(2,2)
$userFML=$stfirstint+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))
$stUser=$userFML+$stgradyr
#Check it user account exists
if (Get-ADUser -F {SamAccountName -eq $stUser})
{
# Write-Warning "User account exists, add second char to first name"
$stfirst2 = $stFirstname.substring(0,2)
$stUser2=$stfirst2+$stMidint+$stlastclean.substring(0,[System.Math]::Min(16, $stlastclean.Length))
$stUsername = $stUser2
}
else
{
#Username $stUser is Good!"
$stUsername = $stUser
}
$OU = "OU=imported,DC=mydomain,DC=local"
$stdisplayname = $dispname
$sttitle = $User.StateIDNumber
$stdepartment = "Student"
$Domain="@ADDomain.local"
$emaildomain="@Myemaildomain.com"
$UPN=$stUsername+$Domain
$STEmail=$stUsername+$emaildomain
#Check to see if the user already exists in AD
$filter = "Title -eq '$($sttitle)'"
#Write-Host "Filter is $filter"
if (Get-ADUser -filter $filter)
{
#If user does exist, give a warning
#Write-Warning "A user account with name $stdisplayname already exists in Active Directory."
#Write-hostName $stdisplayname
#Write-hostSamAccountName $stUsername
#Write-hostUserPrincipalName $UPN
#Write-hostGivenName $stFirstname
#Write-hostSurname $stLastname
#Write-hostInitials $stmidname
#Write-hostDepartment $stdepartment
#Write-hostDisplayName $stdisplayname
#Write-hostDescription $stGraduation
#Write-hosttitle $stPassword
#Write-hostoffice $stoffice
#Write-hostEmailAddress $STEmail
}
else
{
Write-Warning "Creating account for user $stdisplayname."
#Write-host
#Write-host Name $stdisplayname
#Write-host SamAccountName $stUsername
#Write-host UserPrincipalName $UPN
#Write-host GivenName $stFirstname
#Write-host Surname $stLastname
#Write-host Initials $stmidname
#Write-host Department $stdepartment
#Write-host DisplayName $stdisplayname
#Write-host Description $stGraduation
#Write-host title $stPassword
#Write-host office $stoffice
#Write-host EmailAddress $STEmail
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
#New-ADUser -Name $stdisplayname -SamAccountName $stUsername -UserPrincipalName $UPN -GivenName $stFirstname -Surname $stLastname -Initials $stmidname -Department $stdepartment -DisplayName $stdisplayname -Description $stGraduation -title $stPassword -office $stoffice -EmailAddress $STEmail -Path $OU -Enable $True -AccountPassword (convertto-securestring $stPassword -AsPlainText -Force)
}
}
else
{
Write-Warning "$User.LName ------- Missing INFO!!!!"
}
} #end function
Thanks in advanced
4 Spice ups
gungnir
(Gungnir)
August 16, 2016, 7:28pm
2
Seems like that part of the code should work, from my tests. Give it a try like this and see if it makes a difference.
$Filter = { Title -eq $STTitle }
if( Get-ADUser -Filter $Filter ){
# Code here
}
Or, instead of the extra variable, try putting it all in a single line.
if( Get-ADUser -Filter { Title -eq $STTitle } ){
# Code here
}
DoctorDNS
(DoctorDNS)
August 16, 2016, 7:53pm
3
Consider stripping down the script to a much fewer lines. It is not easy to debug such long scripts. Doing so may help you see the issue!
jitensh
(JitenSh)
August 16, 2016, 9:40pm
4
try {
if (!(get-aduser -Filter {samaccountname -eq "$stUser"})){
New-ADUser blah blah
else {
Write-Warning "[WARNING] Samaccount for username [$($stUser)] already exists"
}
try with sam
Sorry about the length, I’ve narrowed it down to if the last column is empty ex: data,data,data, it errors out. I’ll test the suggestions tomorrow. Thanks
Gungnir,
Making the change to the -filter statement yielded the same result:
Get-ADUser : The search filter cannot be recognized
At C:\sftp\INow Student Import.ps1:86 char:9
if (Get-ADUser -filter { Title -eq $sttitle } )
CategoryInfo : NotSpecified: ( [Get-ADUser], ADException
FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman
ds.GetADUser
tfl:
I thinned down the script to:
#begin fie processing
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "C:\sftp\test.csv"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
if ($User.StateIDNumber -ne $null -or $User.GR -ne $null)
{
$stUser = $User.sam
$sttitle = $User.StateIDNumber
#Check it user account exists
if (Get-ADUser -F {SamAccountName -eq $stUser})
{
Write-Warning "$stUser found"
}
else
{
Write-Warning "$stUser not found"
}
#Check to see if the user already exists in AD
if (Get-ADUser -filter { Title -eq $sttitle } )
{
Write-Warning "$sttitle found"
}
else
{
Write-Warning "$sttitle not found"
}
}
else
{
Write-Warning "$User.LName ------- Missing INFO!!!!"
}
} #end function
The csv file is now (sam,GR,StateIDNumber) and if the last column is empty i get the error:
Get-ADUser : The search filter cannot be recognized
At C:\sftp\logic test.ps1:25 char:13
if (Get-ADUser -filter { Title -eq $sttitle } )
CategoryInfo : NotSpecified: ( [Get-ADUser], ADException
FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman
ds.GetADUser
So the line: if ($User.StateIDNumber -ne $null -or $User.GR -ne $null) is not working. its not seeing the column as being null/empty. if there is something in the column then it run great. Thanks again in advanced
gungnir
(Gungnir)
August 17, 2016, 3:00pm
7
jonathangeiselman:
The csv file is now (sam,GR,StateIDNumber) and if the last column is empty i get the error:
Get-ADUser : The search filter cannot be recognized
At C:\sftp\logic test.ps1:25 char:13
if (Get-ADUser -filter { Title -eq $sttitle } )
CategoryInfo : NotSpecified: ( [Get-ADUser], ADException
FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Comman
ds.GetADUser
So the line: if ($User.StateIDNumber -ne $null -or $User.GR -ne $null) is not working. its not seeing the column as being null/empty. if there is something in the column then it run great. Thanks again in advanced
That’s because you’re using an -or statement - this expression will evaluate to true if either of the fields is not $null. Do you need to check if the grade level is set here? If not then you can change the expression to
if( $User.StateIDNumber ){
# Code here
}
If you do have to check that field here, then the -and operator would be more appropriate.
if( $User.StateIDNumber -and $User.GR ){
# Code here
}
Thanks Gungnir! I was over complicating it, that did the trick.