簡體   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