Public/Utils/Test-Online.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function Test-Online {
  <#
      .Synopsis
      Checks if given names/IP addresses are available.

      .DESCRIPTION
      It uses WMI to test connectivity to given names/IP addresses.

      .EXAMPLE
      Test-Online 8.8.8.8

      Address : 8.8.8.8
      ProtocolAddress : 8.8.8.8
      ProtocolAddressResolved : google-public-dns-a.google.com
      ResponseTime : 27
      Timeout : 2000

      .EXAMPLE
      Test-Online someHost1,someHost2

      Address : someHost1
      ProtocolAddress : 10.2.6.49
      ProtocolAddressResolved : someHost1.contoso.com
      ResponseTime : 1
      Timeout : 2000

      Address : someHost2
      ProtocolAddress : 10.2.6.50
      ProtocolAddressResolved : someHost2.contoso.com
      ResponseTime : 0
      Timeout : 2000

      .EXAMPLE
      Test-Online someHost3,someHost4 -ResolveAddressNames:$false -Timeout 1000
      Address : someHost4
      ProtocolAddress : 10.2.6.91
      ProtocolAddressResolved :
      ResponseTime : 1
      Timeout : 1000

      Address : someHost3
      ProtocolAddress :
      ProtocolAddressResolved :
      ResponseTime :
      Timeout : 1000

      .EXAMPLE
      $comps = (get-adcomputer -filter *).where({$_.name -match 'someHost'}).name
      Test-Online $comps | format-table

      Address ProtocolAddress ProtocolAddressResolved ResponseTime Timeout
      ------- --------------- ----------------------- ------------ -------
      someHost4 10.2.6.91 someHost4.contoso.com 1 2000
      someHost2 2000
      someHost3 10.2.4.1 someHost3.contoso.com 3 2000
      someHostTEST0 2000
      someHost3
      2000
  #>


  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWMICmdlet', '')]
  [cmdletbinding()]
  [OutputType([PSObject])]
  Param(
    #Name/IP to check
    [Parameter(Mandatory=$true,Position=0)]
    [String[]]
    $Address,

    #Timeout
    [Parameter(Mandatory=$false)]
    [Int32]
    $Timeout = 2000,

    #Resolve IP to DNS names
    [Parameter(Mandatory=$false,HelpMessage="Resolve IP to DNS names")]
    [Boolean]
    $ResolveAddressNames = $true
  )
  begin{
    if($Address.Count -eq 1) {
      $filter = "Address=""$Address"" and Timeout=$Timeout and ResolveAddressNames=$ResolveAddressNames"
    }
    elseif ($Address.Count -gt 1) {
      $filter = 'Address="' + ($Address -join """ and Timeout=$Timeout and ResolveAddressNames=$ResolveAddressNames or Address=""") + """ and Timeout=$Timeout and ResolveAddressNames=$ResolveAddressNames"
    }
    if($filter) {
      Write-Log -Info -Message "Filter used is {$filter}"
    }
    else {
      Write-Log -Error  -Message 'Filter is empty. Names parsed incorrectly'
      break
    }
  }
  process{
    Get-WmiObject -Class Win32_PingStatus -Filter $filter |  Select-Object  -Property Address, ProtocolAddress,ProtocolAddressResolved, ResponseTime, Timeout
  }
  end{
  }
}