Monday, 3 August 2020

powershell 10 error

PS C:\Users\bob\ps> Get-Item -path c:\nope -ErrorAction Stop; Write-Host 'hello'
Get-Item : Cannot find path 'C:\nope' because it does not exist.
At line:1 char:1
+ Get-Item -path c:\nope -ErrorAction Stop; Write-Host 'hello'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\nope:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

PS C:\Users\bob\ps> try {
    1/0
}
catch {
    write-host 'in catch'
    Write-Host $_
}
in catch
Attempted to divide by zero.

PS C:\Users\bob\ps> try {
    Get-Item -Path c:\nope
    Write-Host 'in try'
}
catch {
    write-host 'in catch'
    Write-Host $_
}
Get-Item : Cannot find path 'C:\nope' because it does not exist.
At line:2 char:5
+     Get-Item -Path c:\nope
+     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\nope:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

in try

PS C:\Users\bob\ps> try {
    Get-Item -Path c:\nope -ErrorAction Stop
    Write-Host 'in try'
}
catch {
    write-host 'in catch'
    Write-Host $_
}
in catch
Cannot find path 'C:\nope' because it does not exist.

PS C:\Users\bob\ps> $processName = @(
    'NotAProcess',
    'notepad++'
)

foreach ($item in $processName){
    try {
        Get-Process -Nam $item -erroraction stop
    }
    catch {
        Write-Host $_
    }
}
Cannot find a process with the name "NotAProcess". Verify the process name and call the cmdlet again.

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    316      21    14896      29524       0.53   7012   1 notepad++

PS C:\Users\bob\ps> try {
    $webResult = Invoke-WebRequest -uri 'https://chuanshoge2.blogspot.com/nope' -ErrorAction Stop     
}
catch {
    Write-Host 'error occured'
    $theError = $_
    if($theError.exception -like "*404*"){
        Write-Warning "web page not found"
    }
}
error occured
WARNING: web page not found

reference:
https://www.youtube.com/watch?v=A6afjA5Q9eM

No comments:

Post a Comment