문제

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