powershell - Filtering Get-aduser on csv contents -
i have csv formatted like
ln,fn jones,bob jagger,mick
and trying mail attribute out of ad these users running following:
$names = import-csv .\names4.csv foreach ($name in $names){ get-aduser -f {surname -eq "$name.ln" -and givenname -eq "$name.fn} -pr mail }
this returns nothing @ all. if echo $name.ln shows list of users first names csv unsure why above command not returning results.
inside context of expandable string (ie. in double-quotes), powershell parser expands references .
- remove ""
quotes:
$names = import-csv .\names4.csv foreach($name in $names){ get-aduser -filter {surname -eq $name.ln -and givenname -eq $name.fn} -properties mail }
alternatively, assign each property value it's own variable:
$names = import-csv .\names4.csv foreach($name in $names){ $sn = $name.ln $gn = $name.fn get-aduser -filter {surname -eq $sn -and givenname -eq $gn} -properties mail }
in other situation need dereference property inside expandable string, use $()
subexpression operator:
"the last name $($name.ln)"
but advise against activedirectory module, -filter
block has quirks nested expressions
Comments
Post a Comment