Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 132
  • Last Modified:

Powershell Script Error Handling

I am trying to setup error handling in my powershell script and am unsure how to do it.

My powershell script disables one Transport Rule in Exchange and Enables a different Transport Rule. Once Rule has been enabled an email is sent to me (For privacy reasons I have changed email address and hostname)

I know I have to setup some kind of Try and Catch Variables so I can add Error Handling to Powershell script but unsure where to start.

Any help would be much appreciated. I have attached a copy of the powershell script in a txt file.

Thanks

TroyPowerShellScript.txt
0
Troy Graham
Asked:
Troy Graham
  • 3
  • 2
  • 2
1 Solution
 
Chamara WeliwattageCommented:
you have to do try and catch. Below is very basic example

Try{ <Command> -erroraction Stop
}

Catch{

Write-host $error.message
}
0
 
oBdACommented:
Like that, for example:
Try {
	<# Start a Powershell Session and Enable Transport Rule in Exchange 2010 #>
	$PSSession = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://exchsrv.exchange.com.au/PowerShell/ -Authentication Kerberos -ErrorAction Stop
	$WarningPreference = "SilentlyContinue"
	Import-PSSession $PSSession -ErrorAction Stop 2>&1 | Out-Null
	Enable-TransportRule "Australia Hours" -Confirm:$false -ErrorAction Stop
	Disable-TransportRule "NZ Hours" -Confirm:$false -ErrorAction Stop
	$EmailSubject = "You have just run the Transport rule to Enable Aus and Disable NZ"
	$Result = "Everything went fine."
} Catch {
	$EmailSubject = "There was an error trying to run the Transport rule to Enable Aus and Disable NZ"
	$Result = $_.Exception.Message
} Finally {
	<# Close Powershell Session #>
	Remove-PSSession $PSSession
}
<# Send email notification #>
$User = "anonymous"
$PWord = ConvertTo-SecureString –String "anonymous" –AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pword
Send-MailMessage -From "dummy@dummy.com.au" -To "dummy2@dummy.com.au" -Cc "dummy3@dummy.com.au" -Bcc "dummy4@dummy.com.au" `
	-SmtpServer "exchsrv.exchange.com.au" `
	-Subject $EmailSubject `
	-BodyAsHtml `
	-Credential $creds `
	-Body @"
Hi CSG,
<br>
<br>
You have just run the Transport rule to Enable Aus and Disable NZ.
<br>
Result: $($Result)
<br>
<br>
Kind Regards
<br>
Troy Graham
<br>
IT Support Administrator
<br>
joebloe@dummy.com.au
"@

Open in new window

0
 
Troy GrahamIT ManagerAuthor Commented:
Hi oBda,

That seems to work ok now but how can i get it to check if the Transport Rule is already enabled or not, and if it is throw off error.

e.g. If "Australia Hours" is already enabled, throw off error that says "Australia Hours is already enabled" and end script execution

Cheers
Troy
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
Chamara WeliwattageCommented:
Hi Troy,

This is a checking condition, not error handling so you have to do a "IF" before try loop. If "IF" condition is true, set your script to go through "try/catch/finally"
0
 
oBdACommented:
You query the transport rule and act accordingly.
Try {
	<# Start a Powershell Session and Enable Transport Rule in Exchange 2010 #>
	$PSSession = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://exchsrv.exchange.com.au/PowerShell/ -Authentication Kerberos -ErrorAction Stop
	$WarningPreference = "SilentlyContinue"
	Import-PSSession $PSSession -ErrorAction Stop 2>&1 | Out-Null
	If (Get-TransportRule "Australia Hours" -State Enabled -ErrorAction SilentlyContinue) {
		Throw "Transport Rule 'Australia Hours' is already enabled."
	}
	Enable-TransportRule "Australia Hours" -Confirm:$false -ErrorAction Stop
	Disable-TransportRule "NZ Hours" -Confirm:$false -ErrorAction Stop
	$EmailSubject = "You have just run the Transport rule to Enable Aus and Disable NZ"
	$Result = "Everything went fine."
} Catch {
	$EmailSubject = "There was an error trying to run the Transport rule to Enable Aus and Disable NZ"
	$Result = $_.Exception.Message
} Finally {
	<# Close Powershell Session #>
	Remove-PSSession $PSSession
}
<# Send email notification #>
$User = "anonymous"
$PWord = ConvertTo-SecureString –String "anonymous" –AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pword
Send-MailMessage -From "dummy@dummy.com.au" -To "dummy2@dummy.com.au" -Cc "dummy3@dummy.com.au" -Bcc "dummy4@dummy.com.au" `
	-SmtpServer "exchsrv.exchange.com.au" `
	-Subject $EmailSubject `
	-BodyAsHtml `
	-Credential $creds `
	-Body @"
