微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell 从 Jenkins groovy 变量中去除双引号

如何解决Powershell 从 Jenkins groovy 变量中去除双引号

我正在尝试将数据从 Jenkins 机器中的 Json 机密文件复制到另一台服务器。 我的 Json 文件数据将如下所示。

{
  "Server:name": "abc","application.version": "12"
}

詹金斯代码


import groovy.json.JsonOutput
import groovy.json.JsonSlurper



node
{
    stage("Print")
    {
     
     
      
      def path = "C:\\xyz\\jsonData.json"      //remote file path where data has to be saved
     
    
       withCredentials([file(credentialsId: 'xyz',variable: 'abcFile')])
       {
        
                
                writeFile file: 'tempFile',text: readFile(abcFile)
                def readContent = readFile 'C:\\***\\tempFile'    // get file Jenkins from workspace

                def temp = readContent.replace("\"","\"\"")

                // tried all these
                def temp = readContent.replace("\"","\"\"\"")
                def temp = readContent.replace("\"","\\\"")    //causes error in powershell
                def temp = readContent.replace("\"","\\\\\"")
                def temp = readContent.replace("\"","\\\`\"")   // causes error in jenkins
                def temp = readContent.replace("\"","\'\"")

               
                def dataInjson = JsonOutput.toJson(readContent) // this one causes error in powersehll

                //println temp
               
                 def psSh = "powershell.exe "
                 psSh += "-NonInteractive -ExecutionPolicy Bypass "
                 psSh += "-Command \"\$ErrorActionPreference='Stop'; "
                 psSh += "invoke-command -computername ${xyz} -authentication NegotiateWithImplicitCredential -scriptblock { "
                 psSh += "import-module webadministration;"
                
                 
                 psSh += "if (!(Test-Path ${path})) {"
                 psSh += "\$folder = New-Item -ItemType Directory -Force -Path \"${path}\" };"

                 //copying file didn't work because of not having right credentials (tried creating sessions)
                 //psSh += "copy-Item $abcFile -Destination ${path}; "

                 //tried all these but not working

                 //psSh += "Set-Content -Path \"${path}\" -Value \"${readContent}\" "
                // psSh += "\"${dataInjson}\" | ConvertTo-Json | Set-Content \"${path}\" "
                 psSh +=  " \$s = \\\"${temp}\\\" ; "
                 psSh += " \$s | Set-Content -Path \"${path}\" "

                 psSh += "};"
                 psSh += "EXIT \$global:LastExitCode\" "
                 bat psSh
           
        }
    }

}

但我能得到的唯一体面的输出是没有双引号。 Powershell 正在去除双引号。

{
  Server:name: abc,application.version: 12
}

如果没有双引号,我的代码将无法运行。任何人都可以帮助我吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。