在脚本块 powershell 中调用函数

Call a function inside a Script Block powershell

我有在 Octopus 中注册 Tentacle 的代码,我想在 Scriptblock 中调用一个名为 RunCommand 的函数。当我尝试在 Scriptblock 中调用它时,它一直失败。我正在从 csv 文件中读取数据,但无法弄清楚如何调用 Scritblock 中的函数。任何人都知道这是如何完成的。正如您从代码中看到的那样,我正在调用 RunCommand 函数,但它一直失败。我已经开始使用 function: call 但那也不起作用。请帮忙

function RunCommand{
Param(
  [string]$myCommand,
  [string]$myArgs
  )

$process = Start-Process -FilePath $myCommand -ArgumentList $myArgs -Wait -PassThru
if ($process.ExitCode -eq 0){
    Write-Host "$myCommand successful"
} else {
    Write-Host "$myCommand failed"
}  
return $process.ExitCode

函数 DeployTentacle{

#Read data from a csv file
$csv = Import-Csv -Path "C:\Users\adm_qvl6\Documents\RegisterTentacle.csv"

$csv | ForEach-Object {
    $ServerName = $($_.ServerName)
    $WorkerName = $($_.WorkerName)
    $Port = $($_.Port)  
    $Space = $($_.Space)    
    $Pool = $($_.Pool)
    $TentacleSource = $($_.TentacleSource)
    $TentacleDestination = $($_.TentacleDestination)
    $TentacleInstallPath = $($_.TentacleInstallPath)
    $TentacleWorkFolder = $($_.TentacleWorkFolder)
    $APIKey = $($_.APIKey)
    $OctopusURL = $($_.OctopusURL)
    $OctopusThumbprint = $($_.OctopusThumbprint)

    Invoke-Command -ComputerName $ServerName -ScriptBlock{

    param($WorkerName, $Port, $Space, $Pool, $TentacleSource, $TentacleDestination, $TentacleInstallPath, $TentacleWorkFolder, $APIKey, $OctopusURL, $OctopusThumbprint)

        $args="create-instance --instance `"$WorkerName`" --config `"$TentacleWorkFolder\Tentacle.config`""
        $rc = RunCommand $TentacleInstallPath $args

        $args="new-certificate --instance `"$WorkerName`" --if-blank"
        $rc = RunCommand $TentacleInstallPath $args

        $args="configure --instance `"$WorkerName`" --reset-trust"
        $rc = RunCommand $TentacleInstallPath $args

        $args="configure --instance `"$WorkerName`" --app `"$TentacleWorkFolder\Applications`" --port `"$Port`" --noListen `"False`""
        $rc = RunCommand $TentacleInstallPath $args

        $args="configure --instance `"$WorkerName`" --trust $OctopusThumbprint"
        $rc = RunCommand $TentacleInstallPath $args

        $args="service --instance `"$WorkerName`" --install --stop --start"
        $rc = RunCommand $TentacleInstallPath $args

        $args="register-worker --space `"$Space`" --instance `"$WorkerName`" --server `"$OctopusURL`" --apiKey=`"$APIKey`" --workerpool=`"$Pool`" --comms-style TentaclePassive --force"
        $rc = RunCommand $TentacleInstallPath $args

        $args="service --instance `"$WorkerName`" --install --stop --start"
        $rc = RunCommand $TentacleInstallPath $args    

    } -ArgumentList $WorkerName, $Port, $Space, $Pool, $TentacleSource, $TentacleDestination, $TentacleInstallPath, $TentacleWorkFolder, $APIKey, $OctopusURL, $OctopusThumbprint

} }

使用 invoke-command 您正在创建与另一台主机的会话。您不会将完整的脚本推送到会话中,而只是 scriptblock。因此,您必须在 scriptblock 的内部定义函数才能在其中使用它。

invoke-command -scriptblock{
    function newfunc{
        #do something
    }
    newfunc
}