Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active July 28, 2018 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/8de3469bf31e5cef6fb371a5ced0466e to your computer and use it in GitHub Desktop.
Save stknohg/8de3469bf31e5cef6fb371a5ced0466e to your computer and use it in GitHub Desktop.
プロセスのコマンドラインを取得する関数
<#
.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