(Solved) : Add Exception Handling Code Powershell D Apply Exception Handling Using Try Catch Systemou Q44048564 . . .
How to add exception handling in this code of PowerShell?
D. Apply exception handling using try-catch forSystem.OutOfMemoryException
Here is the current code:
# This forces the user to choose a value 1-5, a certain outputis returned depending on number choice
while($true)
{
$input= Read-Host “Enter the option 1-5”
if ($input -eq 5)
{
break
}
switch ( $input ){
# Gets current date and time
1 {
$temp= Get-Date
$answer = Get-ChildItem | Where-Object { $_.Name -match”[A-Z,a-z,0-9]*[.][l][o][g]” }
echo $temp $answer
echo $temp $answer >> DailyLog.txt
}
# Lists files
2 {
$answer = Get-ChildItem | Sort-Object -Property name | selectname
echo $answer
echo $answer >> C916contents.txt
}
# Lists current CPU processing percentage and memory usage
3 {
Get-Counter -Counter ‘Process(_total)% Processor Time’-MaxSamples 4 -SampleInterval 5
Get-Counter -Counter’desktop-l8dtt4rphysicaldisk(_total)current disk queue length’-MaxSamples 4 -SampleInterval 5
}
# Gets current running procces on PC
4 {
Get-Process | Sort-Object -Property cpu
}
}
}
Expert Answer
Answer to How to add exception handling in this code of PowerShell? D. Apply exception handling using try-catch for System.OutOfMe…
OR