【问题标题】:Powershell script forPowershell 脚本
【发布时间】:2020-06-03 05:21:57
【问题描述】:

我有 Windows Server 2016 Datacenter(64 位)作为文件服务器(包含多个共享文件夹和子文件夹)。

我想创建一个列表或导出用户文件夹结构以及权限(读取、修改、完整......等)

我尝试使用下面的 PS 脚本,但收到一条错误消息,我在脚本之后提到过。

Powershell

$FolderPath = dir -Directory -Path "E:\Project Folders\#Folder_Name" -Recurse -Force 
$Report = @() 
Foreach ($Folder in $FolderPath) {     
    $Acl = Get-Acl -Path $Folder.FullName     
    foreach ($Access in $acl.Access)
    {
        $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
        $Report += New-Object -TypeName PSObject -Property $Properties
    }
} 
$Report | Export-Csv -path "C:\Folder Permissions\Folder Name.csv" 

错误:

dir : 拒绝访问路径 'E:\Project Folders**Folder Path**\New folder'。在 C:\Users\Administrator\Documents\PS 脚本**文件名**.ps1:1 char:15 + ... oldPath = dir -Directory -Path "E:\Project Folders**Folder Name**" -回复 ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (E:\Project Fold...ngar\New folder:String) [Get-Child Item], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

请帮帮我!

提前致谢

【问题讨论】:

  • 您无权访问其中一个文件夹。您是否按原样复制/粘贴错误消息?它真的包含**Folder Path****Folder Name****File Name**
  • 错误,PermissionDenied 表示运行脚本的帐户无权访问文件夹路径。是否被特权账户调用?
  • 我没有复制确切的路径和文件名,我已经修改了。
  • 我已使用管理员登录。

标签: windows powershell


【解决方案1】:

正如其他 cmets 所指出的那样。

这不是 PowerShell 错误/问题,而是权限问题。如果您说您在 Windows 文件夹树上执行此用例,则可能/将会发生同样的事情。

既然您知道这会发生,请修复您正在处理的树的权限或执行此操作。

Get-ChildItem -Directory -Path 'C:\Windows\System32' -Recurse -ErrorAction SilentlyContinue

或者如果您想在路径失败时停止。

# Treat non-terminating erros as terminating
$RootFolderUnc = 'C:\Windows\System32'

Try {Get-ChildItem -Directory -Path $RootFolderUnc -Recurse -ErrorAction Stop}
Catch [System.UnauthorizedAccessException] 
{
    Write-Warning -Message "$env:USERNAME. You do not have permissions to view this path."
    $_.Exception.Message
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多