Function — Check If User In Group


function Check_If_User_In_Group ($group,$user)
{
  try
  {
    try
    {
      Import-Module ActiveDirectory -ErrorAction Stop
    }
    catch
    {
      Write-Host "Active Directory module loaded failed.. Please check if the Active Directory Tool is installed" -ForegroundColor Red
    }

    try
    {
      $groupresult = Get-ADGroup -Identity $group -ErrorAction Stop
    }
    catch
    {
      Write-Host "The group is not found.. Check the spelling" -ForegroundColor Red
    }
    try
    {
      $userresult = Get-ADUser -Identity $user -ErrorAction Stop
    }
    catch
    {
      Write-Host "The user is not found...Check the spelling" -ForegroundColor Red
    }
    if (($groupresult -ne $null) -and ($userresult -ne $null))
    {
      $UserGroups = (Get-ADPrincipalGroupMembership $user | Select-Object name).Name
      if ($group -in $UserGroups)
      {
        return $true
      }
      else
      {
        return $false
      }

    }

  }
  catch {}
}

$CompareResult = Check_If_User_In_Group '#EA-SH-SuperAdmin' ftx035
$CompareResult
本文链接: https://www.pstips.net/function-check-user-group.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注