Question on how to deal with expected errors?

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Ed O'Connor Ed O’Connor 1 year, 3 months ago.

  • Author
    Posts
  • #33093
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    I found a few scripts online that I could use as a template for a GUI tool. All the display in the GUI is working as expected but I am having a issue dealing with a few expected errors. Basically, what I am doing is taking a AD SID and converting it into its AD Display Name. I have it set up so that if it matches the SID to a Name it displays the results and if the SID does not exist in AD that is displays a 'No associated AD object'. This all works, my issue is in Powershell and the errors it reports. Here is the script:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  
    
    $Form = New-Object System.Windows.Forms.Form    
    $Form.Size = New-Object System.Drawing.Size(400,200)  
    $Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedToolWindow #modifies the window border
    $Form.Text = "Translate GUID to User Name tool" #window description
    
    
    ############################################## Start functions
    
    function GUIDInfo 
    {
            $GUID = $InputBox.text;
            $objSID = New-Object System.Security.Principal.SecurityIdentifier $GUID
            Try
            {
            $GUIDResult = $objSID.Translate( [System.Security.Principal.NTAccount])
            }
            Catch 
            {
                [System.Security.Principal.IdentityNotMappedException]
            }
                If ($GUIDResult -notlike "ads\*") {$outputBox.Text = "No associated AD object"}
                Else {$outputBox.text = $GUIDResult}
    } 
    
    #end GUIDInfo
    
    ############################################## end functions
    
    
    ############################################## Start Label Field
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(20,20) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Please enter the GUID in the text box below:"
    $Form.Controls.Add($objLabel) 
    
    ############################################## Start text fields
    
    $InputBox = New-Object System.Windows.Forms.TextBox 
    $InputBox.Location = New-Object System.Drawing.Size(20,50) 
    $InputBox.Size = New-Object System.Drawing.Size(275,20) 
    $Form.Controls.Add($InputBox) 
    
    $outputBox = New-Object System.Windows.Forms.TextBox 
    $outputBox.Location = New-Object System.Drawing.Size(20,85) 
    $outputBox.Size = New-Object System.Drawing.Size(275,70) 
    $outputBox.MultiLine = $True 
    #$outputBox.ScrollBars = "Vertical" 
    $Form.Controls.Add($outputBox) 
    
    ############################################## end text fields
    
    ############################################## Start buttons
    
    $Button = New-Object System.Windows.Forms.Button 
    $Button.Location = New-Object System.Drawing.Size(300,30) 
    $Button.Size = New-Object System.Drawing.Size(80,50) 
    $Button.Text = "Get Name" 
    $Button.Add_Click({GUIDInfo}) 
    $Form.Controls.Add($Button) 
    
    ############################################## end buttons
    
    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
    

    I am able to handle any exceptions if the SID entered does not match anything in AD with the

    Catch
    {
    [System.Security.Principal.IdentityNotMappedException]
    }

    portion of the script. However if the input box is blank or ends with a "-" it generates a:

    ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    exception. I can't seem to find a way to address/"catch" both of these errors/exceptions.

    Any advice is greatly appreciated.

  • #33097
    Profile photo of tommymaynard
    tommymaynard
    Member

    You need to check/clean up the value in $InputBox.text, before you assign it to $GUID. Only assign it to $GUID when you're certain the value of $InputBox.text is not empty (blank), and doesn't end with a hyphen.

    I'd start by looking at the length of the value in $InputBox.text. If it's equal to zero, don't accept it — there's nothing there. As far as the value ending with a hyphen, do that next by trimming the end of the value in $InputBox.text.

    Here's an example of what you might do:

    If ($InputBox.Text.Length -ne 0) {
        $GUID = $InputBox.Text.TrimEnd('-')
    } Else {
        # Indicate to user they need to enter something in the text box.
        [System.Windows.Forms.MessageBox]::Show('Please enter a GUID.')   
    }

    You might look at ways to disable the button until there's a value in your textbox. Fun stuff!

    Edit: It occurred to me that you'll need to check for the hyphen inside the conditional portion of the If statement (the part in the parenthesis). This is because if someone just enters a hyphen into your text box, it'll get a length of 1, and then remove the hyphen, putting nothing into the $GUID variable. Sorry if this is hard to follow; let me know. I suppose you can instead, check for the hyphen before the If statement; it's up to you.

  • #33098
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    I guess that you're trying to only catch the System.Security.Principal.IdentityNotMappedException that the translate method might throw. Then your catch block should be:
    catch [System.Security.Principal.IdentityNotMappedException] {
    }

    You also might want to make sure that the $ErrorActionPreference is set to "Stop".

  • #33107
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    This is great. Thank You.

  • #33112
    Profile photo of Dan Potter
    Dan Potter
    Participant

    get-aduser accepts guid so you could simplify this to be a couple lines.

    function Call-guids_psf {
    
    	#----------------------------------------------
    	#region Import the Assemblies
    	#----------------------------------------------
    	[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    	[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    	#endregion Import Assemblies
    
    	#----------------------------------------------
    	#region Generated Form Objects
    	#----------------------------------------------
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$outputbox = New-Object 'System.Windows.Forms.TextBox'
    	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
    	$button1 = New-Object 'System.Windows.Forms.Button'
    	$errorprovider1 = New-Object 'System.Windows.Forms.ErrorProvider'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	$form1_Load = {
    		
    		
    		
    	}
    	
    	
    	$button1_Click = {
    		
    		$errorprovider1.Clear()
    		
    		try {
    			[guid]$textbox1.Text
    		} catch {
    			
    			$errorprovider1.BlinkStyle = "neverblink"
    			$errorprovider1.SetIconAlignment($textbox1, [System.Windows.Forms.ErrorIconAlignment]::MiddleLeft)
    			$errorprovider1.SetError($textbox1, "Not a guid")
    			$Error = $true
    		}
    		
    	if($Error -ne $true) {
    			$outputBox.text = (get-aduser $textbox1.Text -Properties displayname).displayname
    			
    		}
    		
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$button1.remove_Click($button1_Click)
    			$form1.remove_Load($form1_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$form1.SuspendLayout()
    	#
    	# form1
    	#
    	$form1.Controls.Add($outputbox)
    	$form1.Controls.Add($textbox1)
    	$form1.Controls.Add($button1)
    	$form1.ClientSize = '480, 262'
    	$form1.Name = 'form1'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# outputbox
    	#
    	$outputbox.Location = '50, 118'
    	$outputbox.Name = 'outputbox'
    	$outputbox.Size = '372, 20'
    	$outputbox.TabIndex = 2
    	#
    	# textbox1
    	#
    	$textbox1.Location = '50, 55'
    	$textbox1.Name = 'textbox1'
    	$textbox1.Size = '372, 20'
    	$textbox1.TabIndex = 1
    	#
    	# button1
    	#
    	$button1.Location = '347, 198'
    	$button1.Name = 'button1'
    	$button1.Size = '75, 23'
    	$button1.TabIndex = 0
    	$button1.Text = 'button1'
    	$button1.UseVisualStyleBackColor = $True
    	$button1.add_Click($button1_Click)
    	#
    	# errorprovider1
    	#
    	$errorprovider1.ContainerControl = $form1
    	$form1.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $form1.ShowDialog()
    
    } #End Function
    
    #Call the form
    Call-guids_psf | Out-Null
    
  • #33121
    Profile photo of tommymaynard
    tommymaynard
    Member

    Nice work, Dan. If you run with this Ed, you'll want to include a try-catch around the Get-ADUser command to prevent an error (Directory Object not found), if a GUID cannot be located in Active Directory.

        If ($Error -ne $true) {
            $outputBox.text = 
                try {
                    (Get-ADUser -Identity $textbox1.Text -Properties DisplayName).DisplayName
                } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
                    '***Directory object not found.****'
                } catch {
                    "Undetermined Error: $($Error[0].Exception.GetType().Fullname)"
                }
        }
    
  • #33122
    Profile photo of Dan Potter
    Dan Potter
    Participant

    In haste I used $error which we should probably avoid. Change it to $e or something else.

  • #33123
    Profile photo of tommymaynard
    tommymaynard
    Member

    Ha — yeah that may have an unexpected impact. You'd also need to change it in my example, Ed.

  • #33125
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Thanks again guys, I am going to run with this and appreciate all your help/advice

You must be logged in to reply to this topic.