hankey39
hankey39

Reputation: 359

Call a function inside a Script Block powershell

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

} }

Upvotes: 5

Views: 8406

Answers (4)

Phoenix14830
Phoenix14830

Reputation: 390

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)

Upvotes: 1

Olli
Olli

Reputation: 375

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)
    ...
}

Upvotes: 0

Michael Cobb
Michael Cobb

Reputation: 1

Function MyFunction{

write-host "Hello World"

}


invoke-command -scriptblock ${function:MyFunction}

Upvotes: -2

hcm
hcm

Reputation: 1020

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
}

Upvotes: 2

Related Questions