1

I've got the following script:

param(
    [string[]]$servers
)

function Write-Info($message) {
}

function Introspect($server) {
    Write-Info "about to do something on server"    
    // other powershell stuff that works
}

Foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock ${function:Introspect} -ArgumentList $server -credential 'MyUser'
}

That I'm trying to invoke on a remote server like so (from powershell):

.\myscript.ps1 server1,server2,server3

The script is executing, but the issue is I get an error relating to the Write-Info function like so:

The term 'Write-Info' is not recognised as the name of a cmdlet, function, script file, or operable program

The Introspect function works fine if I embed the function inside but I guess it relates to the function not being on the remote server.

How can I solve this please?

imrichardcole
  • 4,633
  • 3
  • 23
  • 45
  • 1
    you may want to take a look at this article on the subject [and the reddit thread you can reach thru his link in the upper right] ... Transferring Functions with PSRemoting – Clear-Script – Joel (Sallow) Francis | PowerShell Enthusiast | @vexx32 /u/ta11ow — https://vexx32.github.io/Transferring-Functions/ – Lee_Dailey Nov 03 '18 at 17:49
  • Also possible [duplicate](https://stackoverflow.com/questions/11367367/how-do-i-include-a-locally-defined-function-when-using-powershells-invoke-comma)... – Peter Schneider Nov 03 '18 at 18:20

1 Answers1

1

Managed to solve this by embedding the 'missing' function inside the working one:

param(
    [string[]]$servers
)

function Introspect($server) {

    function Write-Info($message) {
    }
    Write-Info "about to do something on server"    
    // other powershell stuff that works

}

Foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock ${function:Introspect} -ArgumentList $server -credential 'MyUser'
}
imrichardcole
  • 4,633
  • 3
  • 23
  • 45