Cache Posted June 28, 2013 Posted June 28, 2013 Evening I've been updating my new user script and setting it up in powershell, the intention being it will later be combined into calling some powershell for Office 365. I've got it all working apart from one small part, it fails to check if a user already exists. Could someone look to see where I've gone wrong please? $usercheck = $null # check if username exists already in AD $usercheck = Get-ADUser -LDAPFilter "(sAMAccountName=$_.SamAccountName)" # Check search result to see if it's Null and then create the user If ($usercheck -eq $null) { Write-Host "User Check Completed" } # If it's not null, print out to screen Else { Write-Host "User: "$_.SamAccountName "already exists!"} } At the moment, regardless of whether the user exists or not, the logic seems to assume it doesn't and runs through the creation script. It's a shame because it took me all day yesterday and while I understand what most of it is doing, I don't understand this simple logic. Thanks!
Cache Posted June 28, 2013 Author Posted June 28, 2013 (edited) Never mind, fixed it myself when I've taken a more objective look and a bit more searching. Modified to the following: $name = $_.SamAccountName $usercheck = $null # check if username exists already in AD $usercheck = Get-ADUser -Filter {sAMAccountName -eq $name} # Check search result to see if it's Null and then create the user If ($usercheck -eq $null) { Write-Host "User Check Completed" } # If it's not null, print out to screen Else { Write-Host "User: "$_.SamAccountName "already exists!"} } Still don't understand why the first conditional failed though :/ Edited June 28, 2013 by Cache
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now