UNCLASSIFIED

Skip to content
Snippets Groups Projects
Commit fc02a82b authored by Joshua Parsons's avatar Joshua Parsons
Browse files

gensite

parent e78b4bea
No related merge requests found
Pipeline #118085 passed with stages
in 11 seconds
......@@ -130,22 +130,19 @@ Error handling is important when creating PowerShell scripts. A script that runs
.Using Try, Catch, Finally
[source,powershell]
----
# Spelling and formatting errors can be common in larger script blocks, since Get-Process is specified as Get-Processes, the first Catch will fire when the code is executed
# Spelling and formatting errors can be common in larger script blocks, but other issues unrelated to user error can generate errors. The below example showcases an access restriction error.
PS C:\> Try {
Get-Processes
}
Catch [System.Management.Automation.CommandNotFoundException] {
Write-Host "CATCH 1: The command is not found!" -ForegroundColor Red
}
Catch {
Write-Host "CATCH 2: This is a catch all by default" -ForegroundColor Yellow
}
Finally {
Write-Host "FINALLY: Runs no matter what..." -ForegroundColor Yellow
}
CATCH 1: The command is not found!
FINALLY: Runs no matter what...
Get-ChildItem "c:\windows\system32\com\dmp"
}
Catch [System.UnauthorizedAccessException] {
Write-Host "You don't have access to the file" -ForegroundColor Red
}
Catch {
Write-Host "Something else weird happened" -ForegroundColor Yellow
}
Finally {
Write-Host "Regardless of whether or not there was an error, we still execute this" -ForegroundColor Green
}
# PRODUCE_ERROR will cause an error during code execution because it is not a valid computer name, but it will still return the BIOS Name property since we specify -ErrorAction Continue
# and the computer name for localhost is a valid property value for the Get-CimInstance Win32_BIOS -ComputerName parameter.
......@@ -161,7 +158,7 @@ PS C:\> Try {
Write-Host "CATCH 2: This is a catch all by default" -ForegroundColor Yellow
}
Finally {
Write-Host "FINALLY: Runs no matter what..." -ForegroundColor Yellow
Write-Host "FINALLY: Runs no matter what..." -ForegroundColor Green
}
Get-CimInstance : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not
......@@ -203,35 +200,29 @@ PS C:\> Try {
** The Trap statement can also be used to handle terminating errors in scripts
** The Trap keyword specifies statements to run when a terminating error occurs
** Trap statements handle the terminating errors and allow execution to continue
** Continue can be used with Trap statements to prevent the error message from being displayed
.Using a Trap Statement function
[source,powershell]
----
# Once again Get-Process is misspelled as Get-Processes and will cause an error at execution, the Trap Statement will trap the error and allow the remaining code to run successfully
# Get-Process is misspelled as Get-Processes and will cause an error at execution, the Trap Statement will trap the error and allow the remaining code to run successfully
PS C:\> Function Trap-Test {
Trap [System.Management.Automation.CommandNotFoundException] {
Write-Host "TRAP ERROR: The command is not found, check your spelling!" -ForegroundColor Red
continue
}
Trap {
Write-Host "TRAP ERROR: This is a catch all by default" -ForegroundColor Yellow
}
Get-Processes
$BiosName = (Get-CimInstance Win32_BIOS -ComputerName localhost).Name
Write-Host $BiosName -ForegroundColor Green
Write-Host "Bios Name: $BiosName" -ForegroundColor Green
}
PS C:\> Trap-Test
TRAP ERROR: The command is not found, check your spelling!
Get-Processes : The term 'Get-Processes' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:8 char:13
+ Get-Processes
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Processes:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Default System BIOS
Bios Name: 1301
----
=== Error Reporting
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment