调用命令问题 [英] Problems with Invoke-Command
问题描述
在我的脚本中,我希望将Exchange Online通讯组成员添加到我的数组$members_id
.
In my script I want to get Exchange Online distribution group members to my array $members_id
.
我想在远程服务器上运行cmdlet Get-DistributionGroupMember
,所以它看起来像这样:
I want to run the cmdlet Get-DistributionGroupMember
on a remote server so it looks like this:
Invoke-Command -Session $Session -ScriptBlock {
$members_id = Get-DistributionGroupMember -Identity "power_shell_test"
} -ArgumentList $members_id
运行此命令后出现错误:
After running this I get an error:
此运行空间不支持该语法.如果运行空间处于无语言模式,则会发生这种情况.
The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.
当我删除$members_id =
时,它运行良好.
When I delete $members_id =
it runs good.
请告诉我您是否知道为什么会这样工作.
Please tell me if you have an idea why it works like that.
推荐答案
我不太确定为什么会收到错误消息(可能是因为您打开$Session
的原因),但是如果您想要输出本地变量$members_id
中的远程命令Get-DistributionGroupMember
,您需要将代码更改为以下内容:
I'm not quite sure why you're getting the error (it's probably because of how you opened $Session
), but if you want the output of a remote command Get-DistributionGroupMember
in a local variable $members_id
you need to change your code to something like this:
$members_id = Invoke-Command -Session $Session -ScriptBlock {
Get-DistributionGroupMember -Identity "power_shell_test"
}
仅当您要将要解析其成员的组的ID传递到脚本块中时,才使用-ArgumentList
.您可以使用Param()
指令将参数分配给脚本块中的变量:
Use -ArgumentList
only if you want to pass the ID of the group whose members you want resolved into the scriptblock. You can either assign the parameters to variables inside the scriptblock with a Param()
directive:
$members_id = Invoke-Command -Session $Session -ScriptBlock {
Param($id)
Get-DistributionGroupMember -Identity $id
} -ArgumentList $group_id