PowerShell / PowerShell/PSScriptAnalyzer

New Rule: Calling Start-Process without checking ExitCode

オープン
#1,062 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Issue - New Rule
主要言語
C#
スター
2.2k
フォーク
415
平均マージ
13時間 1分
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、直接引数、スプラッティングされたパラメーター、Start-Job の使用を含む、提案されている Microsoft.PowerShell.Management\Start-Process の呼び出しパターンを確認します。非同期呼び出しと、[void] または Out-Null による明示的な抑制を対象に含めるべきかを判断し、その後、結果のルールが終了結果を確認する意図がある場合にのみ警告することを検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
powershell
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。