Please feel free to provide feedback or file bugs here.

Script Analyzer - Severity for Custom Rule does not work

Votes from Connect: 6

Original Date Submitted: 3/20/2015 7:53:36 PM

Description:
********Contact Information********
Handle: Stefan Stranger
Site Name: PowerShell
Feedback ID: 1192179
***************************************

Frequency: Always Happens
Regression: I do not know if this issue existed previously

Problem Description:
When creating a custom script analyzer rule the severity does not seem to be executed.

Example:
<#
.SYNOPSIS
You should never use Write-Host to create any script output whatsoever.
.DESCRIPTION
It is generally accepted that you should never use Write-Host to create any script output whatsoever, unless your script (or function, or whatever) uses the Show verb (as in, Show-Performance).
That verb explicitly means "show on the screen, with no other possibilities." Like Show-Command.
To fix a violation of this rule, please replace Write-Host with Write-Output in most scenarios.
.EXAMPLE
Test-WriteHost -CommandAst $CommandAst
.INPUTS
[System.Management.Automation.Language.CommandAst]
.OUTPUTS
[PSCustomObject[]]
.NOTES
Reference: Output, The Community Book of PowerShell Practices.
#>

function Test-WriteHost

{
[CmdletBinding()]
[OutputType([PSCustomObject[]])]

Param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.CommandAst]
$CommandAst
)

Process
{
try
{
$commandName = $CommandAst.GetCommandName()
# Checks command name, if the command name matches Write-Host or
# user-defined aliases, this rule is triggered.

if ($null -ne $commandName)
{
$alias = (Get-Alias -Definition 'Write-Host' -ErrorAction SilentlyContinue).Name
if (($commandName -eq 'Write-Host') -or
($alias -eq $commandName))
{
[PSCustomObject]@{
Message = 'Avoid Using Write-Host. Write-Host makes it difficult to use functions from other functions and the pipeline.'
Extent = $CommandAst.Extent
RuleName = $PSCmdlet.MyInvocation.InvocationName
Severity = 'Warning'
}
}
}
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}

Output:
MyScriptAnalyzerRules\Tes Strict 50 Avoid Using Write-Host. Write-Host makes it difficult to
t-WriteHost use functions from other functions and the pipeline.

Severity is showing strict instead of warning.

Product Studio item created by Connect Synchronizer due to creation of feedback ID 1192179 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1192179).

Repro Steps:
See description

Expected Results:

Internal BugId: 14327

1 vote
Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)

We’ll send you updates on this idea

AdminJoey Aiello [MSFT] (Program Manager, Windows Server) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

0 comments

Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base