miticoluis
miticoluis

Reputation: 541

force powershell script to stop and retrieve info

I've got an script that lists files located under C:\ on a list of remote servers:

$Servers=Get-ADComputer -SearchBase $TerminalServersOU -Filter '*' | Sort-Object Name | Select -Exp Name

foreach ($Server in $Servers) {
$Server

#Check first if the terminal server is on   
if (Test-Connection $Server -quiet) { 

    Invoke-Command -ComputerName $Server { 
       $output=dir C:\
    }
}

else {
        "`n$Server is disconnected"
    }

The script works perfectly fine on all servers except for one called "UKBTH05CTX03". When the script runs on this server, it crashes and never finishes:

UKBTH05CTX01

UKBTH05CTX01 is disconnected
UKBTH05CTX02


Directory: C:\


Mode                LastWriteTime     Length Name                                          PSComputerName
----                -------------     ------ ----                                         --------------
d----        11/09/2014     23:00            inetpub                                  ukbth05ctx02
d----        11/09/2014     23:00            log                                       ukbth05ctx02
d----        14/07/2009     04:20            PerfLogs                                  ukbth05ctx02
d-r--        16/07/2015     15:15            Program Files                             ukbth05ctx02
d-r--        15/09/2015     13:01            Program Files (x86)                   ukbth05ctx02

UKBTH05CTX03
Provider load failure
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject],     ManagementException
    + FullyQualifiedErrorId :     GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Provider load failure
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject],     ManagementException
    + FullyQualifiedErrorId :     GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

d----        05/10/2014     15:18            inetpub                                  ukbth05ctx03
Provider load failure
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject],    ManagementException
    + FullyQualifiedErrorId : Get   WMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I need to be able to show a message like "the script crashed on this server" and finish its execution. It is very important for me to finish its execution and know why it crashed. I don't know how to do it in PowerShell so I am looking for advise.

Thanks to all.

Upvotes: 0

Views: 904

Answers (2)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

Pretty sure Test-Connection uses wmi to get Win32_PingStatus. Since you see lots of wmi errors, expand a Try...Catch to include that too.

If all else fails, set $ErrorActionPreference = 'SilentlyContinue' right at the start, and you might at least get some visibility on what is going on to help isolate the issue.

Upvotes: 0

steoleary
steoleary

Reputation: 9298

You should use a try/catch block to trap the error, something like this:

$Servers=Get-ADComputer -SearchBase $TerminalServersOU -Filter '*' | Sort-Object Name | Select -Exp Name

foreach ($Server in $Servers) {
$Server

#Check first if the terminal server is on   
if (Test-Connection $Server -quiet) { 

    try 
    {
        Invoke-Command -ComputerName $Server { 
           $output=dir C:\
        }
    }
    catch
    {
        Write-Host "the script crashed on server: $server"
    }
}

else {
        "`n$Server is disconnected"
    }

You could get more advanced error handling and actually return back the error received as well if needed, have a look here for more info:

Try Catch Finally and error handling in PowerShell

Upvotes: 1

Related Questions