相关疑难解决方法(0)

检查项目是否存在而不存在错误(如果该项目不存在)

我想使用PowerShell来检查是否存在IIS Web应用程序(或者可能是其他类型的项目).我可以这样做Get-Item,但是如果项目不存在则报告错误,这会误导运行脚本的用户 - 当实际一切都很好时,看起来出了问题.

如何在没有错误的情况下执行此操作?

powershell

30
推荐指数
2
解决办法
3万
查看次数

PowerShell计数不适用于单个匹配

我正在运行以下PowerShell代码以在收件箱中检索匹配项,并且想知道为什么对于只有一个匹配项的实例,我的过滤器不起作用。这是查找和过滤匹配项的代码;

 Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
 $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
 $outlook = new-object -comobject outlook.application 
 $namespace = $outlook.GetNameSpace("MAPI") 
 $inbox = $namespace.getDefaultFolder($olFolders::olFolderInBox) 
 $filter = (%{$inbox.items | Where {$_.SenderName -match ‘JoeUser’ -and
 $_.UnRead -eq $true}})
Run Code Online (Sandbox Code Playgroud)

...当我通过运行$ filter.count来请求匹配项计数时,只要有多个匹配项,我就会得到正确的答案。因此,对于收件箱中只有一条匹配的消息的情况,$ filter.count不会返回任何内容,并且后续代码也无法对该消息进行处理-我知道该匹配项提取了匹配的消息,因为我可以从$ filter查看

谁能弄清楚为什么这个计数在$ filter的单个匹配中不起作用?

powershell

1
推荐指数
2
解决办法
1460
查看次数

标签 统计

powershell ×2