The previous section mentioned PowerShell, and if you need to build a complex task, it’s not very convenient to use CMD (and maybe because I’m not familiar with the CMD command, it’s a joke), and then PowerShell is used, and it doesn’t introduce power in detail.Shell scripts, if needed, explain the PowerShell basic commands in a separate chapter, PowerShell object oriented programming, PowerShell script debugging, C# writing PowerShell cmdlet, PowerShellThis article mainly introduces how to run PowerShell commands in Jenkins environment.
There are two main ways to run PowerShell scripts in the Jenkins environment: running PowerShell through windows batch command and using the PowerShell Jenkins plug-in to run powerShell
- Using Windows batch command to run powershell. here is mainly to use powershell.exe to execute PowerShell scripts, and there are no two ways to execute exe with CMD under Windows.
Next we create a new project called RunPowershellInCmd, and we still choose FreeStyle Project. We skip several columns that are not related to the current project and go directly into the Build click.
Select the command of the red box
At this time, the following command box will appear on the interface. We have said that we can input all kinds of CMD commands like here in CMD.
After we enter the following commands, we click save
After clicking save, then execute Build Now to execute a build. We can see that the command is executed successfully.
Click the build record #1 to enter the details, then click the left console output to view the output details.
We can see that Jenkins actually created a temporary bat command to execute a CMD command. There is no output here because the execution environment of the current command is the current project directory under the Jenkins installation directory, which is empty in the default, so there is no content, such asIf we specify the -path to specify which directory to list in the PowerShell command, you will see the contents out of this directory here. Of course, you can also add some random content under the current working directory, and then perform a build to see the output.Changes in content.
When using the powershell.exe to execute the PowerShell script, you can also specify some parameters, and notice that the parameters here are not the parameters of the PowerShell command, but the parameters that are supported at the powershell.exe runtime.
Support the parameters of the following documents:
Here are a few useful parameters:
-ExecutionPolicyThe script executes the policy by default.
PowershellThe default execution strategy of the script, if the remote script is to be executed, it must be set to RemoteSigned, Unrestricted, or Bypass. for the PowerShell execution policy, see the following document
-Comand The command to execute, for example, we perform get-childitem on top.
-File To execute the PowerShell script file, if the script is very long, we often save it as a PS1 file, where the -File parameter is used to specify the location of the PS1 file.
-StaSingle thread enable PowerShell. If there is non thread safe code in script, multithread startup may cause data state error. At this time, -sta. must be specified.
-help With the help of PowerShell, the general program provides such a parameter that can quickly find some of the help you want without looking at a document or note.
- Run the PowerShell script through the plug-in.
It’s not very convenient to execute PowerShell through powershell.exe a lot of the time. Fortunately, there is a plug-in that runs the PowerShell script directly in Jenkins. Here’s a look at how to use it.
If you are not in the Jenkins main interface, click the top left corner logo to switch to the main interface, then click the Manage Jenkins on the left.Button
Enter the following interface:
clickEnter the following interface
Everyone may be slightly different from mine, because I have installed some plug-ins, so this will prompt upgrading.
Click the Available tag above
We enter Powershell search PowerShell plug-in in the Filter box.
At this point, we check the check box in front of the PowerShell plug-in and click the Install Without Restart button.
This time to enter the plug-in installation interface:
After a while we found the PowerShell installation successful
Check the check box in front of Restart Jenkins when installation is complete and no jobs are running, so that the reboot will enable the plug-in to take effect.
When Jenkins is restarted, we will create a new task called ExecutePsCmd.
When we click on Add Step Build, if PowerShell is installed successfully, there will be more than one option.
We click on it and add the following commands:
Note here that we can add the same commands as in the local PowerShell window. Some small partners may see me here get-date is all lowercase, but a lot of tutorials start with the initial initials.Powershell is a case insensitive scripting language, complete the wholeThe lower case is also OK.
After clicking save, execute Build Now and then open the #1 task record to see the output of the window:
Jenkins powershellThe plug-in successfully executes a PowerShell script