简体   繁体   English

脚本中的Powershell调用命令功能

[英]Powershell Invoke-Command Function within a Script

I have a Powershell script file called "ExecutBizTalkAppMSI.ps1" This contains a single function called "Install-BizTalkApplication". 我有一个名为“ ExecutBizTalkAppMSI.ps1”的Powershell脚本文件,其中包含一个名为“ Install-BizTalkApplication”的函数。 I need to execute the function on a remote server, so I use the "Invoke-Command" cmdlet as follows: 我需要在远程服务器上执行该功能,因此我使用“ Invoke-Command” cmdlet如下:

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. 问题是,尽管目标脚本正在运行(我在Param()节之后直接添加了Write-Host),但未执行功能“ Install-BizTalkApplication”。

Can anyone please let me know what I need to do to make this happen? 谁能让我知道实现这一目标需要做什么?

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

You say it "contains a function called"... So, if you run that script on your local machine with those arguments.. ie: 您说它“包含一个称为的函数” ...因此,如果您使用这些参数在本地计算机上运行该脚本,即:

.\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语句:

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. 这将允许脚本接受参数,然后将这些参数传递给调用该函数的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM