Skip to content

Commit 34347ba

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

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

PowerShellEditorServices.build.ps1

+52-17
Original file line numberDiff line numberDiff line change
@@ -193,49 +193,84 @@ 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+
$PSVersion = $PSVersionTable.PSVersion
204205
$ErrorActionPreference = 'Stop'
205206
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'
207+
Write-Host -Fore Green "PowerShell $PSVersion Utility Help is already installed"
207208
return
208209
}
209210

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.'
211+
if ($PSEdition -eq 'Desktop') {
212+
# Cant use requires RunAsAdministrator because PS isn't smart enough to know this is a subscript.
213+
if (-not [Security.Principal.WindowsPrincipal]::new(
214+
[Security.Principal.WindowsIdentity]::GetCurrent()
215+
).IsInRole(
216+
[Security.Principal.WindowsBuiltInRole]::Administrator
217+
)) {
218+
throw 'Windows PowerShell Update-Help requires admin rights. Please re-run the script in an elevated powershell session.'
219+
}
213220
}
214221

215222
# We store our local copy of help files here for testing as some pipelines disallow internet access
216223
$helpPath = '{{HELPPATH}}'
217224

218-
Write-Host -Fore Magenta "Powershell 5.1 Utility Help is not installed, installing from $helpPath"
225+
Write-Host -Fore Magenta "Powershell $PSVersion Utility Help is not installed, installing from $helpPath"
226+
227+
$updateHelpParams = @{
228+
Module = 'Microsoft.PowerShell.Utility'
229+
SourcePath = $helpPath
230+
UICulture = 'en-US'
231+
Force = $true
232+
Verbose = $true
233+
}
219234

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

223243
if ((Get-Help Invoke-RestMethod).remarks -like 'Get-Help cannot find the Help files*') {
224-
throw "Failed to install PowerShell 5.1 Help: $updateHelpOutput"
244+
throw "Failed to install PowerShell $PSVersion Help: $updateHelpOutput"
225245
} else {
226-
Write-Host -Fore Green 'Powershell 5.1 Utility Help installed successfully'
246+
Write-Host -Fore Green "Powershell $PSVersion Utility Help installed successfully"
227247
}
228248
}
229249

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

234-
#We might be running as PS7, so run as separate WinPS process to be sure.
235-
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript
236-
237-
if ($LASTEXITCODE -ne 0) {
238-
throw 'Failed to install PowerShell 5.1 Help.'
255+
# if ($PSEdition -eq 'Core') {
256+
# Write-Host -Fore Magenta 'Checking Runner Pwsh help'
257+
# & ([ScriptBlock]::Create($resolvedScript))
258+
# }
259+
260+
# if ($PwshDaily -and (Get-Command $PwshDaily -ea 0)) {
261+
# Write-Host -Fore Magenta "Checking PWSH Daily help at $PwshDaily"
262+
# & $PwshDaily -NoProfile -NonInteractive -Command $resolvedScript
263+
# if ($LASTEXITCODE -ne 0) {
264+
# throw 'Failed to install PowerShell Daily Help.'
265+
# }
266+
# }
267+
268+
if (Get-Command powershell -ea 0) {
269+
Write-Host -Fore Magenta 'Checking PowerShell 5.1 help'
270+
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript
271+
if ($LASTEXITCODE -ne 0) {
272+
throw 'Failed to install PowerShell 5.1 Help.'
273+
}
239274
}
240275
}
241276

0 commit comments

Comments
 (0)