[英]How can I check if a User Account group exists through powershell?
Something like this : 像这样的东西:
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)
}
You can use the Exists static method as follows: 您可以使用Exists静态方法,如下所示:
[ADSI]::Exists("WinNT://localhost/Administrators")
To get a True/False result you can wrap into try/catch statement. 要获得True / False结果,您可以将其包装到try / catch语句中。
$result = try { [ADSI]::Exists("WinNT://localhost/Administrators") } catch { $False }
or in a if/then statement you'll need to wrap it inside a $()
或者在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.