Вопрос

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:

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

To get a True/False result you can wrap into try/catch statement.

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

or in a if/then statement you'll need to wrap it inside a $()

if ( $(try {[ADSI]::Exists("WinNT://localhost/Administrators")} catch {$False}) ) {
    write-host "good"
    }
else {
    write-host "bad"
    } 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top