powershell counting mailboxes

I have this script that I wrote which is functional. Problem is it runs on the entire organization each time it runs a get-mailbox to get the count for those servers. I am trying to count all users who are on servers which contain these names

contoso1
contoso2
contoso22
contoso44
 total mailbox count

 Is there a way to filter and say if it contains this then store in variable and move next,if it contains this then store in this variable. So when it finishes it can display the total amount of users on those servers.

$all = get-mailbox -resultsize:unlimited

 

$all | foreach-object {

 

 

$contoso = ($all | where {$_.servername -like "*contoso*"}).count

$microsoft = ($all | where {$_.servername -like "*micro*"}).count

$linux = ($all| where {$_.servername -like "*lin*"}).count

$otherusers = ($all | where {$_.servername -like "*other*"}).count

 

WRITE-HOST "THERE ARE $contoso users on the contoso servers"

WRITE-HOST "THERE ARE $microsoft users on the microsoft servers"

WRITE-HOST "THERE ARE $linux users on the linux servers"

WRITE-HOST "THERE ARE $otherusers users on the other servers"

 

}

Open in new window

techdriveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
How about this:

$servers=get-mailboxserver
foreach ($server in $servers) {
  $mailboxes = get-mailbox -server $server -resultsize unlimited
  write-host "$server contains $($mailboxes).count"
}

Open in new window

techdriveAuthor Commented:
thanks sirbounty but this only gives a count of all the servers but I need a total of each set of servers EX below


contoso1
contoso2
contoso3
contoso4
total for this

microsoft1
microsoft2
microsoft5
microsoft6
total for this

linux1
linux2
linux3
linux4
linux6
total for this

other1
other2
other4
other5
total for this

write-host the total for $contoso is
write-host the total for $micrsoft is
write-host the total for $Linux is
write-host the total for $other is
Minecraft_ EndermanCommented:
This script will report the mailbox count on each server.

$exchservers = Get-Exchangeserver
foreach($exchserver in $Exchservers)
{
get-mailbox -Server $exchserver.name | Group-Object Database | Select-object Count,Name,Databasesize,Lastfullbackup
$exchserver.name
}

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

techdriveAuthor Commented:
thank you Kiss Sky but I do not need this for each individual servers but for a group of servers like.

contoso123
contose54
contoso321
contoso87
contoso012
total mailbox count for contoso servers are
all these servers mailbox count added together because they are alike hence the *conto" like above.

samething with Linux and mciroosft
Mauro CazabonnetSenior .NET Software EngineerCommented:
Haven't tested but the logic is what I think your looking for

$pattern = "contoso|micro|lin|other"
$all = get-mailbox -resultsize:unlimited
$all | foreach-object {
    $servername = $_.servername
    if ($servername -match $pattern)
    {
        if ($servername -like "*contoso*"){
            $contoso = ($all | where {$_.servername -like "*contoso*"}).count
            write-host "THERE ARE $contoso users on the contoso servers"
        }
        if ($servername -like "*micro*"){
            $microsoft = ($all | where {$_.servername -like "*micro*"}).count
            write-host "THERE ARE $microsoft users on the microsoft servers"
        }
        if ($servername -like "*lin*"){
            $linux = ($all| where {$_.servername -like "*lin*"}).count
            write-host "THERE ARE $linux users on the linux servers"
        }
        if ($servername -like "*other*"){
            $otherusers = ($all | where {$_.servername -like "*other*"}).count
            write-host "THERE ARE $otherusers users on the other servers"
        }
    }    
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techdriveAuthor Commented:
thanks mcazabonnet
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.