Reputation: 4455
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
Upvotes: 1
Views: 15336
Reputation: 126902
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)
}
Upvotes: 1
Reputation: 301477
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
Upvotes: 2