Get-Mailbox command issues
This may sound very basic, but for some reason I can't get this to work... Doing the get-mailbox -database "IS 2" command by itself returns all the results you would expect. If I do the below command then just type $mailboxes, I get the same
results.
$mailboxes = get-mailbox -database "IS 2"
Then if I try to do the following it returns nothing! I do this all the time, what is wrong!!!???
$mailboxes.name returns nothing
$mailboxes.alias returns nothing
$mailboxes.displayname returns nothing
I'm lost...
Thanks!
Tony
September 23rd, 2011 11:40am
Hello
Try this.
foreach($mailbox in $mailboxes}{ $mailbox.Name }
The reason your command is not working is that $mailboxes is a collection of mailbox objects. The collection does not have a name or alias or displayname property. Only the mailbox object itself does.
If you want those three attributes on one line you can use this command:
foreach($mailbox in $mailboxes}{ $mailbox | select name,alias,displayname}
Bill
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2011 12:01pm
For listing single properties you can also use:
$mailboxes | select -expand name[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
September 23rd, 2011 12:11pm
That worked, but that was only part of what I was trying to do and it still won't work. The whole project is to find certain mailboxes and hide them from the address book. below is what I tried and it doesn't work.
$mailboxes = get-mailbox -database "IS 2"
foreach ($mailbox in $mailboxes) {if ($mailbox.Name -contains "Secure") {set-mailbox -hiddenfromaddresslistsenabled $true}}
IT seems like this should work, but it doesn't...
Thanks!
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2011 12:15pm
-contains is an array operator. It is used to determine if an array of objects contains a particular object. For string comparisons, use -like or -match:
$mailboxes = get-mailbox -database "IS 2" |
foreach ($mailbox in $mailboxes) {if ($mailbox.Name -match "Secure") {set-mailbox -hiddenfromaddresslistsenabled $true}}[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
September 23rd, 2011 12:29pm
So weird, I still can't get it to work! I am doing it exactly like above and it doesn't set the flag for those accounts to hidden!
Thanks for the insight that -contains only applies to arrays!
I now realize why some of the other scripts I tried using it on wouldn't work...
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2011 1:04pm
AH, I got it! I needed to add -identity $mailbox in the set-mailbox command.
Thanks for the help, was driving me nuts!
September 23rd, 2011 1:09pm
I just realized there's another bit missing from that script - the identity of the mailbox to set:
$mailboxes = get-mailbox -database "IS 2" |
foreach ($mailbox in $mailboxes) {if ($mailbox.Name -match "Secure") {set-mailbox
$mailbox -hiddenfromaddresslistsenabled $true}}
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2011 1:12pm
On Fri, 23 Sep 2011 15:34:27 +0000, tlitterio wrote:
>
>
>This may sound very basic, but for some reason I can't get this to work... Doing the get-mailbox -database "IS 2" command by itself returns all the results you would expect. If I do the below command then just type $mailboxes, I get the same results.
>
>$mailboxes = get-mailbox -database "IS 2"
>
>Then if I try to do the following it returns nothing! I do this all the time, what is wrong!!!???
>
>$mailboxes.name returns nothing $mailboxes.alias returns nothing $mailboxes.displayname returns nothing
>
>I'm lost...
The variable "$mailboxes" is an array, not a single mailbox.
$mailboxes[0].name would work for the 1st mailbox in the array.
$mailboxes | foreach {$_.name} would work, too, showing the name for
each mailbox in the array.
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
September 23rd, 2011 10:01pm