DrixlRey
DrixlRey

Reputation: 245

How to do an action inside an If Else and continue? What's the proper error handling?

Using the below comment I made this script, I got close to my goal and it finally got to the catch. However, when it catches, it no longer has the $_.hostname pipeline, it's just blank.

$hostname = Import-Csv C:\Users\jackie.cheng\Desktop\TestComputers2.csv

$array = @()
$array2 = @()
$array3 = @()

$hostname | % {
    If (Test-Connection -ComputerName $_.hostname -count 1) 
    {
        try 
        {
            Write-Host "$($_.hostname) Invoke-Command executing, Test-Connection Sucessful"
            Test-Connection -ComputerName xxxxx -ErrorAction Stop
            
        }
        catch
        {
            Write-Host "$($_.hostname) Failed Invoke-Command, Test-Connection Successful"
            $array3 += [pscustomobject]@{
                Name = $_.hostname
                Status = "WMI Broken"}
        }
        finally
        {
            if ($Session = New-PSSession $($_.hostname))
            {
                Write-Host "$($_.hostname) Executing finally."
                $array += [pscustomobject]@{
                Name = $_.hostname
                Status = "Complete"}
            }
        }
    } 
    Else   
    {
        Write-Host "$($_.hostname) unreachable"
        $array2 += [pscustomobject]@{
            Name = $_.hostname
            Down = "Unreachable"
        }
    }
} 

$array | Export-Csv  'C:\Users\jackie.cheng\Desktop\SCCM Laptop Update Pushed.csv' -NoTypeInformation
$array2 | Export-Csv  'C:\Users\jackie.cheng\Desktop\Unreachable.csv' -NoTypeInformation
$array3 | Export-Csv  'C:\Users\jackie.cheng\Desktop\WMIBroken.csv' -NoTypeInformation

Upvotes: 0

Views: 66

Answers (1)

Abraham Zinala
Abraham Zinala

Reputation: 4694

You can use a Try{}, Catch{} block(s) for this. You'd just have to make Invoke-CimMethod terminate on an error using -ErrorAction 'Stop'. This "throws" the error into your catch block which would be represent by $_, but you can do anything else in it without the need for the error object.

$hostname | % {
    If (Test-Connection -ComputerName $_.hostname -count 1) 
    {
        try 
        {
            Invoke-CimMethod @MethodArgs -ErrorAction 'Stop'
            Invoke-Command -Session $Session -ScriptBlock {./RunFile.bat} -verbose

            $array += [pscustomobject]@{
                Name = $_.hostname
                Up = "Updated"
            }
        }
        catch
        {
            # do stuff here when Invoke-CimMethod fails
        }
        finally
        {
            if ($Session)
            {
                Remove-PSSession -Session $Session -ea 0
            }
        }
    } 
    Else   
    {
        Write-Host "$($_.hostname) offline"
        $array2 += [pscustomobject]@{
            Name = $_.hostname
            Down = "Unreachable"
        }
    }
} 

Also, wrapping your Invoke-Command .. in a scriptblock won't execute the command.

Upvotes: 2

Related Questions