functions/Test-AWPInstall.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
function Test-AWPInstall
{
    <#
        .Synopsis
            Checks a computer to see if the Autotask WorkPlace Agent is installed.
        .DESCRIPTION
            Checks a computer to see if the Autotask WorkPlace Agent is installed. It looks for the uninstall registry key.
        .EXAMPLE
            Test-AWPInstall -computerName 'computerOne', 'computerTwo'
        .EXAMPLE
            'computerOne', 'computerTwo' | Test-AWPInstall -credential Get-Credential
        .NOTES
            Written by Ben Taylor
            Version 1.0, 17.11.2016
 
            Maybe should have been a OVT pester test
    #>

    [CmdletBinding()]
    [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
    {
        $sessionCommonParams = @{
            ErrorAction = 'Stop'
        }

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

                $soonRAgent = Invoke-Command @sessionCommonParams -scriptBlock {
                    Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.displayName -eq 'Soonr Desktop Agent' -and $_.Installed }
                }

                [AWPInstall]::new($computer, $soonRAgent.UninstallString , $soonRAgent.DisplayVersion)
            }
            catch
            {
                Write-Error "$computer - $_"
            }
        }
    }
}