![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Trapping WMI remoting errors and continuing on. Hello Steve, I would ping the machins first. --- Shay Levy Windows PowerShell MVP 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> 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> SS> Steve SS> |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | Re: Trapping WMI remoting errors and continuing on. On Aug 11, 9:41*pm, "Steve Schofield" <st...@xxxxxx> wrote: Quote: > 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. > your trap didn't catch the error is because even though it's a really long timeout that makes it seem like the script is bombing, it's actually just taking a really long time to time out and then returning with a non-terminating error. These errors don't actually throw exceptions, and to control the way your script behaves for these errors you'll want to use the ubiquitous -ea (errorAction) switch or the $ErrorActionPreference preference variable to force PowerShell to Continue or Stop or Inquire or ContinueSilently when it hits an error. In this particular case, though, it's not really all that helpful because you still have that really long timeout for every server that isn't online, so you have no choice but to ping it first like Shay suggested. |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Trapping WMI remoting errors and continuing on. Hi Shay, Thanks for the direction. I modified the one portion connecting and was able to query hundreds of boxes with no issues. Going from VBS error trapping (which is virtually nothing) to Powershell, it's a tad bit of an adjustment. Thanks, Steve "Shay Levy [MVP]" <no@xxxxxx> wrote in message news:89228ed23d1c78caca83ba416285@xxxxxx Quote: > 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| continuing etc. | Vista mail | |||
| continuing the process | Vista mail | |||
| Powershell 2.0 with remoting : ok what is Remoting ? | PowerShell | |||
| PSH Guru needed ! : Trapping terminating errors | PowerShell | |||