Get-ChildItem -force在"我的文档"文件夹和其他联结点上报告"拒绝访问"

Kev*_*in_ 11 powershell acl access-denied

我有一个我写的脚本替换文件.我将params传递给它以获取文件的名称和要搜索的基本位置.工人线是:

$SubLocations = Get-ChildItem -Path $Startlocation -Recurse -include $Filename -Force  | 
                Where { $_.FullName.ToUpper().contains($Filter.ToUpper())}
Run Code Online (Sandbox Code Playgroud)

我将$ Startlocation设置为"C:\ Users",但是,当我试图通过其他用户文件夹进行递归时,我被拒绝访问.我是机器上的完全管理员,我已经尝试过运行PowerShell作为管理员.我可以通过Windows资源管理器访问所有文件,没有任何问题.任何的想法?

Get-ChildItem : Access to the path 'C:\Users\jepa227\Documents\My Music' is denied.
At C:\Users\krla226\Google Drive\Documents\PowerShell\Replace-File.ps1:35 char:46
+ $SubLocations = Get-ChildItem <<<<  -Path $Startlocation -Recurse -    include $Filename -Force | 
    + CategoryInfo          : PermissionDenied: (C:\Users\jepa227\Documents\My     Music:String) [Get-ChildItem], Una 
   uthorizedAccessException
+ FullyQualifiedErrorId :  DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
Run Code Online (Sandbox Code Playgroud)

UPDATE

虽然我无法通过GCI工作,但我能够使用WMI来解决我的问题.对于有兴趣的人:

$SubLocations = Get-WmiObject -Class cim_datafile -Filter "fileName = '$filename' AND Extension = '$extension'" | 
                            Where { $_.Name.ToUpper().contains($Filter.ToUpper()) }
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 6

mcating 的有用答案很好地解释了这个问题。

他建议的快速修复方法是省略-Force,这很有效,因为除非使用 ,否则 PowerShell会忽略隐藏项-Force,并且这些系统定义的连接点确实具有 属性Hidden(以及ReparsePointSystem属性)。

如果您确实需要-Force处理一般隐藏项并且只想忽略这些系统定义的连接点,您可以使用Get-ChildItem-Attributes参数,如下所示:

Get-ChildItem -Force -Recurse -Attributes !Hidden, !System, !ReparsePoint
Run Code Online (Sandbox Code Playgroud)

-Attributes排除设置了以下所有属性的所有项目:HiddenSystemReparsePoint,这对于所有系统定义的连接点都是如此。
虽然在技术上可以使用这些属性创建您自己的连接点(或符号链接),但这在实践中不太可能发生。


mca*_*ing 5

我能够在 Windows 7 机器上重现这一点,使用以下命令以名为“admin”的管理员用户身份登录,以提升的权限运行 powershell,并禁用 UAC:

get-childitem "c:\users\Admin\my documents"
Run Code Online (Sandbox Code Playgroud)

cd "c:\users\admin\my documents"
get-childitem
Run Code Online (Sandbox Code Playgroud)

根据这里的文章,它看起来像我的文档、我的音乐等,被定义为与 pre-Vista 软件向后兼容的连接点。Powershell 本身并不能很好地处理连接点。这里似乎有几个选项:

1) 从 Get-ChildItem 命令中删除 -force。这可能是您最好的选择。

get-childitem c:\users -recurse
Run Code Online (Sandbox Code Playgroud)

正常工作并跳过连接点和系统目录,如 AppData。

编者注:省略-Force确实解决了眼前的问题,但总是会跳过所有隐藏的项目,而不仅仅是导致拒绝访问错误的隐藏连接点。

2)如果您-Force出于某种原因绝对需要使用,您可以以编程方式递归每个子目录,跳过连接点。本文介绍了识别连接点的机制。.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 $_ }
Run Code Online (Sandbox Code Playgroud)

  • 删除 -force 没有帮助,它与连接点无关。我对临时文件有同样的问题。 (2认同)