Powershell ErrorAction 不沉默
Powershell ErrorAction not silent
我有一个 PS 脚本可以在我们网络上的计算机上查找 Office15 文件夹。在大多数情况下,该脚本按预期工作。其实这只是我挑剔。我设置了 -ErrorAction SilentlyContinue
,但是当找不到 Office15 文件夹时的错误消息仍然出现在屏幕上。我想知道我是不是做错了什么,或者只是没有真正理解我的脚本在做什么。
$filePath = "\"+$computer+"\c$\Program Files (x86)\Microsoft Office\"
$listing = Get-ChildItem $filePath | where-object { $_.name -eq "Office15" } | Select-Object Name -ErrorAction SilentlyContinue
按原样使用此脚本,出现如下错误:
Get-ChildItem : Cannot find path '\COMPNAME\c$\Program Files (x86)\Microsoft Office\' because it does not exist.
At C:\Users\someGuy\bootTime\checkOffice.ps1:16 char:20
+ $listing = Get-ChildItem $filePath | where-object { $_.name -eq "Office1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\COMPNAME\c$\Pr...crosoft Office\:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
我将所有有效结果都输入到一个文本文件中,因此脚本的其他部分工作得很好,否则我得到了预期的结果。我只是真的有兴趣了解我在这里可能做错了什么。
您需要将错误操作传递给 gci
:
$listing = Get-ChildItem $filePath -ErrorAction SilentlyContinue | where-object { $_.name -eq "Office15" } | Select-Object Name
我有一个 PS 脚本可以在我们网络上的计算机上查找 Office15 文件夹。在大多数情况下,该脚本按预期工作。其实这只是我挑剔。我设置了 -ErrorAction SilentlyContinue
,但是当找不到 Office15 文件夹时的错误消息仍然出现在屏幕上。我想知道我是不是做错了什么,或者只是没有真正理解我的脚本在做什么。
$filePath = "\"+$computer+"\c$\Program Files (x86)\Microsoft Office\"
$listing = Get-ChildItem $filePath | where-object { $_.name -eq "Office15" } | Select-Object Name -ErrorAction SilentlyContinue
按原样使用此脚本,出现如下错误:
Get-ChildItem : Cannot find path '\COMPNAME\c$\Program Files (x86)\Microsoft Office\' because it does not exist.
At C:\Users\someGuy\bootTime\checkOffice.ps1:16 char:20
+ $listing = Get-ChildItem $filePath | where-object { $_.name -eq "Office1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\COMPNAME\c$\Pr...crosoft Office\:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
我将所有有效结果都输入到一个文本文件中,因此脚本的其他部分工作得很好,否则我得到了预期的结果。我只是真的有兴趣了解我在这里可能做错了什么。
您需要将错误操作传递给 gci
:
$listing = Get-ChildItem $filePath -ErrorAction SilentlyContinue | where-object { $_.name -eq "Office15" } | Select-Object Name