Powershell – Using defined variables in a scriptblock

If you try use an already defined variable inside a scripblock, using the default call without any scope definition, the variable will not be expanded and it probably will not produce the expected outcome. 

From Powershell v3 onwards, the scopes include an «using», that allows to access defined variables in other scopes, in this way the variable will be expanded as expected inside the scriptblock.

$foo = 'Get-Childitem .'
Invoke-Command -Computername server -scriptblock { $using:foo }

 

·n·

Deja un comentario