Created
December 13, 2017 18:57
-
-
Save pinecones-sx/f2325eb0ad9027620e64aa38f29eceec to your computer and use it in GitHub Desktop.
pass local functions to remote session with invoke-command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# 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