プロセスのコマンドラインを取得する関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Get the command line of a process. | |
#> | |
function Get-ProcessCommandline { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[int]$Id | |
) | |
if ($Id -lt 0) { | |
return "" | |
} | |
# Windows PowerShell | |
if ($null -eq $PSEdition -or "Desktop" -eq $PSEdition) { | |
$proc = Get-WmiObject -Class Win32_Process -Filter "ProcessId = $Id" -Property "ProcessId", "CommandLine" | |
if ($null -eq $proc) { | |
return "" | |
} | |
return $proc.CommandLine | |
} | |
# PowerShell Core on Windows | |
if ($IsWindows) { | |
$proc = Get-CimInstance -Class Win32_Process -Filter "ProcessId = $Id" -Property "ProcessId", "CommandLine" | |
if ($null -eq $proc) { | |
return "" | |
} | |
return $proc.CommandLine | |
} | |
# PowerShell Core on macOS | |
if ($IsMacOS) { | |
return (/bin/ps -o command= -p $Id) | |
} | |
# PowerShell Core on Linux | |
if (-not (Test-Path -LiteralPath "/proc/$Id")) { | |
return "" | |
} | |
return @(Get-Content -LiteralPath "/proc/$Id/cmdline")[0] -replace "\0", " " | |
} | |
# 実行例 | |
Get-Process | ForEach-Object { | |
[PSCustomObject]@{ | |
Name = $_.Name; | |
CommandLine = (Get-ProcessCommandline -Id $_.Id); | |
} | |
} | Format-List |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment