Rob Bowman
Rob Bowman

Reputation: 8751

Powershell Invoke-Command Function within a Script

I have a Powershell script file called "ExecutBizTalkAppMSI.ps1" This contains a single function called "Install-BizTalkApplication". I need to execute the function on a remote server, so I use the "Invoke-Command" cmdlet as follows:

Invoke-Command -Computer $TargetServer -FilePath .\ExecuteBizTalkAppMSI.ps1 -argumentlist $MSI, $InstallFolderOnTargetServer, $Environment

Problem is, although the target script runs (I added a Write-Host directly after the Param() section), the function "Install-BizTalkApplication" is not executed.

Can anyone please let me know what I need to do to make this happen?

Upvotes: 0

Views: 3405

Answers (2)

Chris N
Chris N

Reputation: 7499

You say it "contains a function called"... So, if you run that script on your local machine with those arguments.. i.e:

.\ExecuteBizTalkAppMSI.ps1 $MSI $InstallFolderOnTargetServer $Environment

It wouldn't work, because you're running a script which just loads a function, and then stops, correct? That's your problem.

If so, you'll need to add a param statement to the top of the script, before the function:

param($MSI, $InstallFolderOnTargerServer, $Environment)

And then at the bottom, outside of the function you'll have to add:

Install-BizTalkApplication $MSI $InstallFolderonTarget $Environment

This will allow the script to accept arguments, and then pass those arguments to a statement that calls the function.

Upvotes: 0

BartekB
BartekB

Reputation: 8660

I suspect the script looks like that:

# start
function Foo {}
# end

That won't work. Two options:

  • get rid of function name {} and run script as is
  • define a function first, and run it as last step in a script

Example:

# script 1
param ($foo, $bar)
# function body...

# script 2
param ($foo, $bar)
function foo {
param ($foo, $bar)
# function body
}

foo -foo $foo -bar $bar

Upvotes: 1

Related Questions