调用脚本块电源外壳内的函数



我有代码在Octopus中注册触手,我想在脚本块中调用一个名为RunCommand的函数。当我尝试在脚本块中调用它时,它一直失败。我正在从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:Usersadm_qvl6DocumentsRegisterTentacle.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 `"$TentacleWorkFolderTentacle.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 `"$TentacleWorkFolderApplications`" --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

} }

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

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

如果您在两个站点(在本地运行的脚本和远程站点中(都使用RunCommand函数,则可以在调用脚本之前将该函数注入脚本中,如此处所示。在这些条件下,此技术不必为本地和远程访问声明两次函数。但是,前提是将脚本保存在变量中,以便执行注入。喜欢这个:

[ScriptBlock]$script = {
param($WorkerName, $Port, $Space, $Pool, $TentacleSource, $TentacleDestination, $TentacleInstallPath, $TentacleWorkFolder, $APIKey, $OctopusURL, $OctopusThumbprint)
...
}
Function MyFunction{
write-host "Hello World"
}

invoke-command -scriptblock ${function:MyFunction}

相关内容

  • 没有找到相关文章

最新更新