【问题标题】:How to pass the value as a variable into get-mailbox with -identity如何将值作为变量传递到带有-identity的get-mailbox
【发布时间】:2021-08-16 07:18:01
【问题描述】:

我有以下 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'" 

它们都不适合我。如果我使用以下命令,我可以得到我想要的结果
 $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>

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

【问题讨论】:

    标签: powershell exchange-server powershell-2.0 powershell-4.0 exchange-server-2010


    【解决方案1】:

    通过在 ScriptBlock 中将 $mailbox 更改为 $using:mailbox 来设置 $using 范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2021-11-13
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多