如何使用 -identity 将值作为变量传递到 get-mailbox

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

我有以下 powershell 脚本

$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>

但是我收到以下错误

  +  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.

我在 get-mailbox 上测试了几种语法组合,其中包括以下内容:

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

None 他们为我工作。如果我使用以下命令,我可以获得我想要的结果
 $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>

但是我不能这样做,因为exchange服务器中没有一个邮箱,所以我必须使它更灵活

你的 $using 范围通过将 $mailbox 更改为 $using:mailbox脚本块。