如何在Powershell脚本中使用Jenkins管道方法参数

时间:2018-07-31 21:12:49

标签: powershell jenkins groovy jenkins-pipeline

我在jenkins声明性管道中定义了以下方法,但无法使用变量“ serverName”,“ archiveDestination”和“ wwwrootPath”。我已经尝试了不同的方法,但是没有用。 Write-Output的输出仍然是“ $ serverName”,$ {archiveDestination}和/或一个空变量。

如何从Powershell脚本部分中的serverdeployment方法访问参数?

def serverdeployment(serverName, archiveDestination, wwwrootPath) {
    powershell script: '''
    $servername = \'"$serverName"'
    $archivedestination = \'${archiveDestination}'
    $wwwrootpath = ${wwwrootPath}
    Write-Output $servername
    Write-Output $archivedestination
    Write-Output $wwwrootpath
    $pass = ConvertTo-SecureString -AsPlainText "XXXXX" -Force
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList "XXXXX",$pass
    $sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
    $session = New-PSSession -ComputerName $servername -UseSSL -Credential $cred -SessionOption $sessionOption
    Copy-Item $env:WORKSPACE\\* -Destination "$archivedestination" -Filter *TEST* -Recurse -Force -Verbose -ToSession $session
    Invoke-Command -Session $session -ScriptBlock {iisreset /STOP}
    $filename = $env:JOB_NAME + "_TEST_" + $env:Version + "_" + $env:BUILD_NUMBER + "_wwwroot.7z"
    Invoke-Command -Session $session -ScriptBlock { & "C:\\Program Files\\7-Zip\\7z.exe" x $using:archivedestination$using:filename -o$wwwrootpath -aoa >$null }
    Remove-Item "$wwwrootpath\\*\\_Cache\\*" -Recurse -Force
    Invoke-Command -Session $session -ScriptBlock {iisreset /START}
    Remove-PSSession $session
    Exit-PSSession
    '''
}

此方法将在步骤部分中执行:

serverdeployment('myservername', 'C:\\data\\install\\', 'C:\\inetpub\\wwwroot\\') 

0 个答案:

没有答案
相关问题