Skip to content

Commit 77c1fe3

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

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

PowerShellEditorServices.build.ps1

+48-15
Original file line numberDiff line numberDiff line change
@@ -193,49 +193,82 @@ 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+
$updateHelpOutput = Update-Help @updateHelpParams *>&1
222241

223242
if ((Get-Help Invoke-RestMethod).remarks -like 'Get-Help cannot find the Help files*') {
224-
throw "Failed to install PowerShell 5.1 Help: $updateHelpOutput"
243+
throw "Failed to install PowerShell $PSVersion Help: $updateHelpOutput"
225244
} else {
226-
Write-Host -Fore Green 'Powershell 5.1 Utility Help installed successfully'
245+
Write-Host -Fore Green "Powershell $PSVersion Utility Help installed successfully"
227246
}
228247
}
229248

230249
#Need this to inject the help file path, since PSScriptRoot won't work inside the script
231250
$helpPath = "$PSScriptRoot\test\PowerShellEditorServices.Test.Shared\PSHelp"
232251
$resolvedScript = $installHelpScript -replace '{{HELPPATH}}', $helpPath
233252

234-
#We might be running as PS7, so run as separate WinPS process to be sure.
235-
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript
253+
if ($PSEdition -eq 'Core') {
254+
Write-Host -Fore Magenta 'Checking Runner Pwsh help'
255+
& ([ScriptBlock]::Create($resolvedScript))
256+
}
236257

237-
if ($LASTEXITCODE -ne 0) {
238-
throw 'Failed to install PowerShell 5.1 Help.'
258+
if ($PwshDaily) {
259+
Write-Host -Fore Magenta "Checking PWSH Daily help at $PwshDaily"
260+
& $PwshDaily -NoProfile -NonInteractive -Command $resolvedScript
261+
if ($LASTEXITCODE -ne 0) {
262+
throw 'Failed to install PowerShell Daily Help.'
263+
}
264+
}
265+
266+
if (Get-Command powershell -ea 0) {
267+
Write-Host -Fore Magenta 'Checking PowerShell 5.1 help'
268+
powershell.exe -NoProfile -NonInteractive -Command $resolvedScript
269+
if ($LASTEXITCODE -ne 0) {
270+
throw 'Failed to install PowerShell 5.1 Help.'
271+
}
239272
}
240273
}
241274

0 commit comments

Comments
 (0)