Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<#
.SYNOPSIS
Check if user belongs to an active directory group.
.DESCRIPTION
The Get-IsGroupMember.ps1 function will check if a user belongs
to a specified group.
.EXAMPLE
.\Get-IsGroupMember.ps1 -user johnsmith -group accounting
#>
Param(
#User
[Parameter( Mandatory=$True, HelpMessage="user", Position=0 )]
[ValidateNotNullOrEmpty()][string]$user
, #Group
[Parameter( HelpMessage="Group Name", Position=1 )]
[ValidateNotNullOrEmpty()][string]$group
)
function Get-IsGroupMember{
$user = $user.ToLower()
Write-Output "------ Variable Values ------"
Write-Output "`$user: $user"
Write-Output "`$group: $group"
$strFilter = "(&(objectClass=Group)(name=" + $group +"))"
Write-Output "`$strFilter: $strFilter"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = $strFilter
$objSearcher.PageSize = 500
$objSearcher.SearchScope = "Subtree"
$objItem = $objSearcher.FindOne().Properties
return ([string]$objItem.member).contains($user)
}
Get-IsGroupMember