默认情况下,从PowerShell启动进程时,它会附加到其父(shell)。如果您终止进程树,这些进程也会死掉。我想启动一个shell的对等进程;又名。当我杀死PowerShell树时,我不希望它死掉。
$spotifyProcess = start Spotify.exe -PassThru
$spotifyParentId = (gwmi win32_process -Filter "processid='$($spotifyProcess.Id)'").ParentProcessId
$shellId = [System.Diagnostics.Process]::GetCurrentProcess().Id
if ($spotifyParentId -eq $shellId)
{
throw 'Not what I want!!'
}
答案 0 :(得分:5)
您可以使用Win32_Process
WMI类的Create
方法。有几种方法可以在PowerShell中调用它:
Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList notepad
([WmiClass]'Win32_Process').Create('notepad')
<强> V3 +:强>
Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{CommandLine='notepad'}
答案 1 :(得分:2)
“经典”技巧,由于Windows中进程树的简单特性(只是每个进程'祖先的向后链接列表),是打开一个单独的进程,然后启动新的“独立”进程
在powershell.exe
中,这非常简单:
powershell.exe -Command 'Start-Process notepad.exe'
“内部”powershell.exe
实例启动notepad.exe
,然后立即退出,留下notepad.exe
孤儿