'How to use an external Groovy WinRM library within a Jenkins Pipeline parameter?
I need to build a dynamic drop down list parameter for my Jenkins pipeline from the result of a WinRM request (Powershell command). The Jenkins parameter has to be a drop down list of Organisational Units Distinguished names grabbed from an Active Directory domain controller.
WinRM is already configured and working properly (tested from other Windows boxes and from Jenkins using Python pywinrm library).
I don't want to call Python from Groovy and I'd preferably go with a Groovy WinRM client. I found a Goovy library groovy-winrm-client that might be a good fit to execute WinRM commands from Groovy but I can't figure out where should I put the library itself on the Jenkins server file system, how to import it and how to make all work seamlessy in the context of the pipeline parameter.
- Jenkins is running on a Centos 7.X server
- Groovy version is 2.4.7
- Target server is Windows 2012 R2 with WinRM configured
Jenkins Groovy installation
/var/lib/jenkins/tools/hudson.plugins.groovy.GroovyInstallation/my_groovy_version
Pipeline
// Untested
properties([
parameters([
choice(
name: 'USER_TYPE',
choices: ['Active Directory','Other'],
description: "User type"
),
[
$class: 'DynamicReferenceParameter',
name: 'OU',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: "OU distinguishedName",
referencedParameters: 'USER_TYPE',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['Error']"
],
script: [
classpath: [],
sandbox: false,
script: """
// External lib to be imported
import com.aestasit.infrastructure.winrm.client.WinRMClient
if (USER_TYPE.equals('Active Directory')) {
def html = '''<select name="value">
<option value="----">----</option>
'''
WinRMClient client = new WinRMClient(protocol: 'https', host: 'server.tld', port: 5986, user: 'my_user', password: '*********')
client.openShell()
String commandId = client.executeCommand("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", "-command {get-organisationnalunit -filter * -properties * | % { $_.distinguishedname } | sort }")
CommandOutput output = client.commandExecuteResults(commandId)
output.output.eachLine{
html += '''<option value="${it}">${it}</option>'''
}
html += '''</select>'''
return html
}
"""
]
]
]
])
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
