As mentioned in another post here, I’m working on module for handling users in our AD. It’s easy to mistype a user or computer name so I’d like to add a check to see if an object exists in the AD before running the code.
However, whenever I look at info about the try/catch error handling, it never goes beyond just the try/catch it never describes how you write the code to be run after a success.
Basically what I’d like to have spelled out is how do exactly do I proceed after passing the try-test?
Do i write the test like this:
try {
[bool] (Get-ADUser -Filter {samAccountName -eq $UserName}) -ErrorAction Stop
}
catch {
Write-Warning "Can't you spell? User does not exist"
}
# Success!
Do-WhateverAction to $UserName
or do I write it like this - with all of the code embedded within the try code block:
try {
[bool] (Get-ADUser -Filter {samAccountName -eq $UserName}) -ErrorAction Stop
# Success
Do-WhateverAction to $UserName
}
catch {
Write-Warning "Can't you spell? User does not exist"
}
generally i approach it by including the code to execute as part of the initial try…
so
[pre]
try
{
get-aduser $username #do something with valid user here
}
catch
{ #user not found, place error handling code here, notify user with popup whatever
}
[/pre]
[quote quote=136500]You can add a Finally block that contains an If-else statement.
[/quote]
Never found a real usecase for the Finally block…
It always appeared to me as beeing useless because the code after the whole Try/Catch statement would anyway be executed.
If someone could provide a good real example (not just a theoratical one)…
I find that sometimes tutorials are too focused on the exact functionality it is teaching and forgets to show how it is, or can be, used in real world scenarios.
I will agree with a couple of you that the finally block seems, at first glance, mostly pointless.
That does not produce a terminating error, it just has no results because the search was unsuccessful, so try\catch is pointless. For a terminating error, you can do:
Get-ADUser -Identity $UserName
or I normally do this:
$user = Get-ADUser -Filter {samAccountName -eq $UserName}
if ($user) {
Set-ADUser -Identity $user ...
}
else {
'User {0} was not found' -f $username
}
I typically use Rob’s method above, for simplicity as much as anything. However, it is worth noting that you can use try/catch on specific error types, if you know what they are likely to be.
In this case, the most likely problem (if a username is mistyped) is that the AD user account won’t be found. Therefore, you can specifically deal with a Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException as follows:
$Username = "mickeymouse"
$Forename = "Mickey"
$Surname = "Mouse"
try{
Set-ADUser -Identity $Username -GivenName $Forename -Surname $Surname
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
Write-Warning "This is not Disneyland!"
}
catch
{
Write-Warning "$Username exists, but something else went wrong: $($_.Exception.Message)"
}
This gives you the ability to handle specific types of error, before the final catch block deals with anything else. Note that this method starts with “Set-ADUser”, without making a preliminary “Get-ADUser” call to Active Directory. This may have a slight performance benefit (not tested).
In this case a simple if/else is probably just as good a solution as a try/catch, but this is just as much about teaching me the best ways of scripting solutions in PS as it is
I was aware of the problem with Get-ADUser -Filter not producing a terminating error, which is why I cast it as boolean in the OP. Have switched to the Get-ADUser -Identity in the version of the script I’m running now.
I was not aware there was a specific error type for AD Identity Not Found, that’s cool and something I will be using going forward.