PublicFunctions/Test-ServerConnectionOnPipeline.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
Function Test-ServerConnectionOnPipeline
{
    <#
        .Synopsis
            Runs availability checks on servers
        .DESCRIPTION
            This typically takes an imported csv file with a ComputerName Column as an imput object
            but just about any collection of objects that exposes .ComputerName should work
            The output is the same type of object as the input so that it can be piped
            to the next function to add another column.
            Makes both a WMI and PS Remote call

            This takes an object with .ComputerName as one of its properties because it is adding
            columns to that object and then passing it along to the next cmdlet. If the most
            convenient input is a list of stings such as from a text file or the output of an AD Call
            then use Get-ServerObjectCollection to create an object from the list.

            The data gathering cmdlets in this module assume by default that this cmdlet will be called
            and will not return information if the required protocol test fails. That is, if the server
            fails to ping, the cmdlet to get the OS version will not even try. Each cmdlet has a
            -NoError switch that can be used to omit the availability check
        .EXAMPLE
            Get-ServerCollection | Test-ServerConnectionOnPipeline | ft

            Will test network, WMI and PSRemote availability of each server in the collection.
        .EXAMPLE
            ('Server2','Server4') | Get-ServerObjectCollection | Test-ServerConnectionOnPipeline | ft

            See Get-Help Get-ServerObjectCollection -examples for more
        .EXAMPLE
            Get-ServerCollection | Get-TimeZoneOnPipeline -NoErrorCheck

            Gets the Timezone for all servers in the collection, but does not check the server
            availability first.
    #>


    [CmdletBinding()]

    Param (
        [parameter(
        Mandatory=$true,
        ValueFromPipeline=$true)]
        $ComputerProperties
    )
    Begin
        {}
    Process {
        $ComputerProperties | Select-Object *, Ping, WMI, PSRemote, BootTime | ForEach-Object {
            # Test Ping
            $PSItem.Ping = Test-Connection -ComputerName $ComputerProperties.ComputerName -Quiet -Count 1

            If ($PSItem.Ping) {
                # Calling WMI in a wrapper in order to isolate the error condition if it occurs
                $os = Get-WMI_OS -ComputerName $ComputerProperties.ComputerName
                # $os = Get-Wmiobject -ComputerName $ComputerProperties.ComputerName -Class Win32_OperatingSystem -ErrorAction Stop

                If ($null -ne $os)
                    {
                    # This conversion not necessare with ciminstance?
                    # $PSItem.BootTime = [Management.ManagementDateTimeConverter]::ToDateTime($os.LastBootUpTime)
                    $PSItem.BootTime = $os.LastBootUpTime
                    $PSItem.WMI = $true
                    }
                Else
                    {
                    $PSItem.WMI = $false
                    $PSItem.BootTime = 'No Try'
                    }

                # Test PS Remoting
                $ps = Get-PSRemoteComputerName -ComputerName $ComputerProperties.ComputerName
                # $Result = Invoke-Command -ComputerName $ComputerProperties.ComputerName -ScriptBlock {$env:COMPUTERNAME} -ErrorAction Stop

                If ($null -ne $ps)
                    {$PSItem.PSRemote = $true}
                Else
                    {$PSItem.PSRemote = $false}
            }
        $PSItem
        }
    }
}