【发布时间】:2017-06-28 01:10:31
【问题描述】:
我有一个脚本,可以递归地删除某个目录中的一些文件。问题在于该目录的规范。
当我尝试明确指定它(即仅清除 user1 目录)时,它工作正常:
$temp = "C:\Users\user1\AppData\Local\Temp\"
Get-ChildItem $temp -Recurse -Force -Verbose -ErrorAction SilentlyContinue | remove-item -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
但是,当我使用通配符指定它时(即影响此 PC 上的所有用户)
$temp = "C:\Users\*\AppData\Local\Temp\*"
Get-ChildItem $temp -Recurse -Force -Verbose -ErrorAction SilentlyContinue | remove-item -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
错误提示失败
Get-ChildItem : Access is denied
At line:7 char:1
+ Get-ChildItem $localTempDir -Recurse -Force -Verbose -ErrorAction Sil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand
这怎么可能?
这绝对不是权限问题,因为它是相同的目录。
是的,我以提升的权限运行脚本。
以这种格式指定的其他目录,例如
C:\Users\*\AppData\Local\Microsoft\Windows\Caches
C:\Windows\Temp\*
像魅力一样被净化。
【问题讨论】:
-
嗨,这不能与特殊/隐藏/系统配置文件文件夹有关吗?例如
Default -
也许吧。如何诊断?
-
枚举配置文件,排除特殊配置文件,应用其余逻辑?
标签: powershell directory permissions cmdlet