PowerShell / PowerShell/PSScriptAnalyzer

New Rule: Calling Start-Process without checking ExitCode

未关闭
#1,062 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Issue - New Rule
主要语言
C#
星标
2.2k
派生
414
平均合并
13 小时 1 分钟
30 天内合并 PR
2

描述

Summary of the new feature
I recently got burned by a library not handling the exit code for Microsoft.PowerShell.Management\Start-Process.

This could be broken into two rules:

  • Always assign Start-Process to a local variable, and check the result. Caveat: How would this work if invoked from Start-Job and deliberately intended to be asynchronous?

Proposed technical implementation details (optional)

  1. Find calls to Microsoft.PowerShell.Management\Start-Process
    There are several ways this could be called:
Arguments not known at caller. No local way to detect $args sets the -Wait and -NoNewWindow switches on
function Do
{
param(
 [hashtable]$StartProcess_params
)
  Microsoft.PowerShell.Management\Start-Process @StartProcess_params
}
Trivial case: Direct arguments
Microsoft.PowerShell.Management\Start-Process -FilePath "cmd.exe" -ArgumentList "/C" -Wait -NoNewWindow
Middle case: arguments constructed in same scope - I believe this is called $script scope
$StartProcess_params = @{
  FilePath = "cmd.exe"
  ArgumentList = "/C"
  RedirectStandardError = $tempErrorFile
  RedirectStandardOutput = $tempOutputFile
  NoNewWindow = $true
  Wait = $true
}
Microsoft.PowerShell.Management\Start-Process @StartProcess_params
Using Start-Job
Start-Job -Name DoSomething -ScriptBlock {
    & cmd.exe /C
    Write-Output $LASTEXITCODE
}
#Do other stuff here
Get-Job -Name DoSomething | Wait-Job | Receive-Job

A clear and concise description of what you want to happen.
Flag as warnings with suggestion to assign call to a PS variable, e.g.:
Microsoft.PowerShell.Management\Start-Process @StartProcess_params
would become:

$process = Microsoft.PowerShell.Management\Start-Process @StartProcess_params
if (-not $process.ExitCode)
{
  Write-Error $(Microsoft.PowerShell.Management\Get-Content $tempErrorFile
}

Alternatively, if the user truly wishes to suppress the result, the UI could offer a "No, I really don't care and want to explicitly say so" command, which would re-write the code to be:

$(Microsoft.PowerShell.Management\Start-Process @StartProcess_params) | Out-Null

or

[void]Microsoft.PowerShell.Management\Start-Process @StartProcess_params

According to StackOverflow, Out-Null adds a 60% overhead and therefore is slower than [void], so we should probably suggest [void]Microsoft.PowerShell.Management\Start-Process @StartProcess_params

What is the latest version of PSScriptAnalyzer at the point of writing
1.17.1

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查提议的 Microsoft.PowerShell.Management\Start-Process 调用模式,包括直接参数、展开的参数以及 Start-Job 的使用。确定是否应涵盖异步调用和使用 [void] 或 Out-Null 进行的显式抑制,然后验证生成的规则仅在应检查退出结果的情况下发出警告。

由索引模型根据 Issue 内容生成。

评估

技术栈
powershell
领域
tooling
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。