【问题标题】:Windows batch commands is not executed through Jenkins in Windows 7 after converting the agent into service将代理转换为服务后,Windows 7 中的 Jenkins 不执行 Windows 批处理命令
【发布时间】:2016-11-12 08:50:18
【问题描述】:

在尝试通过 Jenkins 执行以下批处理命令以使用 PowerShell 脚本修改 XML 时,

powershell.exe "C:\jenkins\workspace\TemplateJob\Scripts\Common\Prerequsites\Powershell\PowershellScripts\ModifyXML.ps1" "C:\jenkins\workspace\TemplateJob\Scripts\TestNgXML\RegressionTests\Regression.xml" "%Target_Browser%"

我收到以下错误:

无法将值“System.Object[]”转换为类型“System.Xml.XmlDocument”。错误:“无法连接到远程服务器”

但是,如果从代理作为 Web Start (Slave-agent.jnlp) 启动,相同的命令在 Windows 8 中通过 Jenkins 可以正常工作,并且在 Windows 7 中也可以正常工作。

启用“允许服务与桌面交互”后也存在问题。

请查找以下示例:ModifyXML.ps1,它将使用 XPath 将给定 XML 中的参数值更新为参数值。

Regression.xml:

<suite><parameter name="browser" value="ie" /></suite>

ModifyXML.ps1:

param($path, $browser)

$xml = [xml](Get-Content $path) # Getting "Unable to connect to the remote server" error in this line
$xpathbrowser = "/suite/parameter[@name='browser']/@value" 
$nodes = $xml.SelectNodes($xpathbrowser)
foreach ($node in $nodes) {
    if ($node -ne $null) {
        if ($node.NodeType -eq "Element") {
            $node.InnerXml = $browser
        } else {
            $node.Value = $browser
        }
    }
}

【问题讨论】:

  • ModifyXML.ps1的内容是什么?它在哪里抛出错误?
  • 可能不相关,但您应该使用参数-File 来运行 PowerShell 脚本。
  • @AnsgarWiechers 具有相同参数的相同脚本在 Windows 8 中运行良好。在 Windows 7 中,如果 jenkins slave 作为 jnlp 代理运行而不作为服务运行,则也运行良好。 Windows 7 中 Jenkins 服务的唯一问题。
  • 请不要在 cmets 中发布与您的问题相关的代码或其他信息。编辑您的问题并将其添加到那里。
  • @MathiasR.Jessen Powershell 脚本 ModifyXML.ps1,用于修改 xml 参数值。请找到有问题的样本

标签: powershell selenium jenkins windows-7 jenkins-slave


【解决方案1】:

我遇到了同样的问题。由于某种原因,XML 转换停止工作。

这是我的原始代码:

function UpdateConfigValue($FileName, $xpath, $value) {

    $doc =  [xml](Get-Content $FileName) # no XML transformation any more

    $nodes = $doc.SelectNodes($xpath)

        foreach ($node in $nodes) {
            if ($node -ne $null) {
                if ($node.NodeType -eq "Element") {
                    $node.InnerXml = $value
                }
                else {
                    $node.Value = $value
                }
            }
        }

    $doc.save($FileName)
}

我尝试了不同的方法,最后得到了以下适合我的解决方案:

function UpdateConfigValue($FileName, $xpath, $value) {

    $fullpath = Join-Path $(get-location) $FileName # full path to the XML file

    [System.Xml.XmlDocument]$file = new-object System.Xml.XmlDocument
    $file.load($fullpath)

    $node = $file.SelectSingleNode($xpath)
    if ($node -ne $null) {
        if ($node.NodeType -eq "Element") {
            $node.InnerXml = $value
        }
        else {
            $node.value = $value
        }       
    }

    $file.save($fullpath)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多