powershell if statement not executing -
i trying write powershell script automate ad user creation. mixed linux/windows environment , use text (now csv) file keep track of old linux uid #'s , usernames don't accidentally re-used. i'm trying script parse file can't seem comparison work.
$sam = $givenname.substring(0,1).tolower() + $lastname.tolower() $usernames=import-csv "uid.csv" foreach($users in $usernames){ if($usernames.username -contains $sam){$uid = $true} }
i've used several different ways i've found around web read data array file problem seems no matter what, never sets $uid = $true , have no idea why.
edit: changed if statement this:
if($users.username -contains $sam){$uid = $true}
and worked. help!
to clear confusion, $givenname , $lastname input user , looking exact match since i'm trying avoid re-used usernames. also, changing -contains -eq works well. i've been fighting way long , if couldn't tell i'm bit of powershell noob.
it's little unclear information you've provided, here's think problem: since $usernames comes csv file, $usernames.username string. -contains operator used on collections (such arrays), , work if there's exact match (and if you're looking exact matches, should use -eq, i'm assuming that's not want). use -match instead. also, you're setting $users each record in csv, should $users.username. change if statement this:
if ($users.username -match $sam) { $uid = $true }
Comments
Post a Comment