functions/Install-AWP.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function Install-AWP
{
    <#
        .Synopsis
            Installs the SoonR Agent on remote and local computers
        .DESCRIPTION
            Installs the SoonR Agent on remote and local computers. Assumes C: is the system drive.
        .EXAMPLE
            Install-AWP -computername 'computer-one' -path 'c:\install\soonRInstaller.exe'
        .EXAMPLE
            'computer-one', 'computer-two' | Install-AWP -path 'c:\install\soonRInstaller.exe'
        .NOTES
            Written by Ben Taylor
            Version 1.0, 13.11.2016
    #>

    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')]
    [OutputType()]
    Param
    (
        [Parameter(Mandatory=$false, 
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $computerName = $env:computername,
        [Parameter(Mandatory=$true, Position=1)]
        [ValidateScript({ Test-Path $_ })]
        [string]
        $path,
        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential
    )

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

        If ($PSBoundParameters['Credential'])
        {
            $commonSessionParams.Credential = $Credential
        }
    }
    Process
    {
        forEach ($computer in $computerName)
        {
            try
            {
                if((Test-AWPInstall -ComputerName $computer @commonSessionParams).isInstalled -eq $false)
                {
                    Write-Verbose "[$(Get-Date -Format G)] - $computer - AWP not installed so trying to install"

                    if ($pscmdlet.ShouldProcess($computer, 'Install-AWP'))
                    {
                        Write-Verbose "[$(Get-Date -Format G)] - $computer - Creating remote session"

                        $psSession = New-PSSession -ComputerName $computer @commonSessionParams

                        $remoteInstallerpath = Invoke-Command -Session $psSession -ScriptBlock {
                            Join-Path ([environment]::GetEnvironmentVariable('temp','machine')) soonRAgent.exe
                        } -ErrorAction Stop 

                        Write-Verbose "[$(Get-Date -Format G)] - $computer - Copying Installer"
                        Copy-Item -Path $path -Destination ($remoteInstallerpath -replace [regex]::Escape('c:\'), "\\$computer\C$\") -Force @commonSessionParams

                        Write-Verbose "[$(Get-Date -Format G)] - $computer - Installing SoonR agent"

                        Invoke-Command -Session $psSession -ScriptBlock {
                            $args = @(
                                        '/install'
                                        '/quiet'
                                        '/norestart'
                            )

                            Start-Process $USING:remoteInstallerpath -ArgumentList $args -Wait -Verbose
                        } -ErrorAction Stop 
                    }
                }
                else
                {
                    Write-Verbose "[$(Get-Date -Format G)] - $computer - AWP allready installed. Skipping on this computer"
                }
            }
            catch
            {
                Write-Error $_
            }
            finally
            {
                Write-Verbose "[$(Get-Date -Format G)] - $computer - Tidying up install files\sessions if needed"

                if($null -ne $psSession)
                {
                    try
                    {
                        Invoke-Command -Session $pssession -ScriptBlock {
                            # Check if file exists and if so remove
                            if(Test-Path $USING:remoteInstallerpath)
                            {
                                Remove-Item $USING:remoteInstallerpath -force -Confirm:$false -WhatIf:$false
                            }
                        } -ErrorAction Stop
                    }
                    catch
                    {
                        Write-Error $_
                    }

                    Remove-PSSession $psSession -Confirm:$false -WhatIf:$false
                }
            }
        }
    }
}