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