我在尝试检索域内的域策略和组织单位 (OU) 时遇到错误。
脚本:
Import-Module GroupPolicy
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$domains = $forest.Domains|ForEach-Object { $_.Name }
foreach ($domain in $domains) {
Write-Host "Domain Name: $domain"
# Connect to the domain
$gpmc = New-Object -ComObject "GPMgmt.GPM"
$gpmcDomain = $gpmc.GetDomain($domain)
$gpos = $gpmcDomain.GetGPOs("All")
foreach ($gpo in $gpos) {
Write-Host " GPO Name: $($gpo.DisplayName)"
}
foreach ($ou in $ous) {
Write-Host " OU Name: $($ou.Name)"
}
}
错误:
这是我遇到的错误。
PS C:\Users\subhavignesh> C:\testing_gpo.ps1
Domain Name: ngmgmt.internal
Cannot find an overload for "GetDomain" and the argument count: "1"
At C:\testing_gpo.ps1:10 char:5
+ $gpmcDomain = $gpmc.GetDomain($domain)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : RuntimeException
You cannot call a method on a null-valued expression.
At C:\testing_gpo.ps1:11 char:5
+ $gpos = $gpmcDomain.GetGPOs("All")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
1)我需要知道如何与GPO连接 2)我需要我的域下的 OU 和 GPO 策略的输出
您收到的错误意味着 GetDomain() 方法存在,但它不只接受一个参数。声明中指出了这一点:
Cannot find an overload for "GetDomain" and the argument count: "1" At C:\testing_gpo.ps1:10 char:5
当我们查看文档时,我们发现该方法需要三个参数 https://learn.microsoft.com/en-us/windows/win32/api/gpmgmt/nf-gpmgmt-igpm-getdomain
在这种特定情况下,该函数始终需要三个参数。当我们只想将域作为参数传递时,我们必须将其他两个参数作为空字符串传递。
$gpmc.GetDomain($domain, '', '')
顺便说一句,当使用Powershell中的自动完成功能(Control + Space)时,它还会告诉你哪些方法需要哪些参数。