是否需要终止通过PowerShell远程创建的进程?如何操作?
Great question! Let's break this down clearly for you.
It depends entirely on what your C:\script.bat does:
- If the batch file is a one-time task (like copying files, running a quick configuration command), it’ll automatically exit once it finishes executing. The underlying
cmd.exeprocess will terminate on its own, so you don’t need to take any action. - If the batch file runs a long-running or persistent task (such as an infinite loop, a monitoring script that stays active), the process will keep running until you manually stop it. In this case, you’ll want to terminate it explicitly when you’re done.
Since you’re already using WMI for process creation, let’s stick with that framework (and its modern CIM counterpart) for termination—they’re reliable and align with your existing workflow.
Method 1: Use the Process ID from Your Original Creation Call
When you run $process.Create("C:\script.bat"), the $result object includes a ProcessId property that gives you the exact ID of the new process. You can store this ID and use it later to target the process:
# Your original process creation code $server = "ServerName" $process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process" $result = $process.Create("C:\script.bat") # Save the Process ID for later use $targetProcessId = $result.ProcessId # When you need to terminate the process: $remoteProcess = Get-WmiObject -ComputerName $server -Class Win32_Process -Filter "ProcessId = $targetProcessId" if ($remoteProcess) { $terminateStatus = $remoteProcess.Terminate() # A return value of 0 means successful termination if ($terminateStatus.ReturnValue -eq 0) { Write-Host "Process $targetProcessId terminated successfully on $server" } else { Write-Host "Failed to terminate process $targetProcessId. Error code: $($terminateStatus.ReturnValue)" } } else { Write-Host "Process $targetProcessId not found on $server—it may have already exited on its own" }
Method 2: Use CIM (Modern Alternative to WMI)
CIM is the updated, more compatible successor to WMI. If you’re using PowerShell 5.1 or later, this is a great option:
$server = "ServerName" $targetProcessId = <your-stored-process-id> # Replace with the PID from your creation result $remoteProcess = Get-CimInstance -ComputerName $server -ClassName Win32_Process -Filter "ProcessId = $targetProcessId" if ($remoteProcess) { $terminateStatus = Invoke-CimMethod -InputObject $remoteProcess -MethodName Terminate if ($terminateStatus.ReturnValue -eq 0) { Write-Host "Process $targetProcessId terminated successfully on $server" } else { Write-Host "Failed to terminate process $targetProcessId. Error code: $($terminateStatus.ReturnValue)" } } else { Write-Host "Process $targetProcessId not found on $server" }
Method 3: Terminate by Process Name (If You Don’t Have the PID)
If you didn’t save the PID, you can target the process by its name (batch files run under cmd.exe by default). Warning: This will affect all processes with that name, so add a check to make sure you’re only terminating the one running your script:
$server = "ServerName" $targetScriptPath = "C:\script.bat" # Get all cmd.exe processes running the target batch file $remoteProcesses = Get-WmiObject -ComputerName $server -Class Win32_Process -Filter "Name = 'cmd.exe'" | Where-Object { $_.CommandLine -like "*$targetScriptPath*" } foreach ($proc in $remoteProcesses) { $terminateStatus = $proc.Terminate() if ($terminateStatus.ReturnValue -eq 0) { Write-Host "Process $($proc.ProcessId) (running $targetScriptPath) terminated successfully" } }
内容的提问来源于stack exchange,提问作者Rotan075

