Answered by:
function script block and arguments

Question
-
$myResukt = @( [PSCustomObject]@{ Sam = "PC2Test"; upn = "PC2Test@domain.org" } [PSCustomObject]@{ Sam = "PC3Test"; upn = "PC3Test@domain.org" } ) $AllUpn = ($myResukt.upn) $xchSvrLic = "dcchvw" $mySessionLic = New-PSSession -ComputerName $xchSvrLic function SendReport ($myResukt) { foreach ($one in $myResukt) { $upn = $one Write-Host $upn } } Invoke-Command -session $mySessionLic -ScriptBlock ${function:SendReport} -ArgumentList $AllUpn
when I execute this function I get only one value
PC2Test@domain.org
what is wrong? it should bring both.
Saturday, April 27, 2019 9:37 PM
Answers
-
I wonder that you don't get an error. Your function does not exist in the remote session. You would need to declare that function first inside your remote session to be able to use it.
You can use local variables in a remote session with the scope "modifier" $using: ... like this:
Invoke-Command -session $mySessionLic -ScriptBlock {$Using:myResukt.upn}
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Marked as answer by asif300 Saturday, April 27, 2019 10:41 PM
Saturday, April 27, 2019 9:56 PM -
Like this:
Hmmm ... wow ... I will have to think about this ... :-O ;-)
......
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
It is an old behavioral issue with PS and passing arguments.
\_(ツ)_/
- Marked as answer by asif300 Saturday, April 27, 2019 11:56 PM
Saturday, April 27, 2019 11:22 PM
All replies
-
I wonder that you don't get an error. Your function does not exist in the remote session. You would need to declare that function first inside your remote session to be able to use it.
You can use local variables in a remote session with the scope "modifier" $using: ... like this:
Invoke-Command -session $mySessionLic -ScriptBlock {$Using:myResukt.upn}
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Marked as answer by asif300 Saturday, April 27, 2019 10:41 PM
Saturday, April 27, 2019 9:56 PM -
I am confused here, you are not calling the function sendReport inside the scriptBlock, but when I execute I get what I want. Please explain me. Thanks
Is there a way to explicitly mention the function name?
- Edited by asif300 Saturday, April 27, 2019 10:15 PM
Saturday, April 27, 2019 10:08 PM -
It's actually the same like when you do
$myResukt.upn
in your current local session. You are just enumerating the elements of the array. I already asked you in the other thread what it is what you actually want to do. ;-)Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Saturday, April 27, 2019 11:09 PM -
Like this:
$myResukt = @( [PSCustomObject]@{ Sam = "PC2Test"; upn = "PC2Test@domain.org" } [PSCustomObject]@{ Sam = "PC3Test"; upn = "PC3Test@domain.org" } ) $xchSvrLic = "dcchvw" $AllUpn = $myResukt.upn function SendReport($myResukt){ foreach ($one in $myResukt) { $upn = $one Write-Host $upn } } Invoke-Command -ScriptBlock ${function:SendReport} -ArgumentList @(,$AllUpn) -ComputerName $xchSvrLic
\_(ツ)_/
- Edited by jrv Saturday, April 27, 2019 11:10 PM
Saturday, April 27, 2019 11:09 PM -
Like this:
......
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Edited by BOfH-666 Saturday, April 27, 2019 11:13 PM
Saturday, April 27, 2019 11:13 PM -
Like this:
Hmmm ... wow ... I will have to think about this ... :-O ;-)
......
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
It is an old behavioral issue with PS and passing arguments.
\_(ツ)_/
- Marked as answer by asif300 Saturday, April 27, 2019 11:56 PM
Saturday, April 27, 2019 11:22 PM