I'd like to use PowerShell to check whether an IIS Web Application exists (or potentially some other kind of item). I can do this with Get-Item
, but that reports an error if the item doesn't exist, which is misleading to the user running the script - it looks like something went wrong when actually everything is fine.
我想使用PowerShell来检查是否存在IIS Web应用程序(或者可能是其他类型的项目)。我可以使用Get-Item执行此操作,但如果项目不存在则会报告错误,这会误导运行脚本的用户 - 实际上一切正常时看起来出了问题。
How do I do this without an error?
如何在没有错误的情况下执行此操作?
2 个解决方案
#1
28
Use the command ... get-item blah -ErrorAction SilentlyContinue
使用命令... get-item blah -ErrorAction SilentlyContinue
#2
53
The cmdlet Test-Path
is specifically designed for this, it determines whether items of a path exist. It returns a Boolean value and does not generate an error.
cmdlet Test-Path是专门为此设计的,它确定路径的项是否存在。它返回一个布尔值,不会生成错误。
The cmdlet Get-Item
(and similar) can be used, too, but not directly. One way is already proposed: use -ErrorAction SilentlyContinue
. It might be important to know that in fact it still generates an error; it just does not show it. Check the error collection $Error
after the command, the error is there.
也可以使用cmdlet Get-Item(和类似的),但不能直接使用。已经提出了一种方法:使用-ErrorAction SilentlyContinue。知道事实上它仍然会产生错误可能很重要;它只是没有显示出来。在命令之后检查错误集合$ Error,错误就在那里。
Just for information
仅供参考
There is a funny way to avoid this error (it also works with some other cmdlets like Get-Process
that do not have a Test-Path
alternative). Suppose we are about to check existence of an item "MyApp.exe" (or a process "MyProcess"). Then these commands return nothing on missing targets and at the same time they generate no errors:
有一种有趣的方法可以避免这种错误(它也适用于其他一些像Get-Process这样没有Test-Path替代的cmdlet)。假设我们要检查项目“MyApp.exe”(或进程“MyProcess”)的存在。然后这些命令在缺少目标时不返回任何内容,同时它们不会产生任何错误:
Get-Item "[M]yApp.exe"
Get-Process "[M]yProcess"
These cmdlets do not generate errors for wildcard targets. And we use these funny wildcards that actually match single items.
这些cmdlet不会为通配符目标生成错误。我们使用这些有趣的通配符实际匹配单个项目。
#1
28
Use the command ... get-item blah -ErrorAction SilentlyContinue
使用命令... get-item blah -ErrorAction SilentlyContinue
#2
53
The cmdlet Test-Path
is specifically designed for this, it determines whether items of a path exist. It returns a Boolean value and does not generate an error.
cmdlet Test-Path是专门为此设计的,它确定路径的项是否存在。它返回一个布尔值,不会生成错误。
The cmdlet Get-Item
(and similar) can be used, too, but not directly. One way is already proposed: use -ErrorAction SilentlyContinue
. It might be important to know that in fact it still generates an error; it just does not show it. Check the error collection $Error
after the command, the error is there.
也可以使用cmdlet Get-Item(和类似的),但不能直接使用。已经提出了一种方法:使用-ErrorAction SilentlyContinue。知道事实上它仍然会产生错误可能很重要;它只是没有显示出来。在命令之后检查错误集合$ Error,错误就在那里。
Just for information
仅供参考
There is a funny way to avoid this error (it also works with some other cmdlets like Get-Process
that do not have a Test-Path
alternative). Suppose we are about to check existence of an item "MyApp.exe" (or a process "MyProcess"). Then these commands return nothing on missing targets and at the same time they generate no errors:
有一种有趣的方法可以避免这种错误(它也适用于其他一些像Get-Process这样没有Test-Path替代的cmdlet)。假设我们要检查项目“MyApp.exe”(或进程“MyProcess”)的存在。然后这些命令在缺少目标时不返回任何内容,同时它们不会产生任何错误:
Get-Item "[M]yApp.exe"
Get-Process "[M]yProcess"
These cmdlets do not generate errors for wildcard targets. And we use these funny wildcards that actually match single items.
这些cmdlet不会为通配符目标生成错误。我们使用这些有趣的通配符实际匹配单个项目。