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