0

I'm working on a PowerShell script to change a local account name. Of course, the first step is to check that the account exists:

$user=[ADSI]"WinNT://$server/$oldName,user"

If the account exists, then no problem. But if it doesn't, then I get this error:

format-default : The following exception occurred while retrieving member >"distinguishedName": "The user name could not be found." + CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand

I can't figure out how to look for that error, report something like "$oldName not found" and continue on. From what I can tell, it isn't being thrown into an error variable, so I can't search for a "user name could not be found" string. Try-Catch-Finally seems to ignore the error.

I admit I'm weak on error-handling. It seems there's countless ways for something to fail, and my users always find new ones when using my scripts.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
James Brown
  • 327
  • 2
  • 11
  • 21
  • I added an answer to a linked question that might help, here: https://stackoverflow.com/questions/43145567/powershell-directoryservice-object-error-not-catched-neither-trapped/43617003 – Dave_J Apr 26 '17 at 09:24

3 Answers3

3

It seems like the command is actually throwing a terminating error. From about_preference_variables

"Neither $ErrorActionPreference nor the ErrorAction common parameter affect how Windows PowerShell responds to terminating errors (those that stop cmdlet processing)."

So when the command runs it is terminating the script even before it can move on to try and process a catch block.

Interestingly if you put it into a variable this behavior stops happening. I'd be curious to see if anyone has a better answer, but it looks like the solution from what I can see, would be an if statement based on the results of the variable.

$User = [ADSI]"WinNT://badserver/Name,user" 
If (! $User.Name)
{
    Throw "Issue retrieving user"
}

#Rest of script can continue here
Noah Sparks
  • 1,710
  • 13
  • 14
  • Thanks, Noah. But I'm not seeing a difference between your first line with mine. – James Brown Oct 08 '14 at 20:26
  • I thought you wanted to find out how to handle an error condition? The first line is irrelevant, this is just showing you how to handle the error, put your line in and it will do the same thing. I just changed it while I was testing. – Noah Sparks Oct 08 '14 at 20:27
  • Well, never mind. Looking for $user.name is all it took. Thank you, Noah. – James Brown Oct 08 '14 at 20:32
  • Let me clarify (for others and my future self.) What I was searching for errors on was just $user, not $user.name – James Brown Oct 08 '14 at 20:42
0

You can check whether a username exists in this way

[ADSI]::Exists("WinNT://$Server/$UserName")

It returns a Boolean value. If user exists, you get true, otherwise false.

Sridhar
  • 837
  • 1
  • 10
  • 21
  • That works, too, PortableDevil, except I needed to eliminate one of the backslashes between $Server and $UserName to get it to work. Thanks. – James Brown Oct 10 '14 at 17:27
0

I solved a similar issue by wrapping the command in a script block and using Invoke-Command.

$ChangePassword = {([adsi]"WinNT://domain/$Username,user").ChangePassword($CurrentPassword, $NewPassword)}
try {
    Invoke-Command -ScriptBlock $ChangePassword -ErrorAction Stop
}
catch {
    # Error handling code
}
dzampino
  • 123
  • 4