1

Is there a way to get a list of all remote variables in a script block?

Consider the following:

$x = 1
$block = { Write-Host $using:x }
Invoke-Command -Session (New-PSSession) -ScriptBlock $block

Inside of $block, is there any way of getting a list of available $using: scoped variables?

$x = 1
$block = { Get-Variable }
Invoke-Command -Session (New-PSSession) -ScriptBlock $block

Does not yield $x as an available variable

1 Answer 1

1

The short answer is: you can't.

The remote side doesn't know anything about the variables. They get serialized and then the deserialization code and the literal serialized XML is embedded.

If you're the one writing the script bock, then I recommend you just assign each $Using: variable to a local variable inside the scriptblock:

$block = {
    $x = $Using:x
    $y = $Using:y
}

I wrote a more detailed explanation of how $Using: is implemented on my blog regarding using it in DSC Script resources.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.