Hi CSG,
<br>
<br>
You have just run the Transport rule to Enable Aus and Disable NZ.
<br>
Result: $($Result)
<br>
<br>
Kind Regards
<br>
Troy Graham
<br>
IT Support Administrator
<br>
joebloe@dummy.com.au
"@ 

Open in new window

0
 
Troy GrahamIT ManagerAuthor Commented:
Thanks a lot oBda. Your solution worked like a charm. :-)

Here is what I ended up with. I added a separate email for failure and a separate email for success.

Try {
	<# Start a Powershell Session and Enable Transport Rule in Exchange 2010 #>
	$PSSession = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://exchsrv.exchange.com.au/PowerShell/ -Authentication Kerberos -ErrorAction Stop
	$WarningPreference = "SilentlyContinue"
	Import-PSSession $PSSession -ErrorAction Stop 2>&1 | Out-Null
    If (Get-TransportRule "Testing" -State Enabled -ErrorAction SilentlyContinue) {
		Throw "Transport Rule Has already been enabled"
	}
	Enable-TransportRule "Testing" -Confirm:$false -ErrorAction Stop
	Disable-TransportRule "Testing 2" -Confirm:$false -ErrorAction Stop
	$EmailSubject = "SUCCESS: Transport rule to Enable Aus and Disable NZ"
	$Result = "Everything went fine."

<# Send email notification #>
$User = "anonymous"
$PWord = ConvertTo-SecureString –String "anonymous" –AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pword
Send-MailMessage -From "dummy@dummy.com.au" -To "dummy2@dummy.com.au" -Cc "dummy3@dummy.com.au" -Bcc "dummy4@dummy.com.au" `
	-SmtpServer "exchsrv.exchange.com.au" `
	-Subject $EmailSubject `
	-BodyAsHtml `
	-Credential $creds `
	-Body @"

<font face="Arial" color="#000000" size="2">Hi CSG,</font>
<br>
<br>
<font face="Arial" color="#000000" size="2">SUCCESS: Transport rule to Enable Aus and Disable NZ ran successfully</font>
<br>
<br>
</br>
<font face="Arial" color="#000000" size="2">Regards</font>
</br></br>
<font face="Arial" color="#000000" size="2" style="font-weight:bold">Troy Graham</font>
</br>
<font face="Arial" color="#000000" size="2">IT Support Administrator</font>
</br>
“@


} Catch {
	$EmailSubject = "FAILURE: Error running Transport rule to Enable Aus and Disable NZ"
	$Result = $_.Exception.Message

<# Send email notification #>
$User = "anonymous"
$PWord = ConvertTo-SecureString –String "anonymous" –AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pword
Send-MailMessage -From "notifications@eclgroup.com.au" -To "troy.graham@eclgroup.com.au" -Cc "troy.graham@eclgroup.com.au" -Bcc "troy.graham@eclgroup.com.au" `
	-SmtpServer "fq-exchsrv.eclfuelquip.com.au" `
	-Subject $EmailSubject `
	-BodyAsHtml `
	-Credential $creds `
	-Body @"

<font face="Arial" color="#000000" size="2">Hi CSG,</font>
<br>
<br>
<font face="Arial" color="#000000" size="2">FAILURE: Transport Rule to Enable Aus and Disable NZ didnt run</font>
<br>
<br>
</br>
<font face="Arial" color="#000000" size="2">Regards</font>
</br></br>
<font face="Arial" color="#000000" size="2" style="font-weight:bold">Troy Graham</font>
</br>
<font face="Arial" color="#000000" size="2">IT Support Administrator</font>
“@

} Finally {
	<# Close Powershell Session #>
	Remove-PSSession $PSSession
}

Open in new window

0
 
Troy GrahamIT ManagerAuthor Commented:
Thanks Again oBda. Your Solution worked like a charm. :-)
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now