Solved

Powershell - If statements based on AD Security group memberships

Posted on 2016-11-28
2
8 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 82

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now