Saturday, 12 December 2015

Collect Distribution Group where the user memberof


Below script helps in collecting Distribution Group where the user memberof


################################################################
# Scripting for Office365
################################################################

#Script Written by : Kingson Jebaraj

# ==============================================================================================
# Crendentials and Logon
# ==============================================================================================

Get-PSSession | Remove-PSSession
Get-Module tmp* | Remove-Module
$administrator = "Kingson@de-hackers.com"
$password = Get-Content "C:\Scripts\pass.txt" | ConvertTo-SecureString -AsPlainText -force
$credential = New-Object System.Management.Automation.PSCredential $administrator,$password
$Server= "https://outlook.office365.com/powershell-liveid/"
$session = New-PSSession -Authentication basic -Credential $credential –ConnectionUri $Server -Configuration Microsoft.Exchange -AllowRedirection
Import-PSSession $session

# ==============================================================================================
# Get DL to Which user is member of
# ==============================================================================================

$User = Read-Host "Enter input here"
$user_dn = (Get-Mailbox $user).distinguishedname
Write-Host "Please wait this might take some time to process....."
"User" + $User + " is a member of the following groups:"
foreach ($group in Get-DistributionGroup -resultsize unlimited){
if ((get-distributiongroupmember $group.identity | select -expand distinguishedname) -contains $user_dn){$group.name}
}

No comments:

Post a Comment

Google+ Followers

Blog Archive