【发布时间】:2021-04-28 00:58:08
【问题描述】:
如果存在,我想杀死 nodepad 进程。如果我使用这个:
PS C:\> get-process -name notepad | Stop-Process -Force
进程不存在时会报错。
get-process : Cannot find a process with the name "notepad". Verify the process name and call the cmdlet again.
At line:1 char:1
+ get-process -name notepad | Stop-Process -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (notepad:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
我觉得应该是静默的,因为不是错误,对象应该是null。
我知道我能做到:
PS C:\> get-process | where { $_.processname -eq 'notepad' } | Stop-Process -Force
但如果存在,我更喜欢简单的方法。谢谢。
PS:我需要关闭记事本,因为当我以非交互方式安装 ClamAV 时,它会使用用户手册创建记事本。我找不到告诉 ClamAV 不要打开用户手册的方法。
【问题讨论】:
标签: windows powershell clamav