导出和导入文件夹结构和rigts与CSV文件使用PS脚本

xkrw2x1b  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(119)

我是全新的ps脚本和一些建议,请寻找。
我们每隔几年就更换一个数据共享服务器,手工创建完整的文件夹结构和权限是非常繁琐的,所以我尝试用powershell脚本来自动化它,因为我是新来的,我一直在谷歌上搜索一些例子和片段,并从中编译我需要的东西。
我的导出脚本读取文件夹结构并将其写入文本文件,而我的导入脚本在我将文件夹移动到新服务器后创建它,没有问题。问题在于访问权限。它读取权限并将其写入CSV,但一旦我尝试导入它,我就会得到一个错误:
新对象:无法将“FileSystemControlRule”的值为“TRUE”的参数“2”转换为类型“System.Security. SecurityControl. SecurityControlType”:“无法将值“TRUE”转换为类型“System.Security. SecurityControl. SecurityControlType”。错误:“无法将标识符名称TRUE与有效的枚举器名称匹配。请指定以下枚举器名称之一,然后重试:Allow,Deny””第1行,第23个字符

  • . cnView Rule = new-object System.Security. CnView Control.FileSystemAccess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

字符串

  • CategoryInfo:InvalidOperation:(:)[New-Object],MethodException
  • FullyExclusivedErrorId:ConstructorExclusivedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

据我所知,它正在寻找一个允许/拒绝,而不是一个真/假,但导出给出了一个真/假。所以我猜有什么问题与我的导出.
这是我的代码,如果有人能指出我在正确的方向,我会非常感激!!(让我知道,如果我应该张贴所有的代码,我只是不想杂乱任何比我已经做的:D)

导出:

$FolderPath = dir -Directory -Path $DriveLetter -Force
$Report = @()
Foreach ($Folder in $FolderPath)
    {
        if ($Folder.Name -notlike '*$RECYCLE.BIN*')
        {
            
            if ($Folder.Name -notlike '*System Volume Information*')
            {          
                $Acl = Get-Acl -Path $Folder.FullName
                foreach ($Access in $acl.Access)
                {                        
                    $Properties = [ordered]@{'FolderName'=$Folder.Name;'IDRef'=$Access.IdentityReference;'FSRights'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
                    $Report += New-Object -TypeName PSObject -Property $Properties                            
                }
            }
            
        }
            
    }
$Report | Export-Csv -path $ExportACL -NoTypeInformation

导入:

foreach ( $LItem in $ACL_Imp ) 
    {        
        $path_full = $Drivepath.ToString() + $LItem.FolderName
        $ACL_Set = Get-Acl $path_full
        $permission = $LItem.IDRef, $LItem.FSRights, $LItem.Inherited
        $accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $permission <<<--- Error occurs here
        $ACL_Set.SetAccessRule($accessRule)        
        $ACL_Set | Set-Acl $path_full
    }


导出csv中的一个用户的示例(我删除了驱动器号,因为它并不总是相同的驱动器号。)

TYPE System.Management.Automation.PSCustomObject; FolderName;IDRef;FSRights;Inherited Data\UserA;Domain\UserA;FullControl; TRUE Data\UserA;NT权限\SYSTEM;FullControl;TRUE Data\UserA;DOMAIN\UserB;FullControl;TRUE Data\UserA;BUILTIN\Administrators;FullControl;TRUE Data\UserA;DOMAIN\GRP_A; ReadAndExecute,TRUE;TRUE Data\UserA;Domain\GRP_A;ReadAndExecute,TRUE;TRUE

再次感谢您的帮助!如果您不能提供任何帮助,感谢您花时间查看!!:)

lmvvr0a8

lmvvr0a81#

我已经改变了我导出和导入的变量的数量,这似乎可以做到这一点。(导出所有变量,只使用5个)
我张贴我的完整代码的情况下,其他人也想使用这个,或想修改他们的需要:)
希望这将有助于在未来的人,我的评论是有意义的。

Our Directory structure:
ImportExport <-- Location of scripts and output files (whole folder to be copied to new server)
Shared
Software
Users/UserA/
Users/UserB/
....
Users/UserZ/

字符串

导出:

#Variables
$drivepath = Get-Location                                                        #Get working drives' letter
$DriveLetter = Split-Path -qualifier $drivepath
$ExportACL = $DriveLetter.ToString() + "\ImportExport\export_acl.csv"            #ACL Location
$ExportFolders = $DriveLetter.ToString() + "\ImportExport\export_folders.txt"    #File with List of Folders
$UsersPath = $DriveLetter.ToString() + "\Users"                                  #"Users" folders location on working drive located in Data folder

