简体   繁体   中英

Powershell: How to obtain the complete list of distribution groups

I am completely new to powershell, I have never touched this scripting language before. However, I have some backgrounds in perl and bash scripting. I am trying to implement a small script that will obtain the list of DG in Exchange server, filters the results to get only the groups that have a certain string, corresponding to the current year.

Example: check the year, in this case 2011. Filter Name Contains 'P11' Return only the last DG name and parse the first 7 characters.

How could I do this using powershell from an exchange server? Here is what I got:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

# Retrieve all DGs
$temp = Get-DistributionGroup -ResultSize Unlimited | 

foreach($group in $temp) 
{ 
write-output "GroupName:$group " 
Write-output "GroupMembers:" 
Get-DistributionGroupMember $group |ft displayname,alias,primarysmtpaddress 
write-output ‘ ‘ 

}

this results in the following error:

Unexpected token 'in' in expression or statement. At C:\\Users\\jfb\\Desktop\\NewGroupProject.ps1:7 char:18 + foreach($group in <<<< $temp) + CategoryInfo : ParserError: (in:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken

Remove the trailing | in the line $temp = Get-DistributionGroup -ResultSize Unlimited | and it should work fine.

What is happening is that since you had the | it is treating the foreach as a foreach-object

Try this (not tested). Create a date object,using Get-Date, and format the date to include the last two digits of the year enclosed in asterisks. This would be the wildcard for the Get-DistributionGroup cmdlet. Select the last DG object and expand its name.

$name = Get-Date -Format *Pyy*
$group = Get-DistributionGroup $name | Select-Object -Last 1 -ExpandProperty Name

if($group)
{
    $group.Substring(0,7)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM