PoshCode Logo PowerShell Code Repository

Test-Server by JRICH 4 weeks ago (modification of post by Juan Carlos view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3183"></script>download | new post

A test that checks for hostname, ip, domain, ping, wsman (with credssp if you wish) remote reg RPC and RDP

  1. Function Test-Server{
  2. [cmdletBinding()]
  3. param(
  4.         [parameter(Mandatory=$true,ValueFromPipeline=$true)]
  5.         [string[]]$ComputerName,
  6.         [parameter(Mandatory=$false)]
  7.         [switch]$CredSSP,
  8.         [Management.Automation.PSCredential] $Credential)
  9.        
  10. begin{
  11.         $total = Get-Date
  12.         $results = @()
  13.         if($credssp){if(!($credential)){Write-Host "must supply Credentials with CredSSP test";break}}
  14. }
  15. process{
  16.     foreach($name in $computername)
  17.     {
  18.         $dt = $cdt= Get-Date
  19.         Write-verbose "Testing: $Name"
  20.         $failed = 0
  21.         try{
  22.         $DNSEntity = [Net.Dns]::GetHostEntry($name)
  23.         $domain = ($DNSEntity.hostname).replace("$name.","")
  24.         $ips = $DNSEntity.AddressList | %{$_.IPAddressToString}
  25.         }
  26.         catch
  27.         {
  28.                 $rst = "" |  select Name,IP,Domain,Ping,WSMAN,CredSSP,RemoteReg,RPC,RDP
  29.                 $rst.name = $name
  30.                 $results += $rst
  31.                 $failed = 1
  32.         }
  33.         Write-verbose "DNS:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  34.         if($failed -eq 0){
  35.         foreach($ip in $ips)
  36.         {
  37.            
  38.                 $rst = "" |  select Name,IP,Domain,Ping,WSMAN,CredSSP,RemoteReg,RPC,RDP
  39.             $rst.name = $name
  40.                 $rst.ip = $ip
  41.                 $rst.domain = $domain
  42.                 ####RDP Check (firewall may block rest so do before ping
  43.                 try{
  44.             $socket = New-Object Net.Sockets.TcpClient($name, 3389)
  45.                   if($socket -eq $null)
  46.                   {
  47.                          $rst.RDP = $false
  48.                   }
  49.                   else
  50.                   {
  51.                          $rst.RDP = $true
  52.                          $socket.close()
  53.                   }
  54.        }
  55.        catch
  56.        {
  57.             $rst.RDP = $false
  58.        }
  59.                 Write-verbose "RDP:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  60.         #########ping
  61.             if(test-connection $ip -count 1 -Quiet)
  62.             {
  63.                 Write-verbose "PING:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  64.                         $rst.ping = $true
  65.                         try{############wsman
  66.                                 Test-WSMan $ip | Out-Null
  67.                                 $rst.WSMAN = $true
  68.                                 }
  69.                         catch
  70.                                 {$rst.WSMAN = $false}
  71.                                 Write-verbose "WSMAN:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  72.                         if($rst.WSMAN -and $credssp) ########### credssp
  73.                         {
  74.                                 try{
  75.                                         Test-WSMan $ip -Authentication Credssp -Credential $cred
  76.                                         $rst.CredSSP = $true
  77.                                         }
  78.                                 catch
  79.                                         {$rst.CredSSP = $false}
  80.                                 Write-verbose "CredSSP:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  81.                         }
  82.                         try ########remote reg
  83.                         {
  84.                                 [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $ip) | Out-Null
  85.                                 $rst.remotereg = $true
  86.                         }
  87.                         catch
  88.                                 {$rst.remotereg = $false}
  89.                         Write-verbose "remote reg:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  90.                         try ######### wmi
  91.                         {      
  92.                                 $w = [wmi] ''
  93.                                 $w.psbase.options.timeout = 15000000
  94.                                 $w.path = "\\$Name\root\cimv2:Win32_ComputerSystem.Name='$Name'"
  95.                                 $w | select none | Out-Null
  96.                                 $rst.RPC = $true
  97.                         }
  98.                         catch
  99.                                 {$rst.rpc = $false}
  100.                         Write-verbose "WMI:  $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
  101.             }
  102.                 else
  103.                 {
  104.                         $rst.ping = $false
  105.                         $rst.wsman = $false
  106.                         $rst.credssp = $false
  107.                         $rst.remotereg = $false
  108.                         $rst.rpc = $false
  109.                 }
  110.                 $results += $rst       
  111.         }}
  112.         Write-Verbose "Time for $($Name): $((New-TimeSpan $cdt ($dt)).totalseconds)"
  113.         Write-Verbose "----------------------------"
  114. }
  115. }
  116. end{
  117.         Write-Verbose "Time for all: $((New-TimeSpan $total ($dt)).totalseconds)"
  118.         Write-Verbose "----------------------------"
  119. return $results
  120. }
  121. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me