#Read user access levels on each folder on working drive and write to file.
cd.. #<-- add this if script is not run from within the PS environment.
$FolderPath = dir -Directory -Path $DriveLetter -Force
$Report = @()
Foreach ($Folder in $FolderPath)
    {
        if ($Folder.Name -notlike '*$RECYCLE.BIN*')
        {
            if ($Folder.Name -notlike '*ImportExport*')
            {
                if ($Folder.Name -notlike '*System Volume Information*')
                {          
                    $Acl = Get-Acl -Path $Folder.FullName
                    foreach ($Access in $acl.Access)
                    {                        
                        $Properties = [ordered]@{'FolderName'=$Folder.Name;'FSRights'=$Access.FileSystemRights;'ACType'=$Access.AccessControlType;'IDRef'=$Access.IdentityReference;'Inherited'=$Access.IsInherited;'IFlags'=$Access.InheritanceFlags;'PFlags'=$Access.PropagationFlags}
                        $Report += New-Object -TypeName PSObject -Property $Properties                            
                    }
                }
            }
        }            
    }
$Report | Export-Csv -path $ExportACL -NoTypeInformation

#Read user access levels on each child folder of Users folders on working drive and add to file.
$FolderPath = dir -Directory -Path $UsersPath -Force
$UserReport = @()
Foreach ($Folder in $FolderPath)
    {
        if ($Folder.Name -notlike '*$RECYCLE.BIN*')
        {
            if ($Folder.Name -notlike '*ImportExport*')
            {
                if ($Folder.Name -notlike '*System Volume Information*')
                {          
                    $Acl = Get-Acl -Path $Folder.FullName
                    foreach ($Access in $acl.Access)
                    {   
                        $StrFolderPath = $Folder.Parent.ToString() + "\" + $Folder.BaseName
                            
                        $Properties = [ordered]@{'FolderName'=$StrFolderPath;'FSRights'=$Access.FileSystemRights;'ACType'=$Access.AccessControlType;'IDRef'=$Access.IdentityReference;'Inherited'=$Access.IsInherited;'IFlags'=$Access.InheritanceFlags;'PFlags'=$Access.PropagationFlags}
                        $UserReport += New-Object -TypeName PSObject -Property $Properties                                                                                    
                    }
                }
            }
        }            
    }
$UserReport | Export-Csv -append $ExportACL

#Read Directory Structure and Export to File
$Dirs = Dir -Directory -Path $DriveLetter -Force
foreach($Dir in $Dirs)
    {
        if ($Dir.Name -notlike '*$RECYCLE.BIN*')
        {
            if ($Dir.Name -notlike '*ImportExport*')
            {
                if ($Dir.Name -notlike '*System Volume Information*')
                {          
                    $Dir.Name | out-file -Append $ExportFolders
                }
            }
        }
    }
$Dirs = Get-ChildItem -Path $UsersPath -Force
foreach($Dir in $Dirs)
    {
        $DirName = "Users\" + $Dir.Name
        $DirName | out-file -Append $ExportFolders
    }


在导入之前,我在excel中打开csv文件并将其设置为不同的列,因此我使用“;”作为后缀,如果不编辑它,我会很挣扎。具有多个值的变量用“,”分割,这会搞乱导入。
此外,由于管理员权限需要应用ACL,脚本需要在提升的PS环境中运行,无法弄清楚如何在外部执行。可能可以运行批处理文件来执行RunAs,但我暂时有足够的脚本。:p

导入:

#Variables
$drivepath = Get-Location                                                        #Get working drives' letter
$DriveLetter = Split-Path -qualifier $drivepath                                  
$ImportACL = $DriveLetter.ToString() + "\ImportExport\export_acl.csv"            #ACL Location
$ACL_Imp = Import-Csv $ImportACL -Delimiter ';'                                  #Import ACL
$ImportFolders = $DriveLetter.ToString() + "\ImportExport\export_folders.txt"    #File with List of Folders
$UsersPath = $DriveLetter.ToString() + "\Users"                                  #Users' folder location on working drive

#Create Folders from text file.
Get-Content $ImportFolders | %{mkdir "$DriveLetter\$_"}

#Apply ACL to created Folder structure
foreach ( $LItem in $ACL_Imp ) 
    {
        $path_full = $Drivepath.ToString() + $LItem.FolderName
        $ACL_Set = Get-Acl $path_full
        
             
        $permission = $LItem.IDRef, $LItem.FSRights, $LItem.IFlags, $LItem.PFlags, $LItem.ACType       
                    
        $accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $permission
       
        $ACL_Set.SetAccessRule($accessRule)
        
        $ACL_Set | Set-Acl $path_full
    }

相关问题