displaying null values/error checking

by shawnwat at 2013-01-03 08:17:06

I have a list of real names. I want to convert it to a list of User ID’s.
The following script works except if it can’t find the userID from the real name, the “name” is null and is not displayed in my finished list. I want a list of all the users real names and then user ID. if it can’t find the user i want it left blank in the final output.

Is this a formating issue or a error handling issue?

get-Content c:\userName.txt| foreach { Get-QADUser $_ }|select DisplayName, Name| out-File c:\UserID.txt
by DonJ at 2013-01-03 08:27:51
It’s neither. Get-QADUser might not be returning an error, it might just be returning a null set. You’re going to have to check for that, and doing so in a one-liner is going to be difficult. I’d suggest breaking this out into more of a script.

$names = get-content username.txt
foreach ($name in $names) {
$user = Get-QADUser $name
if ($user) {
$user | Select DisplayName,Name | Out-File userid.txt -append

Something like that will be a little easier to catch the condition and keep you from getting a blank. You could even have it log missing names to a separate file that way. Not saying you couldn’t do this in a one-liner without variables, but it gets visually more complex and there’s no benefit to doing it that way.

If Get-QADUser is in fact returning an error (I don’t have it, so I can’t check) then you’d have to implement error handling, which would look pretty similar.
by shawnwat at 2013-01-03 10:51:25
thank you