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