functions/Invoke-BoxStarterRemoteUpgrade.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
<#
.SYNOPSIS
    Uses Install-Boxstarterpackage to install packages remotely. In addition, provides ability to deploy new packages and exclude packages.
.DESCRIPTION
    Long description
.EXAMPLE
    Here, we upgrade any out of date packages on winclient2, push out curl and git packages and exclude jre8 from updating.
    Each of these commands is created dynamically at runtime on a text file on the local machine called Boxstarterupgrade.txt
 
    Invoke-BoxStarterRemoteUpgrade -ComputerName winclient2 -Credential $DomainCred -AdditionalPackages curl,git -ExcludedPackages jre8 -ScriptPath C:\Windows\Temp\BoxstarterUpgrade.txt
.EXAMPLE
     Here we use the -Parallel switch so that each remote machine is processed at the same time.
 
      Invoke-BoxStarterRemoteUpgrade -ComputerName winclient2 -Credential $DomainCred -AdditionalPackages curl,git -Parallel -ScriptPath C:\Windows\Temp\BoxstarterUpgrade.txt
#>

function Invoke-BoxStarterRemoteUpgrade {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string[]]$ComputerName,
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential,
        [string[]]$AdditionalPackages,
        [string[]]$ExcludedPackages,
        [Parameter(Mandatory=$true)]
        [string]$ScriptPath,
        [switch]$Parallel
    )

    Invoke-Command -ArgumentList $AdditionalPackages,$ExcludedPackages,$ScriptPath -ComputerName $ComputerName -ScriptBlock {
        param (
            $AdditionalPackages,
            $ExcludedPackages,
            $ScriptPath
        )
        if (Test-Path $ScriptPath) {
            Remove-Item $ScriptPath -Force
        }
            $packages = [System.Collections.ArrayList]@(choco outdated -r --ignore-unfound --ignore-pinned  | Foreach-Object {
                ($_.split("|"))[0]
            })
        foreach ($AddedPackage in $AdditionalPackages){
            if ($packages -notcontains $AddedPackage){
                $packages.Add($AddedPackage) | Out-Null
            }
        }
        foreach ($ExcludedPackage in $ExcludedPackages){
            if ($packages -contains $ExcludedPackage){
                $packages.Remove($ExcludedPackage) | Out-Null
            }
        }
        $Packages | ForEach-Object {
            Add-Content $ScriptPath -Value "choco upgrade $_ -r -y"
        }
    }
    if (!$Parallel){
        Install-BoxstarterPackage -ComputerName $ComputerName -PackageName $ScriptPath -DelegateChocoSources
    }
    else {
        $ComputerName | ForEach-Object {
              start-process -RedirectStandardOutput C:\Windows\Temp\$_.txt -FilePath powershell -ArgumentList "-windowstyle hidden Install-BoxstarterPackage -ComputerName $_ -PackageName $ScriptPath" -PassThru
            }
    }
}