繁体   English   中英

如何通过PowerShell检查用户帐户组是否存在?

[英]How can I check if a User Account group exists through powershell?

像这样的东西:

if(GroupExists("Admins")) // <-- How can I do this line ?
{
    ([ADSI]"WinNT://$_/Admins,group").add($User)
}
else
{
    $Group = Read-Host 'Enter the User Group :'
    ([ADSI]"WinNT://$_/$Group,group").add($User)
}

您可以使用Exists静态方法,如下所示:

[ADSI]::Exists("WinNT://localhost/Administrators")

要获得True / False结果,您可以将其包装到try / catch语句中。

$result = try { [ADSI]::Exists("WinNT://localhost/Administrators") } catch { $False }

或者在if / then语句中,你需要将它包装在$()

if ( $(try {[ADSI]::Exists("WinNT://localhost/Administrators")} catch {$False}) ) {
    write-host "good"
    }
else {
    write-host "bad"
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM