Powershell and get-mailbox filter
I am trying to start develop a simple power shell script to do a filter on the get-mailbox command. I've created a variable and tried to use it as a filter, to no avail.(Ultimately, I want to read in the server name into the variable and filter with it.) ButI'm having problems geting started. What am I doing wrong?
[PS] C:\>$a = ' -filter {servername -eq "dc1"}'[PS] C:\>$a-filter {servername -eq "dc1"}[PS] C:\>get-mailbox $aGet-Mailbox : The operation could not be performed because object ' -filter {servername -eq "dc1"}' could notbe found on domain controller 'dc1.litwareinc.com'.At line:1 char:12+ get-mailbox <<<< $a
If I type in the same filter in from the command line it works....
[PS] C:\>get-mailbox -filter {servername -eq "dc1"} | more
WARNING: By default only the first 1000 items are returned. To change the number of items returned, specifythe parameter "-ResultSize". To return all items specify "-ResultSize Unlimited" (Note: Returning all itemsmay take a very long time and consume a large amount of memory depending on the actual number of items). Itis not recommended to store the results in a variable; instead pipe the results to another task or script toperform batch changes.
Name Alias ServerName ProhibitSendQuota---- ----- ---------- -----------------Administrator Administrator dc1 unlimitedJim Hance JiHa dc1 unlimited
Help!
Thanks in advance...
Steve
June 25th, 2008 5:32am
Hi Steve,
Get-Mailbox required static value while defining a parameter and can not accept as a variable sothese give you perfectresult while other not.
get-mailbox-filter {servername -eq "dc1"} | More
get-mailbox -ResultSize Unlimited | where{$_.servername -eq "dc1"}
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2008 11:43am
Are there any list of commands which are not scriptable? or is there any workarounds?
September 23rd, 2010 4:46am
The get-mailbox cmdlet accepts a -server parameter, and you can use a variable to set the parameter value.
$a = "dc1"
get-mailbox -server $a[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, 2010 8:19am
I am trying to start develop a simple power shell script to do a filter on the get-mailbox command. I've created a variable and tried to use it as a filter, to no avail. (Ultimately, I want to read in the server name into the variable and
filter with it.) But I'm having problems geting started. What am I doing wrong?
[PS] C:\>$a = ' -filter {servername -eq "dc1"}'
[PS] C:\>$a
-filter {servername -eq "dc1"}
[PS] C:\>get-mailbox $a
Get-Mailbox : The operation could not be performed because object ' -filter {servername -eq "dc1"}' could not
be found on domain controller 'dc1.litwareinc.com'.
At line:1 char:12
+ get-mailbox <<<< $a
If I type in the same filter in from the command line it works....
[PS] C:\>get-mailbox -filter {servername -eq "dc1"} | more
WARNING: By default only the first 1000 items are returned. To change the number of items returned, specify
the parameter "-ResultSize". To return all items specify "-ResultSize Unlimited" (Note: Returning all items
may take a very long time and consume a large amount of memory depending on the actual number of items). It
is not recommended to store the results in a variable; instead pipe the results to another task or script to
perform batch changes.
Name Alias ServerName
ProhibitSendQuota
---- ----- ----------
-----------------
Administrator Administrator dc1 unlimited
Jim Hance JiHa dc1
unlimited
Help!
Thanks in advance...
Steve
You can use this
[PS] C:\>$a= Get-Mailbox -filter {servername -eq "dc1"}
[PS] C:\>Get-Mailbox $a
December 5th, 2010 7:37pm
On Mon, 6 Dec 2010 00:33:29 +0000, Vministrator wrote:
>I am trying to start develop a simple power shell script to do a filter on the get-mailbox command. I've created a variable and tried to use it as a filter, to no avail. (Ultimately, I want to read in the server name into the variable and filter with
it.) But I'm having problems geting started. What am I doing wrong?
>
>
>
>[PS] C:\>$a = ' -filter {servername -eq "dc1"}' [PS] C:\>$a -filter {servername -eq "dc1"} [PS] C:\>get-mailbox $a Get-Mailbox : The operation could not be performed because object ' -filter {servername -eq "dc1"}' could not be found on domain controller
'dc1.litwareinc.com'. At line:1 char:12 + get-mailbox <<<< $a
You need to build the command and then invoke it:
$a = "-filter.."
$cmd = "get-mailbox $a"
invoke-expression $cmd
You'll have to work out the quotes in the composition of the command.
Each pass through the command interpreter will remove one level of
protection. So instead of a simple "dc1" you may need `"dc1`" or
""""dc1"""" or "'dc1'" to arrive at what you need.
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
December 5th, 2010 9:45pm