调用脚本块powershell中的函数

db2dz4w8  于 2023-03-02  发布在  Shell
关注(0)|答案(2)|浏览(197)

我有代码在Octopus中注册一个Tentacle,我想在Scriptblock中调用一个名为RunCommand的函数。当我试图在Scriptblock中调用它时,它总是失败。我正在从csv文件中阅读数据,但就是不知道如何调用Scritblock中的函数。任何人都知道这是如何完成的。正如您从代码中看到的,我正在调用RunCommand函数,但它总是失败。我使用函数来访问:但那也不行。请帮帮忙。

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

函数展开触角{

#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

} }

axzmvihb

axzmvihb1#

使用invoke-command,你创建了一个到另一个主机的会话。你没有把你的整个脚本推到会话中,而只是把scriptblock推到会话中。所以你必须在scriptblock内部定义你的函数,以便在里面使用它。

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

gcuhipw92#

Function MyFunction{

write-host "Hello World"

}

invoke-command -scriptblock ${function:MyFunction}

相关问题