View Single Post
Old 08-12-2008   #3 (permalink)
Shay Levy [MVP]


 
 

Re: Trapping WMI remoting errors and continuing on.

Check this:

http://bsonposh.com/archives/311




---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

SL> Hello Steve,
SL>
SL> I would ping the machins first.
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> http://blogs.microsoft.co.il/blogs/ScriptFanatic
SS>> I'm trying to write a powershell script that does the following
SS>>
SS>> 1) Selects from a database of computers (name of computer and IP
SS>> address is
SS>> in the table)
SS>> 2) Loops through the results querying the
SS>> Win32_NetworkAdapterConfiguration
SS>> per machine.
SS>> 3) Logs information to the stringbuilder variable
SS>> 4) Logs errors if a machine isn't available.
SS>> I've tried a couple examples on Try/Catch and TRAP exception. The
SS>> powershell script bombs on a machine not available.
SS>> Here is the script. I can do this in VBS, but I wanted to see if
SS>> it's possible in Powershell 1.0.
SS>>
SS>> Hope someone can point me in the right direction when it comes to
SS>> error trapping.
SS>>
SS>> # ********************* Global variables *********************
SS>> $sb = new-object System.Text.StringBuilder
SS>> $sbErrors = new-object System.Text.StringBuilder
SS>> # ********************* Defining functions *********************
SS>> function GetListOfComputer
SS>> {
SS>> $cn = new-object system.data.SqlClient.SqlConnection("Data
SS>> Source=mydbserver;Password=P@xxxxxx;Persist Security Info=True;User
SS>> ID=User;Initial Catalog=DBServer");
SS>> $ds = new-object "System.Data.DataSet" "dsPersonData"
SS>> $q = "select ServerName, IPAddress from myTable"
SS>> $da = new-object "System.Data.SqlClient.SqlDataAdapter" ($q, $cn)
SS>> $da.Fill($ds)
SS>> $dtPerson = new-object "System.Data.DataTable" "dtPersonData"
SS>> $dtPerson = $ds.Tables[0]
SS>> $dtPerson | FOREACH-OBJECT {LogInfo $_.ServerName $_.PrimaryIP}
SS>> }
SS>> function LogError ([string]$strError)
SS>> {
SS>> $sbErrors.Append($strError)
SS>> }
SS>> function LogInfo
SS>> ([string]$strServerFriendlyName,[string]$strIPAddress)
SS>> {
SS>> # &{#Try
SS>> # Write-Host $strServerFriendlyName + "," + $strIPAddress
SS>> # $colItems = get-wmiobject -query "Select * From
SS>> Win32_NetworkAdapterConfiguration Where IPEnabled = 1" -namespace
SS>> "root\cimv2" -computername $strIPAddress
SS>> # trap
SS>> # $sb.Append($strServerFriendlyName + "," + $strIPAddress + "," +
SS>> $_.Caption + "," + $_.Description + "," + $_.DNSServerSearchOrder +
SS>> "," +
SS>> $_.MACAddress)
SS>> # }
SS>> # #CATCH
SS>> # trap [Exception]
SS>> # {
SS>> # Write-Host $strServerFriendlyName + "," + $strIPAddress
SS>> # LogError $strServerFriendlyName + "," + $strIPAddress
SS>> # continue # So that the "Finally" stuff gets executed
SS>> # }
SS>> trap [Exception] {continue}
SS>> Write-Host $strServerFriendlyName + "," + $strIPAddress
SS>> $colItems = get-wmiobject -query "Select * From
SS>> Win32_NetworkAdapterConfiguration Where IPEnabled = 1" -namespace
SS>> "root\cimv2" -computername $strIPAddress
SS>> $sb.Append($strServerFriendlyName + "," + $strIPAddress + "," +
SS>> $_.Caption
SS>> + "," + $_.Description + "," + $_.DNSServerSearchOrder + "," +
SS>> $_.MACAddress)
SS>> }
SS>> # ********************* Get list of Computers and call function to
SS>> get data
SS>> *********************
SS>> GetListOfComputer
SS>> # ********************* Write out data *********************
SS>> $sb.ToString() > st1.txt
SS>> $sbErrors.ToString() > st2.txt
SS>> Thanks,
SS>> Steve
SS>>


My System SpecsSystem Spec