I have common helper etc methods on script library, i'd like to use them directly over powershell remoting, how ever i don't know how to pass them to scripts targeted to other machines.
I try pass function as scriptblock and execute it at remote server, on local computer everything works fine:
function test { write-host "abc" }
Invoke-Command -ScriptBlock {Param($1) & $1} -ArgumentList ${Function:Test}
Prints "abc"
However following does not work:
function test { write-host "abc" }
Invoke-Command -ScriptBlock {Param($1) & $1} -ArgumentList ${Function:Test} -ComputerName "OTHERCOMPUTER" -Credential $cred
The term ' write-host "abc" ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: ( write-host "abc" :String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Any idea what is wrong?
Edit: For clarification following answer where function itself is defined in scriptblock does not apply because function is taken from common script file located only on computer where remote is used. They are like "GetConfigurationFromAppConfig","SelectFromDatabase" and so on.
If i write function to every script block, then i have to copypaste same code over and over again everywhere. I don't want to use "common" script location where every computer can load scripts inside block either (for example . "\SharedLocation\") because it is fragile (lots of depencies) and it must be made for every server targeted for remoting (lots of upkeep).