5

I have code to register a Tentacle in Octopus and I want to call a Function called RunCommand inside the Scriptblock. It keeps failing when I try to call it inside the Scriptblock. I am reading my data from a csv file but just cant figure out how to call the function inside the Scritblock. Anyone know how this is done. As you can see from the code I am calling the RunCommand function but it keeps failing. I have come acress using the function: call but that does not work either. Please help.

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

function 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

} }

4 Answers 4

2

With invoke-command you are creating a session to another host. You don't push your complete script into the session but only the scriptblock. So you've got to define your function INSIDE of the scriptblock to use it inside it.

invoke-command -scriptblock{
    function newfunc{
        #do something
    }
    newfunc
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you use the RunCommand function at both sites - in your locally running script and remote - you can inject the function into the script before of invoking it as demonstrated here. Under these conditions, this technique saves having to declare the function for local and remote access twice. However, the prerequisite is that you save the script in a variable in order to then carry out the injection. Like this:

[ScriptBlock]$script = {
    param($WorkerName, $Port, $Space, $Pool, $TentacleSource, $TentacleDestination, $TentacleInstallPath, $TentacleWorkFolder, $APIKey, $OctopusURL, $OctopusThumbprint)
    ...
}

Comments

0

Here's a simple command that works:

[ScriptBlock]$SimpleFunction = {
param (
    [Parameter(Mandatory = $true)]
    [string]$Parameter1,
    [String]$Parameter2
)
}

Invoke-Command -ScriptBlock $SimpleFunction -ArgumentList @("First Parameter",$ParameterVariable)

Comments

-2
Function MyFunction{

write-host "Hello World"

}


invoke-command -scriptblock ${function:MyFunction}

1 Comment

you could improve this by including an example of how to pass parameters ( I did not down-vote btw)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.