-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstall-MySoftware.ps1
60 lines (51 loc) · 2.43 KB
/
Install-MySoftware.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
# You must have Chocolately installed first
# Usage example
# Install-MySoftware -Computers dc1,dc2,svr12,svr16 -Packages 'powershell.portable','microsoft-edge','microsoft-windows-terminal'
Function Install-MySoftware {
[CmdletBinding()]
Param(
[Parameter()]
[string[]]$Computers,
[Parameter()]
[string[]]$Packages
)
$liveComputers = [Collections.ArrayList]@()
foreach ($computer in $computers) {
if (Test-Connection -ComputerName $computer -Quiet -count 1) {
$null = $liveComputers.Add($computer)
}
else {
Write-Verbose -Message ('{0} is unreachable' -f $computer) -Verbose
}
}
$liveComputers | ForEach-Object {
Invoke-Command -ComputerName $_ -ScriptBlock {
$Result = [Collections.ArrayList]@()
$testchoco = Test-Path 'C:\ProgramData\chocolatey'
if ($testchoco -eq $false) {
Write-Verbose -Message "Chocolately is not installed on $($Env:COMPUTERNAME). Please install and try again." -Verbose
}
# TLS 1.2 is required for Chocolatey to install
# https://chocolatey.org/blog/remove-support-for-old-tls-versions
# Check if using TLS 1.2
$tls = [System.Net.ServicePointManager]::SecurityProtocol.HasFlag([Net.SecurityProtocolType]::Tls12)
if ($tls -eq $false) {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}
foreach ($Package in $using:Packages) {
choco Install $Package -y | Out-File -FilePath "c:\Windows\Temp\choco-$Package.txt"
if ($LASTEXITCODE -eq '1641' -or '3010') {
# Reference: https://chocolatey.org/docs/commandsinstall#exit-codes
# create new custom object to keep adding information to it
$Result += New-Object -TypeName PSCustomObject -Property @{
ComputerName = $Env:COMPUTERNAME
InstalledPackage = $Package
}
}
else {
Write-Verbose -Message "Packages failed on $($Env:COMPUTERNAME), see logs in c:\windows\temp" -Verbose
}
} $Result
}
} | Select-Object ComputerName, InstalledPackage | Sort-Object -Property ComputerName
}