functions/Test-AWPPreReq.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
function Test-AWPPreReq
{
    <#
        .Synopsis
            Checks a computer has the Pre-Reqs to install the Autotask WorkPlace Agent.
        .DESCRIPTION
            Checks a computer has the Pre-Reqs to install the Autotask WorkPlace Agent. Looks at Windows Version and .Net framework version.
        .EXAMPLE
            Test-AutoTaskWorkPlacePreReq -computerName 'computerOne', 'computerTwo'
        .EXAMPLE
            'computerOne', 'computerTwo' | Test-AutoTaskWorkPlacePreReq -credential Get-Credential
        .NOTES
            Written by Ben Taylor
            Version 1.0, 17.11.2016
 
            Maybe should have been a OVT pester test
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('AWPPreReq')]
    Param
    (
        [Parameter(Mandatory=$false, 
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNullOrEmpty()]
        $computerName = $env:COMPUTERNAME,
        [Parameter(Mandatory = $false, Position = 1)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential
    )

    Begin
    {
        $cimCommonParams = @{
            ErrorAction = 'Stop'
        }

        If ($PSBoundParameters['Credential'])
        {
            $cimCommonParams.Credential = $Credential
        }
    }
    Process
    {
        foreach($computer in $computerName)
        {
            try
            {
                $cimCommonParams.ComputerName = $computer

                $dotNetRelease = Invoke-Command @cimCommonParams -scriptBlock {
                    Get-ItemProperty -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full'
                }
                
                Write-Verbose 'Getting connection protocol'
                try
                {
                    Test-WSMan @cimCommonParams | Out-Null
                    $ProtocolAuto = 'WsMan'
                }
                catch
                {
                    $ProtocolAuto = 'DCOM'
                }

                $cimSession = New-CimSession @cimCommonParams -SessionOption (New-CimSessionOption -Protocol $ProtocolAuto)      
                $osVersion = Get-CimInstance Win32_OperatingSystem -CimSession $cimSession -ErrorAction Stop | Select-Object -ExpandProperty Version

                [AWPPreReq]::new($computer, ($dotNetRelease | Select-Object -ExpandProperty Release), $osVersion)
            }
            catch
            {
                Write-Error "$computer - $_"
            }
            finally
            {
                if($null -ne $cimSession)
                {
                    Remove-CimSession $cimSession -ErrorAction SilentlyContinue
                }

            }
        }
    }
}