Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Assistance with Capturing Catch Exceptions

$
0
0

Hello,

I am trying to capture the results of the Catch portion of my script.  I have tried a number of different ways, but have yet to be successful.  Here is what I have currently.  How do I accomplish this?

 

function LogMyError ($strErrorMessage) {

Add-Content -Value $strErrorMessage -path ($env:TEMP + "c:\ps\ErrorLog.csv") -Force

}

 

$computers = import-csv -path "c:\ps\AllServers_2.csv" -header "Name"

 

ForEach ($computer in $computers) {

if(test-connection $computer.name -count 1 -quiet) {

$bComputerInAD = $true

 

Try {

#Computer object found in AD

$objComputer = Get-ADComputer -Identity $computer.name -Properties Name,OperatingSystem,CanonicalName

}

Catch {

#Computer object not found in AD

$objComputer = $false

LogMyError -strErrorMessage ($_.Exception.Message) 

}

 

if($bComputerInAD) {

#write to standard file

Add-Content -Value ($objComputer.Name + "," + $objComputer.OperatingSystem + "," + $objComputer.CanonicalName) -Path "C:\ps\ActiveResults6.20.14.csv"

}

}

}

 

Thank you in advance.

Regards,

BrianSW


Viewing all articles
Browse latest Browse all 6937

Trending Articles