确定用户是否是Powershell中本地组的成员

时间:2015-03-03 21:24:47

标签: powershell

以下是我最好的猜测,即我们的服务帐户是否是IIS_IUSRS本地群组的成员。

两者都不会崩溃,但每个if法规每次都会返回false。我已手动将用户帐户添加/删除到该论坛,并确认这是一个" false"负。

#Connect to Active Directory database using Powershell ADSI  adapter
#Keep this value as 'localhost' to ensure you connect to this computer
$cn = [ADSI]"WinNT://localhost"
$env = "dev"
$monet = "s.Monet.$env"
$periscope = "s.PeriscopeWeb.$env"
$agentData = "s.AgentData.$env"

#add service accounts to IIS_IUSRS group
$iis_iusrs = [ADSI] "WinNT://localhost/IIS_IUSRS"

if (-not $iis_iusrs.memberOf | where { $_ -match $monet})
{
    $iis_iusrs.Add("WinNT://DOMAIN/$monet, user")
}    
if (-not (Get-ADUser $periscope -Properties memberof).memberof -like "CN=IIS_IUSRS*")
{

1 个答案:

答案 0 :(得分:3)

这可能是您正在寻找的内容:

#Get the IIS_IUSRS group
$iis_iusrs = [ADSI]"WinNT://localhost/IIS_IUSRS"
$Members = @($iis_iusrs.psbase.Invoke("Members"))
#Populate the $MemberNames array with all the user ID's
$MemberNames = @()
$Members | ForEach-Object {$MemberNames += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null);}

#See if your user ID is in there
if (-Not $MemberNames.Contains($monet)) {
    $iis_iusrs.Add("WinNT://DOMAIN/$monet, user")
}