Jenkins is the the top and leading open source automation server, It offers hundreds of plugins to help building (develop), deploying and automating any project. It is a great orchestration tool for automation. In this article I will use it to schedule a simple Job for PowerShell script using Git.
I have installed Jenkins server on Microsoft Windows OS. To use PowerShell and Jenkins together first I will install the necessary PowerShell plugin in the Jenkins server. After login into Jenkins, Click Manage Jenkins on the dashboard. Select Manage Plugins from list.
Under Available plugins, search for PowerShell, Select PowerShell Plugin from list, Either click Install without restart or Download now and install after restart button. PowerShell Plugin allows Jenkins to invoke PowerShell as build scripts. It uses PowerShell.exe on Windows and pwsh on Linux.
Once the PowerShell plugin is installed successfully, you can find it under Installed plugins option, search for the plugin and verify that it is installed. Make sure it is Enabled.
I have written a small and one-liner PowerShell script which only echo/print write output Hello World! text on the console. I have pushed all my scripts on the git server (On premises git server), you can check the article Step by Step guide to push your first project to github.com. I will use the configured git URL, I will copy it and use it in the later steps in the Jenkins configuration.
Note: the PowerShell ps1 script name is psjen-demo.ps1.
Next on the Jenkins Dashboard click New Item to create a new PowerShell job.
Type enter an item name, Select Freestyle Project (This is the central feature of Jenkins. Jenkins will build your project, combining any SCM with any build system and this can be even used for something other than software build). Click OK button.
In the freestyle job configuration page, On the General tab Provide job a Description and make sure it is Enabled.
Click Source Code Management, choose Git, Enter repository URL as copied earlier from git server. If required add Credentials. Click Save button. If you are seeing git related error make sure you install SCM Git on the operating system where this Jenkins is installed.
Next choose Build Steps, and click Add build step, from drop down list select PowerShell option (If you don't see PowerShell option in the drop down list make sure you have installed PowerShell Plugin in Jenkins).
In the Command textbox enter the PowerShell ps1 filename as . dot sourced. This ps1 file is the same uploaded on the git server earlier as shown Step by Step guide to push your first project to github.com. Save the Job. (Instead of script file you can add multiple commands directly)
After saving the Project Job, click Build Now button. It will execute the build. Navigate Under Build History, You can see the first task is executed successfully with green check mark.
After Clicking on the Job, It shows more information and options of executed task. Click Console Output to view more information. I see git fetch is successful and it executed PS1 script file with PowerShell execution is successful and it shows successful message with results.
Useful Articles
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resolved issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell Invoke-WebRequest The request was aborted Could not create SSL TLS secure channel
PowerShell Invoke-WebRequest The underlying connection was closed: Could not establish trust relationship for the SSL TLS secure channel.