【问题标题】:Import-GPO by using folders named after its DisplayName - Powershell使用以其 DisplayName 命名的文件夹导入 GPO - Powershell
【发布时间】:2020-08-22 06:49:36
【问题描述】:

我正在使用 powershell 脚本导入我保存为 GUID(默认)的 GPO。

我希望在不同的系统上导入 GPO,此时 GPO 已保存到 CD 中。因此,导入单个 GPO 的简单方法是:

$domain=[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name

$import_array = get-childitem ($current_drive + $gpo_backup_location) | Select name
foreach ($GPO in $import_array) {
Import-GPO |
-backupgponame $GPO.name -path ($Current_Drive + $gpo_backup_location) |
-domain $domain | 
-Targetname $GPO.name |
-migrationtable ($Current_drive + $mig_table_path) |
-Createifneeded;
}
}

$import_array 命令给出了我的文件夹名称

Name
----------
GPO-1
GPO-2
GPO-3
GPO-4
and so on...

我想遍历这些并导入每一个。使用另一个代码块保存 GPO,以将 GPO 作为其显示名称进行备份。 (GPO-1、GPO-2 等)

我终生无法弄清楚如何强制它查看文件夹名称并导入这些

编辑


备份是使用以下代码创建的:

Function GPO_Backup_DisplayName ($MenuChoice) {
    write 'Backing Up GPOs by Display Name'
    Foreach ($GPO in $GPO_Temp_Backup) {
        $GPOBackup = Backup-GPO `
            -name $GPO.DisplayName `
            -path ($Current_drive + $GPO_Backup_Location) `
            -Domain $domain;
        Get-GPOReport $GPO.DisplayName `
            -reporttype HTML `
            -Domain $domain `
            -path ($Current_drive + $GPO_Backup_HTML_Report + $GPO.DisplayName + ".html");
        Rename-item 
            -path ($Current_drive + $GPO_Backup_Location + "{" + $GPOBackup.Id + "}") `
            -newname $GPO.DisplayName
    }
}

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    如果将每个 GPO 导出到其自己的文件夹并在 GPO 之后命名该文件夹,则可以在 foreach 循环中迭代这些文件夹以导入 GPO 并将目标名称设置为文件夹名称。像这样的东西应该可以工作:

    Get-ChildItem "C:\backup" | % {
      $name = $_.name
      $id = (get-childitem $_) -replace '[{}]', ''
      Import-GPO -BackupId $id -TargetName $name -path $_.Path -CreateIfNeeded
    }
    

    先决条件是您使用以下内容创建了备份:

    Get-GPO -All | % {
      $name = $_.DisplayName
      $dir = New-Item "C:\backup\$name" -Type Directory
      Backup-GPO -Guid $_.Id -Path $dir
    }
    

    此外,备份目录应仅包含具有单个 GPO 备份的文件夹,否则您必须在 Get-ChildItemForEach-Object 之间添加过滤器。

    【讨论】:

    • 你能提供一个更详细的例子吗?我已将其设置为将 GPO 保存为其 DisplayName。这工作正常,但是当我尝试导入这些时,它给了我错误the gpo cannot be imported because a gpo backup that is named "Our Name" was not found at domain.com 我将更新我的问题以支持我的新代码
    • 这只是创建空文件夹,我似乎无法让它正常工作。将名称输出到文本文件会更好吗?我宁愿不这样做我希望有一种方法可以在不使用文本文件的情况下使其工作,但我无法获得它。
    • 那是因为我忘记将New-Item 的结果赋给变量$dir。很抱歉,现在已经解决了。
    【解决方案2】:

    使用 PowerShell 列出组策略对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      相关资源
      最近更新 更多