Last active
August 3, 2023 17:12
-
-
Save SteloNLD/5ad9fec0144ae4e0d0113de0b7cb626e to your computer and use it in GitHub Desktop.
Powershell Example: Try/Catch, ForEach, If/Else, Break/Continue, Write-Verbose/Warning/Host/Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clear-Host #Clearup the console. | |
### Script Output/Error Preferences. | |
#Enable/Disable Verbose Output for Debugging | |
$VerbosePreference = "Continue" #Use either Continue (Show) or SilentlyContinue (Hide) | |
#Stop Script Execution when an error occurs. | |
#Always use Stop!, if you need to bypass this behaviour for a specific command then use the -ErrorAction Parameter with SilentlyContinue for that command. | |
$ErrorActionPreference = "Stop" | |
### Script Settings | |
#Declare Services to Stop, To throw a warning make a typo. | |
$Services = @("WinRM", "W32Time") | |
#Get-Date -Format "0" outputs the time as a universal time stamp | |
Write-Verbose "Script Started at $(Get-Date -format "O") under user $($env:USERDOMAIN)\$($env:USERNAME) on system $($env:COMPUTERNAME)" | |
#Define $ServiceObjects as empty array in GlobalScope | |
$ServiceObjects = @() | |
# Service Retrieve Loop Start | |
Foreach ($Service in $Services) { | |
Write-Verbose "Retrieving Service $($Service)" | |
Try { | |
#Add the ServiceObject to the $ServiceObjects array. | |
$ServiceObjects += (Get-Service -Name $Service) | |
} | |
#To find the Exeption Name for a specific error examine the $Error variable after the error occured and note its place counting from 0 | |
#Then Use $Error[ErrorPosition].Exception.GetType().FullName to display the execption. | |
Catch [Microsoft.PowerShell.Commands.ServiceCommandException] { | |
#Some Exceptions are to generic, in this case you could use the Message Property of the Error Object to Narrow it down to a specific error. | |
If ($_.Exception.Message -like "Cannot find any service with service name *" ) { | |
Write-Warning "Service $($Service) does not exist, skipping." | |
Continue #ForEach Loop wil start proccesing the Next Item. | |
} | |
Else { | |
Write-Error -Message "Unknown Error, error message: $($Error[0].Exception.Message)" | |
Break #ForEach Loop wil end | |
} | |
} | |
Catch { | |
Write-Error -Message "Unknown Error, error message: $($Error[0].Exception.Message)" | |
Break #ForEach Loop wil end | |
} | |
#Only Runs when there is no Error (Break) or Warning (Continue) | |
Write-Verbose "Service $($Service) retrieved succesfully" | |
} # Service Retrieve Loop End | |
#Calculate the amount of Services found | |
$ServiceObjects_Measure = $ServiceObjects | Measure-Object | |
#Console Message. | |
Write-Host " | |
$($ServiceObjects_Measure.Count) Service(s) has been found, see table below:" | |
#Output the ServiceObjects to the Console | |
$ServiceObjects | Format-Table | Out-String | Write-Host | |
Write-Verbose "Script Stopped at $(Get-Date -format "O")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the following code if you want to output this in a HTML Mail Message or .HTML File for example.
$ServiceObjects_HTML = $ServiceObjects | Select-Object Status, Name, DisplayName | ConvertTo-Html -Fragment