You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline中PowerShell无法获取VM_NAME参数值问题求助

问题:Jenkinsfile中PowerShell无法获取VM_NAME参数值

我使用包含多行PowerShell脚本的Jenkinsfile实现VM加入域并配置DNS,Jenkinsfile内容如下:

pipeline {
agent { label 'slave-windows' }
stages {
    stage('Parameters') {
        steps {
            script {
            properties([
                parameters([
                            string(name: 'VM_NAME', defaultValue: '', description: 'Enter the VM you would like to add to Domain.'),
                            string(name: 'IP_ADDR', defaultValue: '', description: 'Enter the IP of your VM.')
                ])
            ])
            }
        }
    }
    stage('domain') {
        steps {
            powershell '''
                \$VM_NAME=\$VM_NAME
                \$DOMAIN="domain"
                \$IP_ADDR=\$IP_ADDR
                \$DNS="adserver"
                \$username="domain\\administrator" 
                \$password="password" | ConvertTo-SecureString -asPlainText -Force
                \$credential=New-Object System.Management.Automation.PSCredential(\$username,\$password)

                Add-Computer -ComputerName \$VM_NAME -Domain \$DOMAIN -Credential \$credential -Restart
                Enter-PSSession -ComputerName \$DNS
                Add-DnsServerResourceRecordA –ComputerName \$DNS -Name \$VM_NAME -ZoneName \$DOMAIN -AllowUpdateAny -IPv4Address \$IP_ADDR
            '''
        }
    }
}

构建时已传入VM_NAME和IP_ADDR参数,但执行时出现错误,提示VM_NAME变量为空:

powershell.exe : Add-Computer : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\jenkins\workspace\DNS Registration\Add To Domain@tmp\durable-809aaad3\powershellScript.ps1:10 char:48
+                     Add-Computer -ComputerName $VM_NAME -Domain $DOMA ...

+ CategoryInfo          : InvalidData: (:) [Add-Computer], ParentContainsErrorRecordException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddComputerCommand

解决思路

  • 修正参数传递方式:Jenkins的Groovy脚本中,三重单引号(''')包裹的内容不会解析变量,导致PowerShell无法获取Jenkins的VM_NAME和IP_ADDR参数。需要改用三重双引号("""),让Groovy将参数值注入到PowerShell脚本中,同时注意转义PowerShell内部的双引号。
  • 移除冗余变量赋值:原脚本中\$VM_NAME=\$VM_NAME属于无效赋值,直接使用Jenkins传入的参数即可。
  • 修复远程执行问题:Enter-PSSession是交互式命令,在Jenkins的非交互式环境下无法正常工作,改用Invoke-Command远程执行DNS配置操作。

修改后的Jenkinsfile示例:

pipeline {
agent { label 'slave-windows' }
stages {
    stage('Parameters') {
        steps {
            script {
                properties([
                    parameters([
                        string(name: 'VM_NAME', defaultValue: '', description: 'Enter the VM you would like to add to Domain.'),
                        string(name: 'IP_ADDR', defaultValue: '', description: 'Enter the IP of your VM.')
                    ])
                ])
            }
        }
    }
    stage('Join Domain & Configure DNS') {
        steps {
            powershell """
                \$DOMAIN = "domain"
                \$DNS = "adserver"
                \$username = "domain\\administrator" 
                \$password = "password" | ConvertTo-SecureString -asPlainText -Force
                \$credential = New-Object System.Management.Automation.PSCredential(\$username, \$password)

                # 将VM加入域
                Add-Computer -ComputerName "${VM_NAME}" -Domain \$DOMAIN -Credential \$credential -Restart

                # 远程配置DNS记录
                Invoke-Command -ComputerName \$DNS -Credential \$credential -ScriptBlock {
                    param(\$VM_NAME, \$IP_ADDR, \$DOMAIN)
                    Add-DnsServerResourceRecordA -Name \$VM_NAME -ZoneName \$DOMAIN -AllowUpdateAny -IPv4Address \$IP_ADDR
                } -ArgumentList "${VM_NAME}", "${IP_ADDR}", \$DOMAIN
            """
        }
    }
}
}

内容的提问来源于stack exchange,提问作者zorro1224

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 13:48:22