Powershell 调用命令操作错误



我被这个问题难住了。

我编写了一个Powershell脚本,我正在尝试使用它来跨多个域导入GPO,然后将其与new-gplink链接。我已经确保所有服务器都安装了GP Powershell模块,到目前为止它一直运行良好,但是我遇到的问题是,在某些服务器上,我的命令在其他服务器上工作正常,我收到错误,在最后一步我得到一个操作错误我的调用命令之一。其他命令使用invoke命令(例如get-service)在同一服务器上工作,甚至是我使用的import-GPO命令。

有问题的错误:

An operations error occurred. (Exception from HRESULT: 0x80072020)
    + CategoryInfo          : NotSpecified: (:) [New-GPLink], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.GroupPolicy.Commands.NewGPLinkCommand
    + PSComputerName        : 10.0.0.10

命令:

Invoke-Command -ComputerName $serverip -scriptblock {New-GPLink -Name "GPO" -Target $args[0]} -ArgumentList $oupath -credential $cred

我已经尝试了我能想象到的这个命令的每个版本。 没有 [0],没有参数列表,只是使用服务器 ip 并将目标替换为 OU 路径,我仍然收到相同的错误,如下所示。

Invoke-Command -ComputerName $serverip -scriptblock {New-GPLink -Name "GPOName" -Target ou=users,ou=site,ou=domain,dc=server,dc=com} -ArgumentList $oupath -credential $cred

我让它工作的方式是一个带有服务器信息的.csv,它被导入到 foreach 循环中,然后输入到脚本中。我让它获取凭据并馈送。我知道其他一切都在工作,因为我导入 GPO 的调用命令都有效,我运行的所有服务器都成功导入了 GPO。我也知道我的 OU 路径是正确的,因为我在本地将它们与另一个脚本一起使用,以将计算机放置在我想要的位置。CSV 中的示例行类似于

服务器名称, 10.0.0.10, domain.com, OU=

用户,OU=站点,DC=域,DC=com

我还在本地运行了该命令并收到类似的错误:

PS> New-GPLink -Name "GPO" -Target "ou=users,ou=Site,dc=domain,dc=com"
New-GPLink : A referral was returned from the server.
At line:1 char:1
+ New-GPLink -Name "GPO" -Target "ou=users,ou=site,dc=domain,d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-GPLink], DirectoryServicesCOMException
    + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMException,Microsoft.GroupPolicy.Commands.NewGPLinkCommand

如果有其他问题或需要其他信息,请告诉我。我完全被这个问题难住了,感谢您提供的任何帮助。提前谢谢。

编辑:我所有的服务器都至少是2008 R2,并且使用Powershell版本3,0,1,1

PS> $psversiontable.psversion
Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1

您需要指定尝试在其中应用 GPO 的域,以及相关域中分别具有-Domain-Server参数的域控制器:

$OU = "ou=users,ou=Site,dc=domain,dc=com"
New-GPLink -Name "GPO" -Target $OU -Server "domain.com" -Domain "domain.com"

但是,正确的方法不是仅使用域名,而是实际找到域控制器,如下所示:

$DC = Get-ADDomainController -Discover -DomainName "domain.com" |Select -ExpandProperty HostName
New-GPLink -Name "GPO" -Target $OU -Server $DC -Domain "domain.tld"

或者在Get-ADDomainController不可用的环境中,可以使用 .NET 模拟 DCLocator(也称为 AD DS 的基础高可用性设计)行为:

$DomainFqdn = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$dctx = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "Domain",$DomainFqdn
$DomainController = $[System.DirectoryServices.ActiveDirectory.DomainController]::FindOne($dctx)
New-GPLink -Name "GPO" -Target $OU -Server $DomainController.Name -Domain $DomainFqdn

最新更新