|
| 1 | +<# |
| 2 | +A PowerShell script that is used to invoke a VSTS task script. This script is used by the VSTS task runner to invoke the task script. |
| 3 | +This script replaces some legacy stuff in PowerShell3Handler.cs and turns it into a dedicated signed script. |
| 4 | +since it is parameterized it can be signed and trusted for WDAC and CLM. |
| 5 | +#> |
| 6 | + |
| 7 | +param ( |
| 8 | + [Parameter(mandatory = $true)] |
| 9 | + [string]$VstsSdkPath, |
| 10 | + |
| 11 | + [Parameter(mandatory = $true)] |
| 12 | + [string]$DebugOption, |
| 13 | + |
| 14 | + [Parameter(mandatory = $true)] |
| 15 | + [string]$ScriptBlockString |
| 16 | + |
| 17 | +) |
| 18 | + |
| 19 | +function Get-ClmStatus { |
| 20 | + # This is new functionality to detect if we are running in a constrained language mode. |
| 21 | + # This is only used to display debug data if the device is in CLM mode by default. |
| 22 | + |
| 23 | + # Create a temp file and add the command which not allowed in constrained language mode. |
| 24 | + $tempFileGuid = New-Guid | Select-Object -Expand Guid |
| 25 | + $tempFile = "$($env:AGENT_TEMPDIRECTORY)\$($tempFileGuid).ps1" |
| 26 | + |
| 27 | + Write-Output '$null = New-Object -TypeName System.Collections.ArrayList' | Out-File -FilePath $tempFile |
| 28 | + |
| 29 | + try { |
| 30 | + . $tempFile |
| 31 | + $status = "FullLanguage" |
| 32 | + } |
| 33 | + catch [System.Management.Automation.PSNotSupportedException] { |
| 34 | + $status = "ConstrainedLanguage" |
| 35 | + } |
| 36 | + |
| 37 | + Remove-Item $tempFile |
| 38 | + return $status |
| 39 | +} |
| 40 | + |
| 41 | +$VerbosePreference = $DebugOption |
| 42 | +$DebugPreference = $DebugOption |
| 43 | + |
| 44 | +if (!$PSHOME) { |
| 45 | + Write-Error -Message "The execution cannot be continued since the PSHOME variable is not defined." -ErrorAction Stop |
| 46 | +} |
| 47 | + |
| 48 | +# Check if the device is in CLM mode by default. |
| 49 | +$clmResults = Get-ClmStatus |
| 50 | +Write-Verbose "PowerShell Language mode: $($clmResults)" |
| 51 | + |
| 52 | +if ([Console]::InputEncoding -is [Text.UTF8Encoding] -and [Console]::InputEncoding.GetPreamble().Length -ne 0) { |
| 53 | + [Console]::InputEncoding = New-Object Text.UTF8Encoding $false |
| 54 | +} |
| 55 | + |
| 56 | +Import-Module -Name ([System.IO.Path]::Combine($PSHOME, 'Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1')) |
| 57 | +Import-Module -Name ([System.IO.Path]::Combine($PSHOME, 'Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1')) |
| 58 | + |
| 59 | +$importSplat = @{ |
| 60 | + Name = $VstsSdkPath |
| 61 | + ErrorAction = 'Stop' |
| 62 | +} |
| 63 | + |
| 64 | +# Import the module and catch any errors |
| 65 | +try { |
| 66 | + Import-Module @importSplat |
| 67 | +} |
| 68 | +catch { |
| 69 | + Write-Verbose $_.Exception.Message -Verbose |
| 70 | + throw $_.Exception |
| 71 | +} |
| 72 | + |
| 73 | +# Now create the task and hand of to the task script |
| 74 | +try { |
| 75 | + Invoke-VstsTaskScript -ScriptBlock ([scriptblock]::Create( $ScriptBlockString )) |
| 76 | +} |
| 77 | +# We want to add improved error handling here - if the error is "xxx\powershell.ps1 is not recognized as the name of a cmdlet, function, script file, or operable program" |
| 78 | +# |
| 79 | +catch { |
| 80 | + Write-Verbose "Invoke-VstsTaskScript -ScriptBlock ([scriptblock]::Create( $ScriptBlockString ))" |
| 81 | + Write-Verbose $_.Exception.Message -Verbose |
| 82 | + throw $_.Exception |
| 83 | +} |
| 84 | +# |
0 commit comments