Powershell – 隐藏显示的错误信息,出现在第3行

我需要帮助隐藏下面的错误信息,当Powershell没有find一个Excel实例运行(或以某种方式绕过它):

Get-Process:找不到名称为“excel”的进程。 validation进程名称并再次调用该cmdlet。 在run.ps1:3 char:24 + $ before = @(get-process <<<< excel |%{$ _ .Id})+ CategoryInfo:ObjectNotFound:(excel:String)[Get-Process],ProcessCommandException + FullyQualifiedErrorId:NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

我的代码行3-5如下:

$before = @(get-process excel | %{$_.Id} ) $excel=new-object -com excel.application $excelId = get-process excel | %{$_.Id} | ?{$before -notcontains $_} 

也许你可以做到以下几点?

 Get-Process Excel -ErrorAction SilentlyContinue | %{ $_.Id } 

或这个?

 Get-Process | ?{ $_.name -eq "excel" } | %{ $_.Id }