Add a check to a script to tell if a user is in a list of groups or not

Hi I have been working on a script for a while and I have mot things I won working fine. I am trying to add one last bit I previously have fixed in Excel this is to check if a user is a member of 1 one more listed groups. 

The part of my code that is not working is the part that begins with: 

Check if user are a member of one or more of the listed groups, if true mark with "Is in group""

I have tried several things to get the correct data to be exported to my csv file but it usually ends up empty where the "exist" value should be.


----

Add-PSSnapin *exchange* # Exchange 2007 is used
Function Get-Quota
{   Param (
        [string]$Quota,
        [string]$DB
    )
    
    If ($Quota -eq "unlimited")
    {   Return $Database[$DB]
    }
    Else
    {   Return $Quota
    }
}
#Load ActiveDirectory module
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?"; Exit }


$Database = @{}
ForEach ($DB in (Get-MailboxDatabase))
{   $Database.Add($DB.Name,(New-Object PSObject -Property @{
        IssueWarningQuota = $DB.IssueWarningQuota
        ProhibitSendQuota = $DB.ProhibitSendQuota
        ProhibitSendReceiveQuota = $DB.ProhibitSendReceiveQuota
    }))
}
$Results = @()
$Results += ForEach ($User in (Get-ADUser -Filter * -Properties Department,Mail,DistinguishedName,Company,Enabled))
{   $Mailbox = Get-Mailbox $User.Name -ErrorAction SilentlyContinue
    If ($Mailbox)
    {   $Mail = $Mailbox | Get-MailboxStatistics -ErrorAction SilentlyContinue
        If ($Mail.TotalItemSize.Value -eq $null)
        {   $TotalSize = 0
        }
        Else
        {   $TotalSize = $Mail.TotalItemSize.Value.ToBytes()
        }
        New-Object PSObject -Property @{
            Name = $User.Name
            SamAccountName = $User.SamAccountName
            Email = $User.Mail
            Department = $User.Department
            MailboxSize = $TotalSize
			DistinguishedName = $User.DistinguishedName
			CompanyName = $User.Company
			Enabled = $User.Enabled
            IssueWarningQuota = (Get-Quota $Mailbox.IssueWarningQuota $Mailbox.Database).IssueWarningQuota
            ProhibitSendQuota = (Get-Quota $Mailbox.ProhibitSendQuota $Mailbox.Database).ProhibitSendQuota
            ProhibitSendReceiveQuota = (Get-Quota $Mailbox.ProhibitSendReceiveQuota $Mailbox.Database).ProhibitSendReceiveQuota
        }
		
# Check if user are a member of one or more of the listed groups, if true mark with "Is in group"
if ((Get-ADUser $User -Properties memberof).memberof -like "CN=grp1" -or "CN=grp2")
{
$exist = "IS in group"
}
Else
{
$exist = "NOT in group"
}
    }
}
$Results | Select Name,SamAccountName,Email,Department,MailboxSize,IssueWarningQuota,ProhibitSendQuota,ProhibitSendReceiveQuota,CompanyName,DistinguishedName,Enabled,exist | Export-Csv -encoding utf8 c:\Scripts\mydata.csv -NoTypeInformation

July 24th, 2015 8:59am

Since you are using like you will need some wildcard so it would be like 

if (((Get-ADUser $User -Properties memberof).memberof -like "*CN=grp1*") -or (Get-ADUser xy9764 -Properties memberof).memberof -like "*CN=grp2*")

If the start of the string return by memberof start by CN=grp1 you can just use the wild card after



Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 9:41am

Try putting your $exist value into your custom object, for instance as follows:

...
        If ($Mail.TotalItemSize.Value -eq $null)
        {   $TotalSize = 0
        }
        Else
        {   $TotalSize = $Mail.TotalItemSize.Value.ToBytes()
        }
		# Check if user are a member of one or more of the listed groups, if true mark with "Is in group"
		if ((Get-ADUser $User -Properties memberof).memberof -like "CN=grp1" -or "CN=grp2")
		{
		$exist = "IS in group"
		}
		Else
		{
		$exist = "NOT in group"
		}
			}
		}

        New-Object PSObject -Property @{
            Name = $User.Name
            SamAccountName = $User.SamAccountName
            Email = $User.Mail
            Department = $User.Department
            MailboxSize = $TotalSize
			DistinguishedName = $User.DistinguishedName
			CompanyName = $User.Company
			Enabled = $User.Enabled
            IssueWarningQuota = (Get-Quota $Mailbox.IssueWarningQuota $Mailbox.Database).IssueWarningQuota
            ProhibitSendQuota = (Get-Quota $Mailbox.ProhibitSendQuota $Mailbox.Database).ProhibitSendQuota
            ProhibitSendReceiveQuota = (Get-Quota $Mailbox.ProhibitSendReceiveQuota $Mailbox.Database).ProhibitSendReceiveQuota
			Exist = $exist
        }
		
$Results | Select Name,SamAccountName,Email,Department,MailboxSize,IssueWarningQuota,ProhibitSendQuota,ProhibitSendReceiveQuota,CompanyName,DistinguishedName,Enabled,exist | Export-Csv -encoding utf8 c:\Scripts\mydata.csv -NoTypeInformation

wizend

July 24th, 2015 10:16am

BAD SYNTAX: Get-ADUser $User -Properties memberof).memberof -like "CN=grp1" -or "CN=grp2")

[bool]$exists=((Get-ADUser $User -Properties memberof).memberof) |?{$_ -match '^CN=grp1|^CN=grp2'}).Count

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 10:31am

[bool]$exists=((Get-ADUser $User -Properties memberof).memberof) |?{$_ -match '^CN=grp1|^CN=grp2'}),Count

Interesting approach. Fixed your errors:

[bool]$exists=(((Get-ADUser $User -Properties memberof).memberof) |?{$_ -match '^CN=grp1|^CN=grp2'}).Count


July 24th, 2015 11:34am

Thanks Leif

Getting a Boolean and not a collection or string can be an issue.  I think using "Count" works in all

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 11:41am

Since you are using like you will need some wildcard so it would be like 

if (((Get-ADUser $User -Properties memberof).memberof -like "*CN=grp1*") -or (Get-ADUser xy9764 -Properties memberof).memberof -like "*CN=grp2*")

If the start of the string return by memberof start by CN=grp1 you can just use the wild card after



July 24th, 2015 1:36pm

[bool]$exists=((Get-ADUser $User -Properties memberof).memberof) |?{$_ -match '^CN=grp1|^CN=grp2'}),Count

Interesting approach. Fixed your errors:

[bool]$exists=(((Get-ADUser $User -Properties memberof).memberof) |?{$_ -match '^CN=grp1|^CN=grp2'}).Count


Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 3:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics