Get-ChildItem-在My Documents文件夹和其他连接点上强制报告&拒绝访问 [英] Get-ChildItem -force reports "Access Denied" on My Documents folder and other junction points
本文介绍了Get-ChildItem-在My Documents文件夹和其他连接点上强制报告&拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个我编写的替换文件的脚本。我将参数传递给它,以获取文件名和搜索的基本位置。工作行为:
$SubLocations = Get-ChildItem -Path $Startlocation -Recurse -include $Filename -Force |
Where { $_.FullName.ToUpper().contains($Filter.ToUpper())}
我将$StartLocation设置为"C:USERS",然而,当我尝试递归其他用户文件夹时,访问被拒绝。我是机器上的完全管理员,并且我已经尝试以管理员身份运行PowerShell。我可以通过Windows资源管理器访问所有文件,没有任何问题。有什么想法吗?
Get-ChildItem : Access to the path 'C:Usersjepa227DocumentsMy Music' is denied.
At C:Userskrla226Google DriveDocumentsPowerShellReplace-File.ps1:35 char:46
+ $SubLocations = Get-ChildItem <<<< -Path $Startlocation -Recurse - include $Filename -Force |
+ CategoryInfo : PermissionDenied: (C:Usersjepa227DocumentsMy Music:String) [Get-ChildItem], Una
uthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
更新
虽然我无法通过GCI使其工作,但我能够使用WMI来解决我的问题。对于感兴趣的人:
$SubLocations = Get-WmiObject -Class cim_datafile -Filter "fileName = '$filename' AND Extension = '$extension'" |
Where { $_.Name.ToUpper().contains($Filter.ToUpper()) }
推荐答案
我能够在Windows7计算机上使用以下命令在Windows7计算机上重现此文件,该命令以名为"ADMIN"的管理员用户身份登录,以提升的权限运行PowerShell,并禁用了UAC:
get-childitem "c:usersAdminmy documents"
和
cd "c:usersadminmy documents"
get-childitem
根据here这篇文章,My Documents、My Music等似乎被定义为向后兼容Vista之前的软件的连接点。PowerShell本身并不能很好地处理连接点。这里似乎有几个选项:
1)从Get-ChildItem命令中删除-force。这可能是您最好的选择。get-childitem c:users -recurse
工作时没有错误,并跳过连接点和系统目录,如AppData。
编者按:省略-Force
确实可以解决眼前的问题,但始终会跳过所有隐藏的项,而不仅仅是导致访问被拒绝错误的隐藏交叉点。
-Force
,您可以通过编程方式递归每个子目录,跳过连接点。This article描述了识别连接点的机制。.ps1脚本文件中的框架可能如下所示:
Param( [Parameter(Mandatory=$true)][string]$startLocation )
$errorActionPreference = "Stop"
function ProcessDirectory( $dir )
{
Write-Host ("Working on " + $dir.FullName)
# Work on the files in this folder here
$filesToProcess = ( gci | where { ($_.PsIsContainer -eq 0) } ) # and file matches the requested pattern
# process files
$subdirs = gci $dir.FullName -force | where {($_.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0 -and ($_.PsIsContainer -eq 1) -and (![string]::IsNullOrEmpty($_.FullName))}
foreach( $subdir in $subdirs )
{
# Write-Host( $subdir.Name + ", " + $subdir.FullName )
if ( $subdir -ne $null )
{
ProcessDirectory -dir $subdir
}
}
}
$dirs = get-childitem $startLocation -force
$dirs | foreach { ProcessDirectory -dir $_ }
这篇关于Get-ChildItem-在My Documents文件夹和其他连接点上强制报告&拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文