PowerShell / PowerShell/PSScriptAnalyzer

New Rule: Calling Start-Process without checking ExitCode

Đang mở
#1,062 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Issue - New Rule
Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét các mẫu gọi Microsoft.PowerShell.Management\Start-Process được đề xuất, bao gồm các đối số trực tiếp, các tham số được splat và việc sử dụng Start-Job. Xác định xem có nên bao phủ các lệnh gọi bất đồng bộ và việc suppression tường minh bằng [void] hoặc Out-Null hay không, sau đó xác minh rằng quy tắc kết quả chỉ cảnh báo ở nơi cần kiểm tra kết quả thoát.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
powershell
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.