Skip to content

Commit b22510a

Browse files
committed
Expand help update to all potential pwsh versions
1 parent 4a5a892 commit b22510a

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

PowerShellEditorServices.build.ps1

+57-18
Original file line numberDiff line numberDiff line change
@@ -193,49 +193,88 @@ Task BuildCmdletHelp -After AssembleModule {
193193
}
194194

195195
Task SetupHelpForTests {
196-
# Powershell 7+ ship with help included, but 5.1 does not on Windows Servers and CI. The secure devops pipeline also does not allow internet access, so we must update help from our local repository source.
196+
# Some CI do not ship with help included, and the secure devops pipeline also does not allow internet access, so we must update help from our local repository source.
197197

198198
# Only commands in Microsoft.PowerShell.Utility can be tested for help so as to minimize the repository storage.
199-
# This requires admin rights
199+
# This requires admin rights for PS5.1
200200

201-
#NOTE: You can run this task once as admin or update help separately, and continue to run tests as non-admin, if for instance developing locally.
201+
#NOTE: You can run this task once as admin or update help separately, and continue to run tests as non-admin, if for instance developing locally. Also this help is for PS5.1 so tests should be written for that even if PS7. For instance, don't write tests for new Invoke-RestMethod parameters.
202202

203203
$installHelpScript = {
204+
param(
205+
[Parameter(Mandatory)][string]$helpPath
206+
)
207+
$PSVersion = $PSVersionTable.PSVersion
204208
$ErrorActionPreference = 'Stop'
209+
$helpPath = Resolve-Path $helpPath
210+
if ($PSEdition -ne 'Desktop') {
211+
$helpPath = Join-Path $helpPath '7'
212+
}
213+
205214
if ((Get-Help Invoke-RestMethod).remarks -notlike 'Get-Help cannot find the Help files*') {
206-
Write-Host -Fore Green 'Powershell 5.1 Utility Help is already installed'
215+
Write-Host -Fore Green "PowerShell $PSVersion Utility Help is already installed"
207216
return
208217
}
209218

210-
# Cant use requires RunAsAdministrator because PS isn't smart enough to know this is a subscript.
211-
if (-not [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
212-
throw 'Windows PowerShell Update-Help requires admin rights. Please re-run the script in an elevated powershell session.'
219+
if ($PSEdition -eq 'Desktop') {
220+
# Cant use requires RunAsAdministrator because PS isn't smart enough to know this is a subscript.
221+
if (-not [Security.Principal.WindowsPrincipal]::new(
222+
[Security.Principal.WindowsIdentity]::GetCurrent()
223+
).IsInRole(
224+
[Security.Principal.WindowsBuiltInRole]::Administrator
225+
)) {
226+
throw 'Windows PowerShell Update-Help requires admin rights. Please re-run the script in an elevated powershell session.'
227+
}
213228
}
214229

215-
# We store our local copy of help files here for testing as some pipelines disallow internet access
216-
$helpPath = '{{HELPPATH}}'
230+
Write-Host -Fore Magenta "Powershell $PSVersion Utility Help is not installed, installing from $helpPath"
217231

218-
Write-Host -Fore Magenta "Powershell 5.1 Utility Help is not installed, installing from $helpPath"
232+
$updateHelpParams = @{
233+
Module = 'Microsoft.PowerShell.Utility'
234+
SourcePath = $helpPath
235+
UICulture = 'en-US'
236+
Force = $true
237+
Verbose = $true
238+
}
219239

240+
#PS7+ does not require admin rights if currentuser is used for scope. 5.1 does not have this option.
241+
if ($PSEdition -ne 'Desktop') {
242+
$updateHelpParams.'Scope' = 'CurrentUser'
243+
}
220244
#Update the help, and capture verbose output
221-
$updateHelpOutput = Update-Help 'Microsoft.PowerShell.Utility' -Verbose -SourcePath $helpPath -Force *>&1
245+
Wait-Debugger
246+
$updateHelpOutput = Update-Help @updateHelpParams *>&1
222247

223248
if ((Get-Help Invoke-RestMethod).remarks -like 'Get-Help cannot find the Help files*') {
224-
throw "Failed to install PowerShell 5.1 Help: $updateHelpOutput"
249+
throw "Failed to install PowerShell $PSVersion Help: $updateHelpOutput"
225250
} else {
226-
Write-Host -Fore Green 'Powershell 5.1 Utility Help installed successfully'
251+
Write-Host -Fore Green "Powershell $PSVersion Utility Help installed successfully"
227252
}
228253
}
229254

230255
#Need this to inject the help file path, since PSScriptRoot won't work inside the script
231-
$helpPath = "$PSScriptRoot\test\PowerShellEditorServices.Test.Shared\PSHelp"
256+
$helpPath = Resolve-Path "$PSScriptRoot\test\PowerShellEditorServices.Test.Shared\PSHelp" -ErrorAction Stop
257+
Write-Host -Fore Magenta "Runner Help located at $helpPath"
232258
$resolvedScript = $installHelpScript -replace '{{HELPPATH}}', $helpPath
233259

234-
#We might be running as PS7, so run as separate WinPS process to be sure.
235-
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript
260+
if (Get-Command powershell.exe -CommandType Application -ea 0) {
261+
Write-Host -Fore Magenta 'Checking PowerShell 5.1 help'
262+
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript -args $helpPath
263+
if ($LASTEXITCODE -ne 0) {
264+
throw 'Failed to install PowerShell 5.1 Help.'
265+
}
266+
}
267+
if ($PSEdition -eq 'Core') {
268+
Write-Host -Fore Magenta 'Checking Runner Pwsh help'
269+
& ([ScriptBlock]::Create($resolvedScript))
270+
}
236271

237-
if ($LASTEXITCODE -ne 0) {
238-
throw 'Failed to install PowerShell 5.1 Help.'
272+
if ($PwshDaily -and (Get-Command $PwshDaily -ea 0)) {
273+
Write-Host -Fore Magenta "Checking PWSH Daily help at $PwshDaily"
274+
& $PwshDaily -NoProfile -NonInteractive -Command $resolvedScript
275+
if ($LASTEXITCODE -ne 0) {
276+
throw 'Failed to install PowerShell Daily Help.'
277+
}
239278
}
240279
}
241280

0 commit comments

Comments
 (0)