N
N
NewDevLab2020-04-02 09:53:36
PowerShell
NewDevLab, 2020-04-02 09:53:36

Can I call my function from a script in Invoke-Command?

Those. there is a ps1 script, it has a TestFunc function and a call to this function

Invoke-Command -ComputerName 192.168.0.1 -ScriptBlock {(TestFunc "test")}

Writes
The name 'TestFunc' is not recognized as the name of a cmdlet, function, script file, or executable program.

Or should the function be placed in a separate script and called as -FilePath?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
azarij, 2020-04-02
@NewDevLab

here, it seems, in detail:
duffney.io/RunLocalFunctionsRemotely

function MyFunction ()
{
    Write-Host 'Hello, World!'
}

Invoke-Command -ComputerName DC1 -ScriptBlock ${Function:MyFunction}

S
Somewhere Intech, 2020-04-02
@john36allTa

Or here


The FilePath parameter specifies a script that is located on the local computer. The script runs on the remote computer and the results are returned to the local computer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question