Skip to content

Commit a97b417

Browse files
authored
Make Sandbox script work in PowerShell 5 (#247924)
1 parent c6246d9 commit a97b417

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

Tools/SandboxTest.ps1

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ enum DependencySources {
4848
$ProgressPreference = 'SilentlyContinue'
4949
$ErrorActionPreference = 'Stop' # This gets overridden most places, but is set explicitly here to help catch errors
5050
if ($PSBoundParameters.Keys -notcontains 'InformationAction') { $InformationPreference = 'Continue' } # If the user didn't explicitly set an InformationAction, Override their preference
51-
$script:OnMappedFolderWarning = ($PSBoundParameters.Keys -contains 'WarningAction') ? $PSBoundParameters.WarningAction : 'Inquire'
51+
if ($PSBoundParameters.Keys -contains 'WarningAction') {
52+
$script:OnMappedFolderWarning = $PSBoundParameters.WarningAction
53+
} else {
54+
$script:OnMappedFolderWarning = 'Inquire'
55+
}
5256
$script:UseNuGetForMicrosoftUIXaml = $false
5357
$script:ScriptName = 'SandboxTest'
5458
$script:AppInstallerPFN = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe'
@@ -75,7 +79,7 @@ $script:UiLibsHash_v2_8 = '249D2AFB41CC009494841372BD6DD2DF46F87386D535DDF8D9F32
7579
$script:UiLibsHash_NuGet = '6B62BD3C277F55518C3738121B77585AC5E171C154936EC58D87268BBAE91736'
7680

7781
# File Paths
78-
$script:AppInstallerDataFolder = Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Packages' -AdditionalChildPath $script:AppInstallerPFN
82+
$script:AppInstallerDataFolder = Join-Path -Path (Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Packages') -ChildPath $script:AppInstallerPFN
7983
$script:TokenValidationCache = Join-Path -Path $script:AppInstallerDataFolder -ChildPath 'TokenValidationCache'
8084
$script:DependenciesCacheFolder = Join-Path -Path $script:AppInstallerDataFolder -ChildPath "$script:ScriptName.Dependencies"
8185
$script:TestDataFolder = Join-Path -Path $script:AppInstallerDataFolder -ChildPath $script:ScriptName
@@ -91,6 +95,9 @@ $script:HostGeoID = (Get-WinHomeLocation).GeoID
9195

9296
# Misc
9397
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
98+
99+
# Ensure the System.Net.Http assembly is loaded
100+
Add-Type -AssemblyName System.Net.Http
94101
$script:HttpClient = New-Object System.Net.Http.HttpClient
95102
$script:CleanupPaths = @()
96103

@@ -212,14 +219,27 @@ function Get-RemoteContent {
212219
)
213220
Write-Debug "Attempting to fetch content from $URL"
214221
# Check if the URL is valid before trying to download
215-
$response = [String]::IsNullOrWhiteSpace($URL) ? @{StatusCode = 400 } : $(Invoke-WebRequest -Uri $URL -Method Head -ErrorAction SilentlyContinue) # If the URL is null, return a status code of 400
222+
# If the URL is null, return a status code of 400
223+
if ([String]::IsNullOrWhiteSpace($URL)) {
224+
$response = @{ StatusCode = 400 }
225+
} else {
226+
$response = Invoke-WebRequest -Uri $URL -Method Head -ErrorAction SilentlyContinue
227+
}
216228
if ($response.StatusCode -ne 200) {
217229
Write-Debug "Fetching remote content from $URL returned status code $($response.StatusCode)"
218230
return $null
219231
}
220-
$localFile = $OutputPath ? [System.IO.FileInfo]::new($OutputPath) : $(New-TemporaryFile) # If a path was specified, store it at that path; Otherwise use the temp folder
232+
# If a path was specified, store it at that path; Otherwise use the temp folder
233+
if ($OutputPath) {
234+
$localFile = [System.IO.FileInfo]::new($OutputPath)
235+
} else {
236+
$localFile = New-TemporaryFile
237+
}
221238
Write-Debug "Remote content will be stored at $($localFile.FullName)"
222-
$script:CleanupPaths += $Raw ? @($localFile.FullName) : @() # Mark the file for cleanup when the script ends if the raw data was requested
239+
# Mark the file for cleanup when the script ends if the raw data was requested
240+
if ($Raw) {
241+
$script:CleanupPaths += @($localFile.FullName)
242+
}
223243
try {
224244
$downloadTask = $script:HttpClient.GetByteArrayAsync($URL)
225245
[System.IO.File]::WriteAllBytes($localfile.FullName, $downloadTask.Result)
@@ -228,7 +248,12 @@ function Get-RemoteContent {
228248
# If the download fails, write a zero-byte file anyways
229249
$null | Out-File $localFile.FullName
230250
}
231-
return $Raw ? $(Get-Content -Path $localFile.FullName) : $localFile # If the raw content was requested, return the content, otherwise, return the FileInfo object
251+
# If the raw content was requested, return the content, otherwise, return the FileInfo object
252+
if ($Raw) {
253+
return Get-Content -Path $localFile.FullName
254+
} else {
255+
return $localFile
256+
}
232257
}
233258

234259
####
@@ -491,17 +516,24 @@ if (!$SkipManifestValidation -and ![String]::IsNullOrWhiteSpace($Manifest)) {
491516
}
492517
switch ($LASTEXITCODE) {
493518
'-1978335191' {
494-
($validateCommandOutput | Select-Object -Skip 1 -SkipLast 1) | Write-Information # Skip the first line and the empty last line
519+
# Skip the first line and the empty last line
520+
$validateCommandOutput | Select-Object -Skip 1 -SkipLast 1 | ForEach-Object {
521+
Write-Information $_
522+
}
523+
495524
Write-Error -Category ParserError 'Manifest validation failed' -ErrorAction Continue
496525
Invoke-CleanExit -ExitCode 4
497526
}
498527
'-1978335192' {
499-
($validateCommandOutput | Select-Object -Skip 1 -SkipLast 1) | Write-Information # Skip the first line and the empty last line
528+
# Skip the first line and the empty last line
529+
$validateCommandOutput | Select-Object -Skip 1 -SkipLast 1 | ForEach-Object {
530+
Write-Information $_
531+
}
500532
Write-Warning 'Manifest validation succeeded with warnings'
501533
Start-Sleep -Seconds 5 # Allow the user 5 seconds to read the warnings before moving on
502534
}
503535
Default {
504-
$validateCommandOutput.Trim() | Write-Information # On the success, print an empty line after the command output
536+
Write-Information $validateCommandOutput.Trim() # On the success, print an empty line after the command output
505537
}
506538
}
507539
}
@@ -548,7 +580,7 @@ Write-Debug @"
548580
"@
549581

550582
# Set the folder for the files that change with each release version
551-
$script:AppInstallerReleaseAssetsFolder = Join-Path $script:AppInstallerDataFolder -ChildPath 'bin' -AdditionalChildPath $script:AppInstallerReleaseTag
583+
$script:AppInstallerReleaseAssetsFolder = Join-Path -Path (Join-Path -Path $script:AppInstallerDataFolder -ChildPath 'bin') -ChildPath $script:AppInstallerReleaseTag
552584

553585
# Build the dependency information
554586
Write-Verbose 'Building Dependency List'

0 commit comments

Comments
 (0)