Solved

Powershell - If statements based on AD Security group memberships

Posted on 2016-11-28
2
25 Views
Last Modified: 2016-11-28
Good Day,

I have a powershell script and I am trying to pass different IF statements based on the currentuser's group membership.

$CurrentUser = $env:USERNAME
$Authorized = get-aduser -Identity $CurrentUser

IF ( (($Authorized).memberof -ne 'GROUP-A' ) -or (($Authorized).memberof -ne 'GROUP-B' )) {
      Write-Host "Unauthorized User - Exiting..."
      Write-Host ""
      PAUSE
      EXIT
}


Then later in the script I call:

If (($Authorized).memberof -eq 'GROUP-A'){
            #Total Size of Archived Directory
            Write-Output ""
            Write-Host ""
            Write-Output ""
            Write-Host ""
}

This last section does not ever provide the correct output.  The $Currentuser is a member of multiple groups and I want this last section to run if the $CurrentUser is a member of this AD security group which this member is.

Thank you in advance for your help.
0
Comment
Question by:mrfite
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41904369
- The memberOf attribute is not returned by default
- You're trying to compare a string scalar against an array.

* To test whether a single string is contained in an array, use the -contains operator
* To test whether at least one string is contained in both arrays, you can use Compare-Object
$ADUser = Get-ADUser -Identity $ENV:UserName -Property memberOf
$Membership = $ADUser.memberOf | ForEach-Object {($_ -split '(?:\A|,)CN=|,OU=', 3)[1]}
$AllowGroups = @(
	'Group-A'
	'Group-B'
	'Domain Admins'
)
If (-not (Compare-Object -ReferenceObject $AllowGroups -DifferenceObject $Membership -ExcludeDifferent -IncludeEqual)) {
	# Unauthorized
	# ...
}

If ($Membership -contains 'Group-A') {
	# Member of Group-A
	# ...
}

Open in new window

0
 

Author Closing Comment

by:mrfite
ID: 41904582
Thank you! - This worked just like you said it would.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question