powershell get-adgroups返回错误的结果

时间:2012-12-13 15:18:40

标签: powershell active-directory

此处get-adgroups在从脚本查询时返回false,而在使用完全相同的PowerShell ISE窗口手动运行时返回true。请参阅以下产生错误的代码。存在组,OU和DN。很可能没有错字。可以通过手动运行命令(参见下面的内容)重现,这样可以正常工作。

Import-Module ActiveDirectory
$Group="ProductInternalInstallProductOnNextLogin"
$BaseDN="OU=Product,DC=int,DC=Domain,DC=de"
write-host "get-adgroup -Filter DistinguishedName -eq CN=$Group,$BaseDN"
$Result=get-adgroup -Filter {(DistinguishedName -eq "CN=$Group,$BaseDN")}
if($Result)
{
    write-host "Group $Group found"
}
else
{
    write-host "Group $Group not found, trying to create $Group"
    New-ADGroup -path "$BaseDN" -GroupScope Global -name $Group
    if (!$?)
    {
        write-host "ERROR creating new group $Group"
        exit
    }
}

这会产生以下输出,您可以在其中看到错误:

____________________________________________________________________________________________________________________________________________________________________________________________________________________
PS C:\Users\MyName.INT> G:\DevPath\Tools\PowerShell-Scripte\Unbenannt2.ps1
get-adgroup -Filter DistinguishedName -eq CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de
Group ProductInternalInstallProductOnNextLogin not found, trying to create ProductInternalInstallProductOnNextLogin
New-ADGroup : Die angegebene Gruppe ist bereits vorhanden
Bei G:\DevPath\Tools\PowerShell-Scripte\Unbenannt2.ps1:13 Zeichen:16
+     New-ADGroup <<<<  -path "$BaseDN" -GroupScope Global -name $Group
+ CategoryInfo          : NotSpecified: (CN=ProductInte...nt,DC=Domain,DC=de:String) [New-ADGroup], ADException
+ FullyQualifiedErrorId : Die angegebene Gruppe ist bereits vorhanden,Microsoft.ActiveDirectory.Management.Commands.NewADGroup

ERROR creating new group ProductInternalInstallProductOnNextLogin

____________________________________________________________________________________________________________________________________________________________________________________________________________________

如果我只运行它,以防该组不存在,New-ADGroup如何失败? PowerShell在德语中运行,因此错误消息“New-ADGroup:Die angegebene Gruppe ist bereits vorhanden”意味着“此组已存在”。

为了验证这一点,我在控制台中手动运行它,它运行良好:

PS C:\Users\MyName.INT> write-host "the following command was run manually from the commandline of the PowerShellISE"
$Result=get-adgroup -Filter {(DistinguishedName -eq "CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de")}
write-host $Result

产生正确的输出:

the following command was run manually from the commandline of the PowerShellISE
CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de

在我的挣扎中我也试过

try {get-adgroups [...]} catch {new-adgroup[...]} 

但这也没有用。

如果我没有看到它,我就不会相信它。

帮助将不胜感激!

吨。

2 个答案:

答案 0 :(得分:2)

您是否尝试在Get-ADGroup命令之外拉出目标组的字符串连接?我实际上能够从PowerShell ISE会话中复制您的问题。当我更新'过滤器'时,它清理了一切,我能够成功检索信息。

原件:

$Group  = "ProductInternalInstallProductOnNextLogin"
$BaseDN = "OU=Product,DC=int,DC=Domain,DC=de"
$Result = get-adgroup -Filter {(DistinguishedName -eq "CN=$Group,$BaseDN")}

修改:

$Group  = "ProductInternalInstallProductOnNextLogin"
$BaseDN = "OU=Product,DC=int,DC=Domain,DC=de"
$Target = "CN=" + $Group + "," + $BaseDN
$Result = get-adgroup -Filter {DistinguishedName -eq $Target}

答案 1 :(得分:0)

你是如何运行powershell.exe的? 从powershell.exe运行脚本与文件与ISE之间存在一些差异,而且我已经厌倦了几次...... 请参阅此StackOverflow post

相关问题