在脚本块 powershell 中调用函数

han*_*y39 5 powershell scriptblock

我有在 Octopus 中注册 Tentacle 的代码,我想在 Scriptblock 中调用一个名为 RunCommand 的函数。当我尝试在脚本块内调用它时,它总是失败。我正在从 csv 文件读取数据,但无法弄清楚如何调用 Scritblock 内的函数。任何人都知道这是如何做到的。正如您从代码中看到的,我正在调用 RunCommand 函数,但它一直失败。我已经使用了以下函数: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
Run Code Online (Sandbox Code Playgroud)

函数部署触手{

#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
Run Code Online (Sandbox Code Playgroud)

} }

hcm*_*hcm 2

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

invoke-command -scriptblock{
    function newfunc{
        #do something
    }
    newfunc
}
Run Code Online (Sandbox Code Playgroud)