The implementation PowerShell script for getting started with Jenkins

Source: Internet
Author: User

The previous section mentions PowerShell, and if you need to build complex tasks, it's not very convenient to use cmd (or maybe because I'm not familiar with the cmd command, laughed at here), and this is where PowerShell comes in handy, and the PowerShell script is not detailed here. If there is a need to open another section later to explain PowerShell basic commands, PowerShell object-oriented programming, PowerShell script debugging, C # writing PowerShell cmdlets, PowerShell-related tools, etc. this article focuses on how to run PowerShell commands in the Jenkins environment.

There are two main ways to run PowerShell scripts in the Jenkins environment: running PowerShell through Windows Batch command and running PowerShell with the PowerShell Jenkins plugin

    1. Run PowerShell using Windows Batch command. The main thing here is to use Powershell.exe to execute the PowerShell script, which is not the same as executing the EXE with CMD under Windows.

      Below we create a new project named Runpowershellincmd, still choose Freestyle Project, we skip a few columns unrelated to the current project, go directly to build click

      Select the Red Box command

      The following command box appears on the interface, as we have said, to enter a variety of CMD commands as in CMD.

      We enter the following command and then click Save

      Click Save, and then execute build now to do this once. We can see that the command was executed successfully.

      Click Build Record # # Enter details, then click on the left console output to view the output details

      We can see that Jenkins actually creates a temporary bat command to execute a cmd command. There is no output because the current command execution environment is the current project directory under the Jenkins installation directory, which is empty by default, so there is no content, If we're writing a command. If you specify-path to specify which directory to list for PowerShell, you will see that the contents of this directory are output here. Of course, you can also add some random content under your current working directory, and then build again to see how the output changes.

      You can also specify parameters when using Powershell.exe to execute PowerShell scripts, noting that the parameters here are not parameters of the PowerShell command, but rather the parameters that are supported by the Powershell.exe runtime.

      Please see the following documents for the supported parameters:

      Https://technet.microsoft.com/zh-cn/library/dd315276.aspx

      Here are a few useful parameters:

The-executionpolicy script executes the policy by default.

The default execution policy for PowerShell scripts, if you want to execute a remote script, you must set this option to remotesigned, unrestricted, or bypass. For PowerShell execution policies, see the following documents

Https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6

-comand the command to execute, such as the one we performed above is Get-childitem.

-file the PowerShell script file to be executed, if the script content is very long, we tend to save it as a PS1 file, where the PS1 file is located by the-file parameter.

-sta single-threaded enable PowerShell, if there is non-thread-safe code in the script, multithreaded boot may cause data state errors, you need to specify-sta.

-help get help with PowerShell, a general program provides a parameter that allows you to quickly find some help you want without having to look at a document or note

    1. Run PowerShell scripts through plugins.

      Many times it is not very convenient to implement PowerShell via Powershell.exe, and fortunately there are plugins in Jenkins that can run PowerShell scripts directly. Let's see how to use

      If you are not in the Jenkins main screen, click the top left corner to switch to the main screen, then click the Manage Jenkins button on the left.

      Enter the following interface:

      Click to enter the following screen

      Everyone's may be slightly different from mine, because I have installed some plugins, so here will be prompted to upgrade.

      Click on the Available tab above

      We enter PowerShell search PowerShell plugin in the filter box

      At this point we tick the checkbox in front of the PowerShell plugin and click the Install without restart button

      At this time into the plug-in installation interface:

      Wait a minute. We found that the PowerShell installation was successful

      Tick restart Jenkins when installation be complete and no jobs be running check boxes in front of to enable Jenkins to restart for plug-ins to take effect

      When the Jenkins restart is complete, we create a new task called Executepscmd.

      When we click on Add Step Build, if the PowerShell installation is successful, an extra option will be given

      We click on it and add the following command:

      Note Here we can add the same commands as in the Local PowerShell window. Some little buddies might see me here. get-date are all lowercase, but many tutorials start with the first letter capitalized. PowerShell is a case insensitive scripting language, complete lowercase is ok.

      Click Save to execute build now and then open the # # task record to see what the window output is:

      Jenkins PowerShell plugin successfully executes a PowerShell script

The implementation PowerShell script for getting started with Jenkins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.