代码:
$cPstFiles = Get-ChildItem -Path $env:USERPROFILE -File -Force `
-Recurse -Include "*.pst" -ErrorAction "SilentlyContinue"例外:
Get-ChildItem : Access is denied
At test.ps1:172 char:22
+ $cPstFiles = Get-ChildItem -Path $env:USERPROFILE -File -Force `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand上下文:WindowsV4.0/.NET 4.5.2/ PowerShell 7。
这怎么可能呢?我怎样才能让SilentlyContinue做它应该做的事情呢?
可能值得一提的是,脚本并不总是失败的。它在一些机器上失败了,但在另一些机器上工作得很好,即使在所有这些机器上Windows都是使用相同的镜像部署的。
发布于 2016-07-20 18:05:04
$ErrorActionPreference = “silentlycontinue”
$Extension = "*pst"
$TargetFolder = "$env:userprofile"
Get-Childitem $TargetFolder -Include $Extension -Recurse 发布于 2016-07-20 19:56:59
尝试
Set-ExecutionPolicy Remotesigned最初PS没有在系统环境上执行脚本的权限,因此将其设置为ResmoteSigned并以管理员权限运行可以解决此问题。
发布于 2016-07-20 17:23:03
只需将其包含在try {}块中,并为此类硬错误添加一个catch {}例程即可。
哦,我刚刚看到,ErrorAction应该只是SilentlyContinue,而不是"SilentlyContinue“字符串。
https://stackoverflow.com/questions/38477236
复制相似问题