[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1547
  • Last Modified:

where-object does not filter out quilifying values for -notmatch

I could use some help with some syntax.

I have a powershell script that lists all the mailboxes on the server and I want to list all the mailboxes in the stores that have "accounting" in the store name but I do not want to list those in the stores that contain "unlimited" or "unlimited accounting"

This is the select-object statement and $dir is a list of keywords in the stores I want to go thru (accounting is just one).  I want to filter out all the mailboxes with a store containing "unlimited" in the name.  Can anybody help with the syntax?

select-object MailboxDisplayName, Size, StoreName | where-object {($_.Size -gt 51200) -and ($_.StoreName -match $dir -and $_.StoreName -NotMatch "Unlimited")}

Open in new window

0
kjanicke
Asked:
kjanicke
  • 3
2 Solutions
 
LearnctxEngineerCommented:
I don't think you can do a match against an array like you're trying with $dir (assuming that it is an array).
Try this, I don't have anyway to test this with Exchange though.


Select-Object MailboxDisplayName, Size, StoreName | % {
   if ($_.Size -gt 51200){
      foreach ($item in $dir){
         if ($_.StoreName -match $item -and $_.StoreName -notmatch "unlimited"){
            #Do stuff
            #with mailbox
         }
      }
   }
}

Open in new window

0
 
BSonPoshCommented:
I think what your looking for is containment operators.
-contains and -notcontains

If you want to do a regex check you will have to foreach through the array.

I *think* what your after is this
... | where-object {($_.Size -gt 51200) -and ($dir -contains $_.StoreName -and $_.StoreName -NotMatch "Unlimited")}

Open in new window

0
 
kjanickeAuthor Commented:
Thanks for the comments.  I'll try it tomorrow and see which works best.  I tried the containment operators and it didn't quite work but I'll give it another shot.  The containment operator didn't find the values in $dir but I am very good at simple syntax errors.

I'll be sure to respond tomorrow.  Thanks again.
0
 
kjanickeAuthor Commented:
Both solutions worked.  You guys are awesome.
0
 
kjanickeAuthor Commented:
Alas, I spoke too soon.  Changing the syntax to use "contains" instead of "match" does not pull the information from a store where the name is longer than the match value.

If the store is named "accounting a-m," using contains does not pull any mailbox data from this store.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now