Skip to content

Commit 1c36127

Browse files
committed
Merge pull request #384 from PowerShell/UpdateHelpFixBranch
Script Analyzer shouldn't automatically update help for modules
2 parents e406f26 + 466b04c commit 1c36127

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,7 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
554554
{
555555
posh.AddCommand("Get-Module").AddParameter("Name", moduleName).AddParameter("ListAvailable");
556556
shortModuleName = posh.Invoke<PSModuleInfo>().First().Name;
557-
558-
// Invokes Update-Help for this module
559-
// Required since when invoking Get-Help later on, the cmdlet prompts for Update-Help interactively
560-
// By invoking Update-Help first, Get-Help will not prompt for downloading help later
561-
posh.AddCommand("Update-Help").AddParameter("Module", shortModuleName).AddParameter("Force");
562-
posh.Invoke();
563-
557+
564558
// Invokes Get-Command and Get-Help for each functions in the module.
565559
posh.Commands.Clear();
566560
posh.AddCommand("Get-Command").AddParameter("Module", shortModuleName);
@@ -586,8 +580,21 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
586580
//Only add functions that are defined as rules.
587581
if (param != null)
588582
{
589-
posh.AddCommand("Get-Help").AddParameter("Name", funcInfo.Name);
590-
Collection<PSObject> helpContent = posh.Invoke();
583+
// On a new image, when Get-Help is run the first time, PowerShell offers to download updated help content
584+
// using Update-Help. This results in an interactive prompt - which we cannot handle
585+
// Workaround to prevent Update-Help from running is to set the following reg key
586+
// HKLM:\Software\Microsoft\PowerShell\DisablePromptToUpdateHelp
587+
// OR execute Update-Help in an elevated admin mode before running ScriptAnalyzer
588+
Collection<PSObject> helpContent = null;
589+
try
590+
{
591+
posh.AddCommand("Get-Help").AddParameter("Name", funcInfo.Name);
592+
helpContent = posh.Invoke();
593+
}
594+
catch (Exception getHelpException)
595+
{
596+
this.outputWriter.WriteWarning(getHelpException.Message.ToString());
597+
}
591598

592599
// Retrieve "Description" field in the help content
593600
string desc = String.Empty;

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
66
Import-Module PSScriptAnalyzer
77
}
88

9+
# Force Get-Help not to prompt for interactive input to download help using Update-Help
10+
# By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
11+
$null,"Wow6432Node" | ForEach-Object {
12+
try
13+
{
14+
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
15+
}
16+
catch
17+
{
18+
# Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
19+
}
20+
}
21+
922
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
1023
$message = "this is help"
1124
$measure = "Measure-RequiresRunAsAdministrator"
@@ -46,8 +59,20 @@ Describe "Test importing correct customized rules" {
4659

4760
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
4861
$customizedRulePath.Count | Should Be 1
49-
}
50-
62+
63+
# Force Get-Help not to prompt for interactive input to download help using Update-Help
64+
# By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
65+
$null,"Wow6432Node" | ForEach-Object {
66+
try
67+
{
68+
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
69+
}
70+
catch
71+
{
72+
# Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
73+
}
74+
}
75+
}
5176
}
5277

5378
Context "Test Get-ScriptAnalyzer with customized rules" {

Tests/Engine/LibraryUsage.tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ $runspace.Open();
134134
# Let other test scripts know we are testing library usage now
135135
$testingLibraryUsage = $true
136136

137+
# Force Get-Help not to prompt for interactive input to download help using Update-Help
138+
# By adding this registry key we force to turn off Get-Help interactivity logic during ScriptRule parsing
139+
$null,"Wow6432Node" | ForEach-Object {
140+
try
141+
{
142+
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
143+
}
144+
catch
145+
{
146+
# Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
147+
}
148+
}
149+
137150
# Invoke existing test files that use Invoke-ScriptAnalyzer
138151
. $directory\InvokeScriptAnalyzer.tests.ps1
139152
. $directory\RuleSuppression.tests.ps1

0 commit comments

Comments
 (0)