I'm trying to pass some variables sort-of similar to the below, but it isn't passing back the updated/change data i want. In the small example below can you state the way this should be written to be able to pass the data in and back as shown?
$myfirstname = Jos
$sess = new-pssession -computername "superdooperkompooter.domain.local"
invoke-command -jobname whatsmyname -session $sess -scriptblock {
#Pass in external parameter
Param($myfirstname)
#Change #myfirstname ... there's more to it in the real script
$myfirstname = Jon
$fullname = @()
$fullname += $myfirstname
$fullname += "Try"
$fullname += "Feckta" # So this should be effectively $fullname = @(Jon,Try,Feckta) at this point
# Now i need to pass back the changed/added variables
} -Argumentlist ($myfirstname,$fullname)
# Now when i attempt to show the data it just comes out blank
write-out $fullname
write-out $myfirstname
The -ArgumentList
is for the variables that goes in.
There is no "byRef" here.
What you need to do is to assign your Invoke-Command
statement to a variable.
From there, when you are ready to return your "multiple variables", you can return them as a hashtable so they are ready to use in your Write-Host
statements.
Example
$Output = invoke-command -jobname whatsmyname -session $sess -scriptblock {
#Pass in external parameter
Param($myfirstname)
# ... Your code here
return @{
MyFirstName = $myfirstname
FullName = $fullname
}
} -Argumentlist ($myfirstname)
write-out $Output.fullname
write-out $Output.myfirstname
Additional notes
I am not passing the $fullName
to the argument list since you did only define Param($myFirstName)
and we established that yhe value would not go out and be assigned to the $fullName
variable.