Close. Foreach($comp in get-qadcomputer){ $srvname = $comp.name if((test-port $srvname -verbose)){ $key = "SOFTWARE\Microsoft\Windows\CurrentVersion\windowsupdate" blah, blah, blah
Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com -----Original Message----- From: Bill Egan [mailto:william.e...@gmail.com] Sent: Friday, April 16, 2010 4:53 PM To: MS-Exchange Admin Issues Subject: Re: powershell/.NET question ok, further showing my ignorance, how do I call that function. I'm trying: Foreach($comp in get-qadcomputer){ $srvname = $comp.name if(test-port $srvname=$true){ $key = "SOFTWARE\Microsoft\Windows\CurrentVersion\windowsupdate" blah, blah, blah and it does not seem to be working, though i am not getting errors... thanks, bill On Fri, Apr 16, 2010 at 3:56 PM, Campbell, Rob <rob_campb...@centraltechnology.net> wrote: > This may help with testing for RPC availability. > > function Test-Port{ > Param([string]$srv,$port=135,$timeout=1000,[switch]$verbose) > $ErrorActionPreference = "SilentlyContinue" > $tcpclient = new-Object system.Net.Sockets.TcpClient > $iar = $tcpclient.BeginConnect($srv,$port,$null,$null) > $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false) > if(!$wait) > { > $tcpclient.Close() > if($verbose){Write-Host "Connection Timeout"} > Return $false > } > else > { > $error.Clear() > $tcpclient.EndConnect($iar) | out-Null > if($error[0]){if($verbose){write-host $error[0]};$failed = > $true} > $tcpclient.Close() > } > if($failed){return $false}else{return $true} } > > -----Original Message----- > From: Michael B. Smith [mailto:mich...@smithcons.com] > Sent: Friday, April 16, 2010 2:51 PM > To: MS-Exchange Admin Issues > Subject: RE: powershell/.NET question > > Well....I don't think you can actually do that. But there are actually two > potential issues. One you've identified and I'll give you a script to help > with that. The other is that a firewall is running and blocking RPC. I can't > much help you with that one, except to tell you that it can be controlled via > group policy. Here is how you execute a ping in PowerShell to alleviate the > first issue. (You'll need to remove the "log" statements or substitute your > own logging infrastructure.) This works in v1 or v2 of PowerShell. > > function test-ping ([string]$server) > { > [string]$routine = "test-ping:" > > trap > { > # we should only get here if the New-Object fails. > > log $routine "Cannot create System.Net.NetworkInformation.Ping > for $server." > return $false > } > > $ping = New-Object System.Net.NetworkInformation.Ping > if ($ping) > { > trap > [System.Management.Automation.MethodInvocationException] > { > log $routine "Invalid hostname specified (cannot > resolve $server)." > return $false > } > > for ($i = 0; $i -lt 5; $i++) > { > $rslt = $ping.Send($server) > if ($rslt -and ($rslt.Status -eq > [System.Net.NetworkInformation.IPStatus]::Success)) > { > log $routine "Can ping $server. $routine > successful on attempt $i." > $ping = $null > return $true > } > sleep -seconds 1 > } > $ping = $null > } > > log $routine "Cannot ping $server. $routine failed after 5 attempts." > return $false > } > > Regards, > > Michael B. Smith > Consultant and Exchange MVP > http://TheEssentialExchange.com > > -----Original Message----- > From: Bill Egan [mailto:william.e...@gmail.com] > Sent: Friday, April 16, 2010 3:30 PM > To: MS-Exchange Admin Issues > Subject: OT: powershell/.NET question > > Sorry for the off topic post, but since many of you are powershell whizzes, I > thought I'd give it a try. And forgive my inefficient code... > > So I am trying to put together a script to get the value of a registry > key for every computer in the domain - this case the WSUS susclientid > - and have come up with this (after plagiarizing the connection to remote > registry with the .NET object): > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > $erroractionpreference = "SilentlyContinue" > Write-Host "Server Value" > Write-Host > "------------------------------------------------------------------" > > Foreach($srv in get-qadcomputer){ > > $srvname = $srv.name > $key = "SOFTWARE\Microsoft\Windows\CurrentVersion\windowsupdate" > $type = [Microsoft.Win32.RegistryHive]::LocalMachine > $regKey = > [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $srvname) > $regKey = $regKey.OpenSubKey($key) > $val = $regKey.GetValue("susclientid") > Write-host $srvname.PadRight(30) -nonewline > Write-Host $val > $val = "-----" > } > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > And it sort of works. I think I have a few things to deal with in error > handling and output format, but debugging and figuring that out is quite > difficult since the script takes *hours* to run. I think the problem is that > of the 725 computer objects in this domain, many of them are offline and the > .NET object needs to timeout on each system that it cannot reach. And the > timeout period appears to be ~45 seconds. > > Would anyone be able to tell me how I can set the timeout of the > [Microsoft.Win32.RegistryKey] connection down to like 5 seconds? > > thanks and sorry again for the off topic question. > > b > > > > > ********************************************************************** > **************************** > Note: > The information contained in this message may be privileged and > confidential and protected from disclosure. If the reader of this > message is not the intended recipient, or an employee or agent > responsible for delivering this message to the intended recipient, you > are hereby notified that any dissemination, distribution or copying of > this communication is strictly prohibited. If you have received this > communication in error, please notify us immediately by replying to the > message and deleting it from your computer. > ********************************************************************** > **************************** > > > > >