Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Created December 13, 2017 18:57
Show Gist options
  • Save pinecones-sx/f2325eb0ad9027620e64aa38f29eceec to your computer and use it in GitHub Desktop.
Save pinecones-sx/f2325eb0ad9027620e64aa38f29eceec to your computer and use it in GitHub Desktop.
pass local functions to remote session with invoke-command
<# Passing functions to remote sessions
https://stackoverflow.com/questions/11367367/how-do-i-include-a-locally-defined-function-when-using-powershells-invoke-comma
#>
function test-write {Write-Host 'i pood!'}
function test-out {'i pood!' | Out-File 'C:\users\public\documents\test.txt'}
$passedFunctions = "function test-write {${function:test-write}} ; function test-out {${function:test-out}}"
$targetPC = 'PC1'
Invoke-Command `
-Credential (get-credential) `
-ArgumentList $passedFunctions `
-ComputerName $targetPC -ScriptBlock {
param($passedFunctions)
# instantiates functions locally
.([ScriptBlock]::Create($passedFunctions))
test-write
test-out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment