How to execute multiple powershell command from single session

1.5k Views Asked by At

I have tried to execute below commands from Jenkins consecutively:

Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
cd IPL:

But it seems like Jenkins is releasing the session after executing each command. Hence we have tried to keep a delay between two command, but no luck:

Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
Start-Sleep -s 5
cd IPL:

While executing script, Jenkins is taking each command from its workspace directory D:\jenkins\workspace\<JobName>. I was looking to modify the configuration in such a way, where Jenkins will execute entire script (with all commands within) from the same drive where script is located. Not from the Jenkins workspace.

But not such materials available in google. I have looked into the Jenkins Workspace modification area (Jenkins->Manage Jenkins->Configure System and click on the Advanced)

Jenkins Workspace Modification

But it will not help as, it will only change the workspace path and whenever we will execute the script. Again it will take individual command inside workspace and will execute them over there instead of directory where script is located.

Is there any way can we execute all the command (from a single powershell script) without terminate the session for each command? So that the powershell script script can be executed for it own directory only?

2

There are 2 best solutions below

4
4c74356b41 On

Anyway, to answer this question (not touching the SCCM part).

Jenkins will execute all the command that are in the SAME step during the same Powershell session, so if you have 1 step in your Jenkins job, all the commands you put in there will be executed in the same PS shell.

As for the second question, you can use Set-Location to change the current working directory of powershell.

0
Krish On

Windows Powershell was running using \Administrator account while Jenkins was running with System Account. So we have change the Jenkins service Logon from System account to Domain account: First, downloaded “PsExec.exe” and execute following command from command prompt ("C:\Users\Administrator.DUMMYIPSL\Desktop\PsExec.exe" –i –s cmd.exe) / Second, opened up Jenkins service and open the properties tab. Now from the Logon tab to change the priviliges from System account to the Domain Admin account and restarted the Jenkins service to execute the script. / Third, then triggered the build from Jenkins and it worked.