how to avoid error message being displayed

Hi All,

I’ve got this code and want to check if a samaccount name already exists if yes then using the first 2 letters of the first name and try again.
[pre]

$givenname = "paul"
$firstletter = $givenname.Substring(0,1)
$lastname ="Washington"
$TestSam = $firstletter + $lastname
$testAccountNameExist = get-user -Identity $TestSam
if(!($testAccountNameExist -eq "")){
write-host -ForegroundColor Yellow "This Username: $TestSam already exists going for alternative name please wait"
try{
#here I want just to see that the new samaccount name does not exist and that I don't get the output in red.
if(!($testAccountNameExist -eq "")){
$Firstletter = $givenname.Substring(0,2)
$TestSam = $firstletter + $lastname
$testAccountNameExist = get-user -Identity $testsam}
}catch{
Write-host "this SamAccountName does not exist"}
if($testAccountNameExist -eq ""){
write-host -ForegroundColor Cyan "SamaccountName that is available is :" + $TestSam
}
}
[/pre]
so basically my goal is check if samaccount exist if yes going for the alternative which is first 2 letters firstname + lastname and do the check again. if not found just show the text this samaccountname does not exist, and can be used
thanks for your help best regards
Paul

You could use a filter to find the account which doesnt display an error if the result is null. The search filter may can take longer than directly searching for the account.

[pre]

$varAccount = get-aduser -filter {SamAccountName -eq $TestSam}

If ($varAccount -ne $null)

{ “$($varAccount.SamAccountName) Found!”}

else

{“$($varAccount.SamAccounttName) Not Found!”}

[/pre]

… or you could take a look at proper error handling with Powershell … :wink:

Get-Help about_Try_Catch_Finally -ShowWindow

Hi Olaf thanks for your reaction

What am I doing wrong with my try catch?

I’ve modified it into this
[pre]

$givenname = “paul”
$firstletter = $givenname.Substring(0,1)
$lastname =“Washington”
$TestSam = $firstletter + $lastname
$testAccountNameExist = get-user -Identity $TestSam
if(!($testAccountNameExist -eq “”)){
write-host -ForegroundColor Yellow “This Username: $TestSam already excists going for alternative name please wait”

if(!($testAccountNameExist -eq $null)){
$Firstletter = $givenname.Substring(0,2)
$TestSam = $firstletter + $lastname}
try{
$testAccountNameExist = get-user -Identity $TestSam
}catch [ManagementObjectNotFoundException]{

“this accountname does not excist”}

write-host -ForegroundColor Cyan “Username that is available is :” $TestSam

}
[/pre]

but the error message remains
[pre]
The operation couldn’t be performed because object ‘pawashington’ couldn’t be found on ‘mydomain.com’.

  • CategoryInfo : NotSpecified: (:slight_smile: [Get-User], ManagementObjectNotFoundException
  • FullyQualifiedErrorId : [Server=myserver,RequestId=12a93894-a1ff-47d1-b3e3-d91d34c542b7,TimeStamp=7/26/2020 11:21:26 AM] [FailureCate
    gory=Cmdlet-ManagementObjectNotFoundException] 7A347785,Microsoft.Exchange.Management.RecipientTasks.GetUser
  • PSComputerName : mydomain.com
    [/pre]

 

You want to catch the error thrown by Get-ADUser but you call this cmdlet outside of the try block. :wink: AND … Get-ADUser does not trigger terminating errors by default. But that’s needed for the try catch block to work as intended. So you will have to force it to do so with the parameter -ErrorAction Stop and move the cmdlet inside the try block.

Hi Olaf,
sorry I have to disagree here

in my First test
[pre]

$givenname = “paul”
$firstletter = $givenname.Substring(0,1)
$lastname =”Washington”
$TestSam = $firstletter + $lastname
$testAccountNameExist = get-user -Identity $TestSam
if(!($testAccountNameExist -eq “”)){
write-host -ForegroundColor Yellow “This Username: $TestSam already excists going for alternative name please wait”
[/pre]

I know the user exists so I want to go to round 2 by using this code

[pre]

if(!($testAccountNameExist -eq $null)){
$Firstletter = $givenname.Substring(0,2)
$TestSam = $firstletter + $lastname}
try{
$testAccountNameExist = get-user -Identity $TestSam
}catch [ManagementObjectNotFoundException]{

“this accountname does not excist”}

[/pre]
where my try block is  
[pre]
try{ 
$testAccountNameExist = get-user -Identity $TestSam 
}catch [ManagementObjectNotFoundException]
{ “this accountname does not excist”}

[/pre]

or am I missing something here?

Your first call of Get-ADUser is not wrapped in a try block. That’s what I meant. And because Get-ADUser does not throw terminating errrors by default you have to add -ErrorAction Stop to it inside the try block.

EDIT:
Oooops … my mistake … I miss read Get-User as Get-ADUser … so ignore this :wink:

What is Get-User? The first call to it is still not in a try/catch. Why not handle it like this instead of all these checks? It’s simple, if it succeeds it will continue down the Try statement list. If it doesn’t, it will execute what’s in the Catch statement list.

$ErrorActionPreference = "Stop"

$givenname = “paul”

$firstletter = $givenname.Substring(0,1)

$lastname =”Washington”

$TestSam = $firstletter + $lastname

Try
{
    $testAccountNameExist = get-user -Identity $TestSam
    write-host -ForegroundColor Yellow “This Username: $TestSam already exists going for alternative name please wait”
    if(!($testAccountNameExist -eq “”)){

        $Firstletter = $givenname.Substring(0,2)

        $TestSam = $firstletter + $lastname

        Try
        {
            $testAccountNameExist = get-user -Identity $testsam
        }
        Catch
        {
            write-host -ForegroundColor Cyan “SamaccountName that is available is :” + $TestSam
        }
    }
}
Catch
{
    Write-host “this SamAccountName does not exist”
}

Get-User

Anyway … to get the try block work properly you will have to add -ErrorAction Stop !!!

Or as I did for the whole script

$ErrorActionPreference = "Stop"

Oooops … I actually missed that … sorry … of course you’re right. :wink: :smiley:

HI Olaf and Doug thanks for your reactions was not aware that you could nest 2 try blocks together

 

 

If the code is being executed in a remote Powershell session, Exchange doesn’t process -ErrorAction Stop properly and you need to define it like @Doug provided as globally set to ErrorAction to Stop. Bug that has bitten me, thought I would chime in. This code is unnecessary:

if(!($testAccountNameExist -eq “”)){ ...

Using Get-User is either going to find a user or not. If it finds the user, the variable will be set, otherwise you are going to the catch block. Even if you wanted to double check, it should just be:

if ( $testAccountNameExist ) { ...

which is if the variable is NOT null, do X

@rob Simmers
thanks for the explanation