Problems with Invoke-Command

前端 未结 1 1990
臣服心动
臣服心动 2020-11-30 15:38

In my script I want to get Exchange Online distribution group members to my array $members_id.

I want to run the cmdlet Get-DistributionGroupMembe

相关标签:
1条回答
  • 2020-11-30 16:07

    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"
    }
    

    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

    or use the automatic variable $args:

    $members_id = Invoke-Command -Session $Session -ScriptBlock {
        Get-DistributionGroupMember -Identity $args[0]
    } -ArgumentList $group_id

    Alternatively you could access variables outside the scriptblock via the using: scope modifier:

    $members_id = Invoke-Command -Session $Session -ScriptBlock {
        Get-DistributionGroupMember -Identity $using:group_id
    }
    0 讨论(0)
提交回复
热议问题