如何将 powershell 哈希表从一个脚本传递到 Jenkins 管道中的另一个脚本

How to pass powershell hashtable from one script to another script in Jenkins Pipeline

在本地系统中,我可以通过传递哈希表来执行 powershell 脚本,但在 Jenkins 管道中却行不通。我尝试了多种方式,但其中 none 有效。你能帮我解决这个问题吗?

我需要一些解决方案

  1. 如何将 powershell 哈希表传递给 groovy 命令

  2. 如果上述方法不起作用,我如何从 powershell 脚本块执行 powershell 脚本

     stage('Passing Hashtable Test')
             {
                 steps{
    
    
                     script{
                             powershell '''
    
                             write-host "hello Passing Hashtable "
    
                             $dict = @{ 
                             "firstKey" = "abc";
                             "User" = "Robot"
                             }
    
                             Write-host "Keys are present here : " + $dict.Keys   
    
                             $env:WORKSPACE\PassingDictonary.ps1 -param1 "ThisIsStringValuePassed" -myHashTable $dict     
    
                             '''
                              echo "priting outside script1 $dict"  //how to get Powershell $dict variable here ?
    
               //Atleast If I can send hashtable to ps1 file then its good  . I tried this but not working
    
    
           //OR At least If I can get Powershell variable outside of the block .. so that I would like to use a bat command by using -file parameter but I am not able to get value here.
    
    
    
    
                 }
             }
         }
    

这是我的 PassingDictionary。ps1 文件代码(这是在本地工作)

param (

    [string]$param1 = "something",
    [hashtable]$myHashTable
)


Write-host "Param1 value is : " + $param1

write-host "length of my Passed Hashtable is :" + $myHashTable.Count


foreach($k in $myHashTable.Keys)
{

    Write-Host "Prining myHashTable Key is : => " + $k

    Write-Host "Prining myHashTable Value  is : => " + $myHashTable[$k]


}

最后我能够从 Jenkins 管道中的 powershell 块执行 powershell 脚本,如下所示(这为我上面提到的第二个问题提供了解决方案)

script {

   powershell ''' 
   PowerShell(. '.\PassingDictonary.ps1' -param1 "ThisIsStringValuePassed"     
      -myHashTable $dict) 

    ''' 
 }