将 PowerShell 脚本的输出传递给 Jenkins Parameters
Pass the output of PowerShell script to Jenkins Parameters
我正在尝试获取 Vsphere 模板列表并将它们用作 Jenkins 中的参数。
我已经尝试使用函数和 运行 PowerShell 命令。
def findtemplates() {
def $vmTemplate = "powershell -command 'Connect-VIServer -server server -User user -Password pass -Force; Get-Template | select name'"
return $vmTemplate
}
并且在参数部分:
parameters {
choice(name: 'Template', choices: findtemplates(), description: 'test')
}
但是不起作用。
任何帮助将不胜感激。
使用参数 returnStdout: true
为 PS 7+) 调用 powershell
step (or pwsh
以获取 PowerShell 命令的输出:
def findtemplates() {
return powershell( returnStdout: true, script: '''
Connect-VIServer -server server -User user -Password pass -Force
Get-Template | select name'
''')
}
注意使用 '''
将多行脚本传递到 powershell
步骤。
我想您只是为了简洁而对用户名和密码进行了硬编码。一个更完整的示例如下所示:
def findtemplates() {
withCredentials([ usernamePassword( credentialsId: 'ReplaceWithYourCredentialsId',
usernameVariable: 'VIServerUser',
passwordVariable: 'VIServerPassword') ]) {
return powershell( returnStdout: true, script: '''
Connect-VIServer -server server -User $env:VIServerUser -Password $env:VIServerPassword -Force
Get-Template | select name'
''')
}
}
请注意,出于安全原因,使用环境变量而不是 Groovy 字符串插值(请参阅 String interpolation)。
我正在尝试获取 Vsphere 模板列表并将它们用作 Jenkins 中的参数。 我已经尝试使用函数和 运行 PowerShell 命令。
def findtemplates() {
def $vmTemplate = "powershell -command 'Connect-VIServer -server server -User user -Password pass -Force; Get-Template | select name'"
return $vmTemplate
}
并且在参数部分:
parameters {
choice(name: 'Template', choices: findtemplates(), description: 'test')
}
但是不起作用。 任何帮助将不胜感激。
使用参数 returnStdout: true
为 PS 7+) 调用 powershell
step (or pwsh
以获取 PowerShell 命令的输出:
def findtemplates() {
return powershell( returnStdout: true, script: '''
Connect-VIServer -server server -User user -Password pass -Force
Get-Template | select name'
''')
}
注意使用 '''
将多行脚本传递到 powershell
步骤。
我想您只是为了简洁而对用户名和密码进行了硬编码。一个更完整的示例如下所示:
def findtemplates() {
withCredentials([ usernamePassword( credentialsId: 'ReplaceWithYourCredentialsId',
usernameVariable: 'VIServerUser',
passwordVariable: 'VIServerPassword') ]) {
return powershell( returnStdout: true, script: '''
Connect-VIServer -server server -User $env:VIServerUser -Password $env:VIServerPassword -Force
Get-Template | select name'
''')
}
}
请注意,出于安全原因,使用环境变量而不是 Groovy 字符串插值(请参阅 String interpolation)。