Skip to content

Fix crash in ScriptRule parsing when PowerShell help system is not initialized on a machine #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public List<ExternalRule> GetExternalRule(string[] moduleNames)
string script = string.Format(CultureInfo.CurrentCulture, "Get-Module -Name '{0}' -ListAvailable", moduleName);
shortModuleName = posh.AddScript(script).Invoke<PSModuleInfo>().First().Name;

// Invokes Update-Help for this module
// Required since when invoking Get-Help later on, the cmdlet prompts for Update-Help interactively
// By invoking Update-Help first, Get-Help will not prompt for downloading help later
script = string.Format(CultureInfo.CurrentCulture, "Update-Help -Module '{0}' -Force", shortModuleName);
posh.AddScript(script).Invoke();

// Invokes Get-Command and Get-Help for each functions in the module.
script = string.Format(CultureInfo.CurrentCulture, "Get-Command -Module '{0}'", shortModuleName);
var psobjects = posh.AddScript(script).Invoke();
Expand Down
21 changes: 21 additions & 0 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ Describe "Test importing customized rules with null return results" {
}

Describe "Test importing correct customized rules" {

Context "Test Get-Help functionality in ScriptRule parsing logic" {
It "ScriptRule help section must be correctly processed when Get-Help is called for the first time" {

# Force Get-Help to prompt for interactive input to download help using Update-Help
# By removing this registry key we force to turn on Get-Help interactivity logic during ScriptRule parsing
$null,"Wow6432Node" | ForEach-Object {
try
{
Remove-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -ErrorAction Stop
} catch {
#Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
}
}

$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should Be 1
}

}

Context "Test Get-ScriptAnalyzer with customized rules" {
It "will show the customized rule" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure}
Expand Down