powershellexchange-serverpowershell-2.0powershell-4.0exchange-server-2010

How to pass the value as a variable into get-mailbox with -identity


I have the following powershell script

$mailbox = "testmail"

$i=invoke-command -Session $session -ScriptBlock { get-mailbox -identity $mailbox  | get-mailboxpermission | Select-Object Identity,User,AccessRights,ExtendedRights } -HideComputerName  | Where-Object {($_.User -like '*1023') -and ($_.AccessRights -like 'FullAccess*')}
write-host "The I values :"$i <br>

But I am getting the following error

  +  get-mailbox -identity $mailbox | get-mailboxpermission | Select-Obje ...
+                        ~~~~~~~~
A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, 
$PSUICulture, $true, $false, $null.

I tested several combination on the syntax on the get-mailbox which included the following :

 get-mailbox -identity '$mailbox'
 get-mailbox -identity "'$mailbox'" 

None of them work for me. If I used the following command, I am able to get the result that I want
 $i=invoke-command -Session $session -ScriptBlock { get-mailbox -identity testmail  | get-mailboxpermission | Select-Object Identity,User,AccessRights,ExtendedRights } -HideComputerName  | Where-Object {($_.User -like '*1023') -and ($_.AccessRights -like 'FullAccess*')} <br>

But I cant do it this way because there are not one mail box in the exchange server, so I must make it more flexible


Solution

  • Thy the $using scope by changing $mailbox to $using:mailbox within the ScriptBlock.