如何使用 PowerShell GPO cmdlet 设置文件安全性?

如何使用 PowerShell GPO cmdlet 设置文件安全性?

我需要自动创建几个安全组,然后创建并应用一个包含 OU / 安全组特定文件权限和审核的 GPO。

我已经创建了 OU 和组。我可以将权限/审核作为 ACL 进行,但需要将它们应用为 GPO。我可以链接到 OU/组,并且可以复制现有的 GPO,但我仍然需要替换某些安全组。我确实为每个安全组创建了注册表项,但我找不到有关创建文件权限/审核的任何文档。

案例:我们推出了分配给特定客户的虚拟 Web 服务器。我们有一个 PS 脚本,用于为新服务器创建新的 OU 和安全组。如果需要重新部署 VM,我们不必重新应用带有 ACL 的 PS 脚本,而是希望自动创建包含默认文件夹 ACL 的 GPO,这样 VM 恢复后,我们就不必在重新部署时进行任何操作。

我有 ACL 脚本,它运行良好,但在这种情况下它不适合自动化。

答案1

因此,无法使用 Powershell 直接在 GPO 中设置文件权限。(计算机/策略/Windows 设置/安全设置/文件系统)

但是,我可以通过创建 GPO 并手动备份(一次性操作)来解决这个问题。关于我正在寻找的具体答案,GPO 备份中有 3 个文件需要编辑。

  • {SOME_GUID} \备份.xml
  • {SOME_GUID} \ gpreport.xml
  • {SOME_GUID}\DomainSysvol\GPO\Machine\microsoft\windows nt\SecEdit\GptTmpl.inf

您需要在所有三个文件中用占位符 [GROUP_NAME] 和 [GROUP_SID](也可以是 USER)替换用户组名称和 SID。您还需要将 gpreport.xml 中的 {Name} 标签和 backup.XML 中的 {DisplayName} 更新为 GPO 的新名称。我使用另一个占位符 [GPO_NAME] 执行了此操作。

现在,这是需要花一些时间才能弄清楚的棘手部分。您不能只导入 GPO 这个新对象。您实际上必须创建一个新的空白 GPO,备份它,然后将您从模板更新的文件输出到此备份中。

这是我使用的代码。这里有几个占位符。根据您的环境需要替换它们。看起来 Powershell 脚本上的 Stack 格式有点不对。它按粘贴的方式工作。

#root path for script and files.
$scriptPath = "c:\GPO_Deployment"

# Create new GPO 
write-host "Create new GPO"
new-GPO -Name  [GPO_NAME] 

# Backup New GPO to Named folder
$backup = backup-gpo -Name  [GPO_NAME]   -Path $scriptPath

#

# Files that need to be updated:
#  GPO_TEMPLATE_FILES\Backup.xml
#  GPO_TEMPLATE_FILES\gpreport.xml
#  GPO_TEMPLATE_FILES\DomainSysvol\GPO\Machine\microsoft\windows nt\SecEdit\GptTmpl.inf

# Create Output file strucures

$newXMLpath = New-Item -Path ("{" + $backup.Id + "}") -ItemType Directory -Force
$newGPOinfPath = New-Item -ItemType Directory -Path ("{" + $backup.Id + "}\\DomainSysvol\\GPO\\Machine\\microsoft\\windows nt\\SecEdit") -Force

#get the Group SIDS for the groups we created above
$GROUP_SID =  (New-Object System.Security.Principal.NTAccount("DOMAIN", [GROUP_NAME])).Translate([System.Security.Principal.SecurityIdentifier])

write-host "Applying tranforms to template files"

# read inf_template file, replace sids, and write out
$inf_template = join-path -Path ($scriptPath + "\GPO_TEMPLATE_FILES\DomainSysvol\GPO\Machine\microsoft\windows nt\SecEdit") -ChildPath "GptTmpl.inf"
$inf_outfile = Join-Path -Path $newGPOinfPath -ChildPath "GptTmpl.inf"

(Get-Content $inf_template) | ForEach-Object {
    $_ -replace '\[GROUP_SID\]', $GROUP_SID
} | Set-Content $inf_outfile


# read Backup XML template file, replace sids, and write out
$backup_template = join-path -Path ($scriptPath + "\GPO_TEMPLATE_FILES") -ChildPath "Backup.xml"
$backup_outfile = Join-Path -Path $newXMLpath -ChildPath "Backup.xml"

(Get-Content $backup_template) | ForEach-Object {
    $_ -replace '\[GROUP_SID\]', $GROUP_SID `
        -replace '\[GPO_NAME\]', $hostedclient
} | Set-Content $backup_outfile


# read GPO Report XML template file, replace sids, and write out
$gporeport_template = join-path -Path ($scriptPath + "\GPO_TEMPLATE_FILES") -ChildPath "gpreport.xml"
$gporeport_outfile = Join-Path -Path $newXMLpath -ChildPath "gpreport.xml"

(Get-Content $gporeport_template) | ForEach-Object {
    $_ -replace '\[GROUP_SID\]', $GROUP_SID `
        -replace '\[GPO_NAME\]', $hostedclient
} | Set-Content $gporeport_outfile


Write-Host "Saving updated GPO, linking it to the new OU and moving traget web server to new OU."

# Import GPO
import-gpo -BackupId $backup.Id  -Path $scriptPath -TargetName [GPO_NAME]  -CreateIfNeeded 

$updatedGPO = get-gpo  -Name [GPO_NAME]
# Link GPO to OU 
## NOTE:  If you are updating an existing GPO Link, use Set-GPLink here
New-GPLink -Guid $updatedGPO.Id -Target ("OU=[YOUR_OU],DC=domain,DC=local") -LinkEnabled Yes

# Move web server to OU
get-adcomputer [YOUR_SERVER] | Move-ADObject -TargetPath ("OU=[YOUR_OU],DC=domain,DC=local")


# Add another wait for GPO to settle before forcing update.
Write-Host "Pausing again to allow DC to catch-up again."
start-sleep -seconds 15

write-host "Forcing a GP Update on target webserver."

Invoke-GPUpdate -Computer [YOUR_SERVER] 

答案2

您必须在计算机配置中执行此操作。转到:

计算机配置\策略\Windows 设置\安全设置\文件系统

右键点击右侧空白处,选择“添加文件”。如果你所在的系统没有你想要的文件夹,你可以在这个文件浏览器中创建它。选择文件夹后,分配 NTFS 权限。

相关内容