我正在尝试编写一个PS1脚本:
1.取一个计算机名和一个进程名
2.向您显示与搜索匹配的所有PID和进程
3.询问您要杀死的PID。 (我没有将这部分代码包括在内)
我需要有关$ processInfo数组的帮助。 我希望能够浏览每个过程并显示名称和ID。然后我将知道要杀死的PID是什么。
因此,如果我搜索“ App *”,如何输出为以下格式:
Process ID: 1000 Name: Apple
Process ID: 2000 Name: Appster
Process ID: 3000 Name: AppSample
这是我到目前为止所拥有的
# Look up a computer, and a process, and then
$computerName = Read-Host "Enter the FQDN of the target computer:"
# Enter the name of the process you're looking for. Wildcard searching is asterix
$processSearch = Read-Host "Enter the process name to look for:"
# Create a process array with PID, Name, and Runpath
$processInfo = (
processID = get-process -ComputerName $computerName -Name $processSearch | select -expand ID,
processName = get-process -ComputerName $computerName -Name $processSearch |select -expand Name,
processPath = get-process -ComputerName $computerName -Name $processSearch |select -expand Path
)
# Display all of the processes and IDs that match your search
foreach($id in $processInfo){
write-host Process ID: $id.processID Name: $id.processName
}
答案 0 :(得分:2)
Get-Process
可以在Name
参数中使用通配符。因此,您只需要遍历对象并输出所需的属性即可。
# Look up a computer, and a process, and then
$ComputerName = Read-Host "Enter the FQDN of the target computer:"
# Enter the name of the process you're looking for. Wildcard searching is asterix
$ProcessSearch = Read-Host "Enter the process name to look for:"
Get-Process -ComputerName $ComputerName -Name "$ProcessSearch*" | ForEach-Object {Write-Host Process ID: $_.ID Name: $_.ProcessName}
您还可以get rid of all of the Read-Host
and Write-Host
以获得更强大的感觉。
Get-Process -ComputerName $ComputerName -Name "$ProcessSearch*" | Select-Object ID,ProcessName