active directory - RESOLVED get-ADUser with parameters in a foreach loop from csv with powershell -
i try lookup ad powershell 3 , check if users (samaccountname) in csv existing , active in ad. hopping 1 error next 1 based on brackets , parenthesis not find correct way. tried different filters, tried pass them through variable end "a parameter cannot found matches parameter name -eq...". looks error @ get-aduser $adsearch not set therefore fails evaluate if($adsearch).
the csv file sanitized (leading spaces etc.) , looks (only 1 column samaccountname):
john.doe jane.doe doej2 foo.bar barf5
any tip highly appreciated. thanks!
param ($inputfile='.\users.csv',$logfile='.\log.csv') $csv = import-csv $inputfile #"samaccountname,search result" | add-content $logfile foreach ($user in $csv){ echo $user; #$adsearch = get-aduser -filter 'samaccountname -eq "$($user)" -and enabled -eq $true' #$filter = "samaccountname -eq $($user) -and enabled -eq $($true)" #write-host "filter $filter" $adsearch = get-aduser -filter "(samaccountname -eq '$($user)') -and (enabled -eq '$($true)')" echo $adsearch if ($adsearch){ echo "found"; echo "$error" #"$user, found" | add-content $logfile } else{ echo "not found"; echo "$error" #"$user, not found" | add-content $logfile } }
for reference leave buggy script above, here working script (thanks tomer , charlie joynt):
param ($inputfile='.\users.csv',$logfile='.\log.csv') $csv = import-csv $inputfile "samaccountname,search result,error" | add-content $logfile foreach ($user in $csv){ $username = $user.samaccountname; $adsearch = get-aduser -filter {(samaccountname -eq $username) -and (enabled -eq $true)} echo "search active user $user" if ($adsearch){ echo "found"; "$user, found" | add-content $logfile } else{ echo "not found"; "$user, not found, $error" | add-content $logfile } }
for csv file showed, when add header line samaccountname
, following works me:
$username = $user.samaccountname $adsearch = get-aduser -filter {(samaccountname -eq $username) -and (enabled -eq $true)}
notice curly brackets instead of quote marks.
Comments
Post a Comment