2

How can I resuse powershell functions in remote block to re-use them?

Example:- I have 3 functions that i need to run remotely like

function write-something($param1,$param2)
{
          //Do Stuff
}

function Check-Application($param1)
{
          //Do Stuff
}

function Create-Application()
{
          //Do Stuff
          Check-Application $param1 
          //Do Stuff
          Write-Something $param1 $param2 
}

write-something $param1 $param2
Invoke-Command -computer FQDN -scriptBlock <3functionsAbove> -arguments
write-something $param1 $param2

I'm also using write-something($param1,$param2) function locally. i want to define the write-something($param1,$param2) once and use locally and in remote process. Is there any way i can achieve this?

Krishna
  • 347
  • 1
  • 6
  • 23
  • I am not sure that I understand the issue, you are trying to use `write-something` in your code and locally too? – Luke Jun 30 '15 at 20:14
  • 1
    For the functions to be available remotely, they have to be defined within the scriptblock. – Jan Chrbolka Jun 30 '15 at 20:17
  • 1
    If you want to "inject" the locally defined function into the script block it can be done... `$ws = "function write-something {" + (Get-Content function:write-simething) + "}\`n"` This puts your function definition into a [string] variable. You can then just insert it at the beginning of your scriptblock. – Jan Chrbolka Jun 30 '15 at 20:25
  • Are you trying to do something like this? http://stackoverflow.com/questions/11367367/how-do-i-include-a-locally-defined-function-when-using-powershells-invoke-comma – Douglas Plumley Jun 30 '15 at 20:26
  • I answered a similar question last week - http://stackoverflow.com/questions/31006109/functions-powershell-remoting/31006924#31006924 – Keith Hill Jul 01 '15 at 01:08
  • Keith Hill - can i include all three functions in to a script block? i do not want to pass files to the remote computer. i just want to define local functions and use them in remote session. currently im not able to use them locally & remotely. – Krishna Jul 01 '15 at 11:29

0 Answers0