How to use Jenkins pipeline method parameters in Powershell script?

167 views
Skip to first unread message

Daniel Christian Schröter

unread,
Aug 3, 2018, 8:19:24 AM8/3/18
to Jenkins Users
I ask this question already on https://stackoverflow.com/questions/51622150/how-to-use-jenkins-pipeline-method-parameters-in-powershell-script, but maybe here is someone to help me.

I defined the following method in jenkins declarative pipeline, but I am not able to use the variables/output from "serverName", "archiveDestination" and "wwwrootPath". I tried already different ways but is does not work. The output from Write-Output is still "$serverName", ${archiveDestination} and/or an empty variable.

How can I access the parameters from the serverdeployment-method in the Powershell script part?

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
   
'''
}

This method will be executed in steps part:

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

Thank you!


Reply all
Reply to author
Forward
0 new messages