1

Pipeline is not passing down string parameter data to the PowerShell command were trying to create a jenkins page that allows a person to create a cname on a domain. We know creds work if we bypass and run the PowerShell command directly, but when trying to use parameter data, we can't get it to work for PowerShell nor we if use write-host to see if data is being pulled down comes up blank.

pipeline {
    agent any

parameters {
  string(name: 'CNAME', description: 'ShortName')
 // string(name: 'HostNameAlias', defaultValue: '.domain.com', description: 'DestinationServer.domain.com')
}
    environment {
        // Replace with your actual Jenkins credentials ID (username + password)
        REMOTE_CRED = credentials('netdomaindns')
    }

    stages {
        stage('Run PowerShell on Remote Server') {
            steps {
                script {
                        def myParamValue = params.CNAME
                powershell '''
                    Write-Host "Preparing to run PowerShell on remote server..."
                    Write-Host "MyParameters from Jenkins: $myParamValue"
                    $username = "$env:REMOTE_CRED_USR"
                    $password = "$env:REMOTE_CRED_PSW"

                    # Convert password to secure string
                    $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
                    $credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)

                    # Define the remote server name or IP
                    $remoteServer = "ws-server1.domain.com"

                    # Example command to run remotely
                    Invoke-Command -ComputerName $remoteServer -ScriptBlock {
                        Write-Host "Running on remote server: $env:COMPUTERNAME"
                        Write-Host "MyParameter from Jenkins: $CNAME"
                        Write-Host "MyParameter from Jenkins: ${params.CNAME}"
                     #   Write-Host "MyParameter from Jenkins: $HostNameAlias"
                        $env:MyParameterFromPipeline = "$CNAME"
                     #   $env:MyParameterFromPipeline = "$HostNameAlias"
 Add-DnsServerResourceRecordCName -Name ${params.CNAME} -HostNameAlias $($params:HostNameAlias) -ComputerName "ws-server1.domain.com" -ZoneName "domain.com"
                  } -Credential $credential
                '''
            }
        }
    }
}}
1
  • How you try to read this solution describes how to use parameters in a properties block Commented Oct 13 at 22:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.