Build/InstallHelpers.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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
function Install-MSI { Param ( [String]$MsiUrl, [String]$MsiName ) $exitCode = -1 try { Write-Host "Downloading $MsiName..." $FilePath = "${env:Temp}\$MsiName" Invoke-WebRequest -Uri $MsiUrl -OutFile $FilePath $Arguments = ('/i', $FilePath, '/QN', '/norestart' ) Write-Host "Starting Install $MsiName..." $process = Start-Process -FilePath msiexec.exe -ArgumentList $Arguments -Wait -PassThru $exitCode = $process.ExitCode if ($exitCode -eq 0 -or $exitCode -eq 3010) { Write-Host -Object 'Installation successful' return $exitCode } else { Write-Host -Object "Non zero exit code returned by the installation process : $exitCode." exit $exitCode } } catch { Write-Host -Object "Failed to install the MSI $MsiName" Write-Host -Object $_.Exception.Message exit -1 } } function Install-EXE { Param ( [String]$Url, [String]$Name, [String[]]$ArgumentList ) $exitCode = -1 try { Write-Host "Downloading $Name..." $FilePath = "${env:Temp}\$Name" Invoke-WebRequest -Uri $Url -OutFile $FilePath Write-Host "Starting Install $Name..." $process = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -Wait -PassThru $exitCode = $process.ExitCode if ($exitCode -eq 0 -or $exitCode -eq 3010) { Write-Host -Object 'Installation successful' return $exitCode } else { Write-Host -Object "Non zero exit code returned by the installation process : $exitCode." return $exitCode } } catch { Write-Host -Object "Failed to install the Executable $Name" Write-Host -Object $_.Exception.Message return -1 } } function Stop-SvcWithErrHandling { <# .DESCRIPTION Function for stopping the Windows Service with error handling .AUTHOR Andrey Mishechkin v-andmis@microsoft.com .PARAMETER -ServiceName The name of stopping service .PARAMETER -StopOnError Switch for stopping the script and exit from PowerShell if one service is absent #> param ( [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName, [Parameter()] [switch] $StopOnError ) Process { $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue if (-not $Service) { Write-Warning "[!] Service [$ServiceName] is not found"; if ($StopOnError) { exit 1; } } else { Write-Host "Try to stop service [$ServiceName]"; try { Stop-Service -Name $ServiceName -Force; $Service.WaitForStatus("Stopped", "00:01:00"); Write-Host "Service [$ServiceName] has been stopped successfuly"; } catch { Write-Error "[!] Failed to stop service [$ServiceName] with error:" $_ | Out-String | Write-Error; } } } } function Set-SvcWithErrHandling { <# .DESCRIPTION Function for setting the Windows Service parameter with error handling .AUTHOR Andrey Mishechkin v-andmis@microsoft.com .PARAMETER -ServiceName The name of stopping service .PARAMETER -Arguments Hashtable for service arguments #> param ( [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName, [Parameter(Mandatory)] [hashtable] $Arguments ) Process { $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue if (-not $Service) { Write-Warning "[!] Service [$ServiceName] is not found"; } try { Set-Service $ServiceName @Arguments; } catch { Write-Error "[!] Failed to set service [$ServiceName] arguments with error:" $_ | Out-String | Write-Error; } } } function Start-DownloadWithRetry { param ( [Parameter(Mandatory)] [string] $Url, [Parameter(Mandatory)] [string] $Name, [string] $DownloadPath = "${env:Temp}", [int] $Retries = 20 ) $FilePath = Join-Path -Path $DownloadPath -ChildPath $Name #Default retry logic for the package. while ($retries -gt 0) { try { Write-Host "Downloading package from: $Url to path $FilePath ." (New-Object System.Net.WebClient).DownloadFile($Url, $FilePath) break } catch { Write-Host "There is an error during package downloading:`n $_" $retries-- if ($retries -eq 0) { Write-Host "File can't be downloaded. Please try later or check that file exists by url: $Url" exit 1 } Write-Host "Waiting 30 seconds before retrying. Retries left: $retries" Start-Sleep -Seconds 30 } } return $FilePath } function Install-VsixExtension { Param ( [string] $Url, [Parameter(Mandatory = $true)] [string] $Name, [string] $FilePath, [Parameter(Mandatory = $true)] [string] $VSversion, [int] $retries = 20, [switch] $InstallOnly ) if (!$InstallOnly) { $FilePath = Start-DownloadWithRetry -Url $Url -Name $Name } $ArgumentList = ('/quiet', "`"$FilePath`"") Write-Host "Starting Install $Name..." try { #There are 2 types of packages at the moment - exe and vsix if ($Name -match "vsix") { $process = Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\$VSversion\Enterprise\Common7\IDE\VSIXInstaller.exe" -ArgumentList $ArgumentList -Wait -PassThru } else { $process = Start-Process -FilePath ${env:Temp}\$Name /Q -Wait -PassThru } } catch { Write-Host "There is an error during $Name installation" $_ exit 1 } $exitCode = $process.ExitCode if ($exitCode -eq 0 -or $exitCode -eq 1001) { # 1001 means the extension is already installed Write-Host "$Name installed successfully" } else { Write-Host "Unsuccessful exit code returned by the installation process: $exitCode." exit 1 } #Cleanup downloaded installation files if (!$InstallOnly) { Remove-Item -Force -Confirm:$false $FilePath } } function Get-VSExtensionVersion { param ( [string] [Parameter(Mandatory = $true)] $packageName ) $instanceFolders = Get-ChildItem -Path "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances" if ($instanceFolders -is [array]) { Write-Host "More than one instance installed" exit 1 } $stateContent = Get-Content -Path (Join-Path $instanceFolders.FullName '\state.packages.json') $state = $stateContent | ConvertFrom-Json $packageVersion = ($state.packages | Where-Object { $_.id -eq $packageName }).version if (!$packageVersion) { Write-Host "installed package $packageName for Visual Studio 2019 was not found" exit 1 } return $packageVersion } function Get-ToolcachePackages { $toolcachePath = Join-Path $env:ROOT_FOLDER "toolcache.json" Get-Content -Raw $toolcachePath | ConvertFrom-Json } function Get-ToolsByName { param ( [Parameter(Mandatory = $True)] [string]$SoftwareName ) (Get-ToolcachePackages).PSObject.Properties | Where-Object { $_.Name -match $SoftwareName } | ForEach-Object { $packageNameParts = $_.Name.Split("-") [PSCustomObject] @{ ToolName = $packageNameParts[1] Versions = $_.Value Architecture = $packageNameParts[3, 4] -join "-" } } } function Get-WinVersion { (Get-WmiObject -class Win32_OperatingSystem).Caption } function Test-IsWin19 { (Get-WinVersion) -match "2019" } function Test-IsWin16 { (Get-WinVersion) -match "2016" } |