{"id":33704,"date":"2016-04-21T10:54:54","date_gmt":"2016-04-21T05:24:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=33704"},"modified":"2016-04-21T12:06:41","modified_gmt":"2016-04-21T06:36:41","slug":"running-powershell-scripts-using-jenkins","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/running-powershell-scripts-using-jenkins\/","title":{"rendered":"Running Powershell scripts using Jenkins"},"content":{"rendered":"<p>Jenkins has been primarily <a title=\"devOps automation as a service\" href=\"http:\/\/www.tothenew.com\/devops-automation-consulting\">used for automating jobs<\/a> and tasks on Linux servers. In this blog, we will be configuring Jenkins to execute Powershell scripts on Windows. This blog will not be covering Jenkins server set up steps on Windows. You can refer to the following link for <a href=\"https:\/\/wiki.jenkins-ci.org\/display\/JENKINS\/Installing+Jenkins\">Jenkins installation. <\/a><\/p>\n<p><strong>Powershell Plugin<\/strong><\/p>\n<p>Jenkin&#8217;s Powershell plugin is a useful tool for running Powershell scripts on Windows servers via Jenkins.<\/p>\n<p><strong>Plugin installation<\/strong><\/p>\n<ul>\n<li>Login to Jenkins and navigate to <strong>Manage Jenkins &gt; Manage Plugins.<\/strong><\/li>\n<li>Click on the <strong>Available<\/strong> tab and Enter <code> PowerShell<\/code> in the filter box.<\/li>\n<li>Select the plugin showing by name PowerShell Plugin .<\/li>\n<li><strong>Download now and install after a restart<\/strong>.<\/li>\n<\/ul>\n<p>The PowerShell plugin is now installed.<\/p>\n<p>Now, let&#8217;s create a job on Jenkins as steps mentioned below:<\/p>\n<ul>\n<li>On Jenkins interface, click <strong>New Item<\/strong><\/li>\n<li>Enter Create File for the job name<code><\/code>. Select <strong>Freestyle project<\/strong><\/li>\n<li>Tick <strong>This build is parameterized<\/strong>. Expand the <strong>Add Parameter<\/strong> list and choose <strong>String Parameter<\/strong><\/li>\n<li>Again, Click the <strong>Add Parameter<\/strong> list and select <strong>Choice Parameter<\/strong>. Enter the options on new lines inside the <strong>Choices<\/strong> text box. Also, provide description for the options mentioned:<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-33705\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/05_powershell_jenkins_jobs.png\" alt=\"05_powershell_jenkins_jobs\" width=\"475\" height=\"768\" \/><\/p>\n<ul>\n<li>Scroll down to the <strong>Build <\/strong>option and <strong>Add build step.<\/strong><\/li>\n<li>Select <strong>Windows PowerShell, <\/strong>inside the text box where the below Powershell script is to be supplied:<\/li>\n<\/ul>\n<p>[js]<br \/>\n# Create Temp Directory where the files will be created.<br \/>\nif (-not(Test-Path -Path &#8216;C:\\temp&#8217;))<br \/>\n{<br \/>\nNew-Item -Path &#8216;C:\\temp&#8217; -ItemType directory<br \/>\n}<\/p>\n<p># Using the environment variables configured above.<br \/>\nSet-Content -Path &quot;C:\\temp\\$($env:Filename).txt&quot; -Value $env:Message[\/js]<\/p>\n<ul>\n<li>Click <strong>Save<\/strong><\/li>\n<\/ul>\n<p>The Job has been created.<\/p>\n<h1>Running the Job:<\/h1>\n<p>Go to the Jenkins home page and execute the job just created. Executing the job will bring a form with the options provided in the parameters specified as in the image below:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-33706\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/09_jenkins_powershell_job_paramaters.png\" alt=\"09_jenkins_powershell_job_paramaters\" width=\"896\" height=\"386\" \/><\/p>\n<ul>\n<li>Click <strong>Build<\/strong>.<\/li>\n<\/ul>\n<p>That&#8217;s it. Once the job is complete. A new file named <strong>Second File<\/strong> gets created in <strong>C:\\temp <\/strong>with content as <strong>This is a message from Jenkins!<\/strong><\/p>\n<p><strong>Executing Powershell scripts\/commands remotely<\/strong><\/p>\n<p>The above job creates a text file on the Jenkins server itself. To setup remote Powershell scripts we first need to configure Jenkins server for remote Powershell script execution. To enable remote Windows machine in to the WS-Man trusted list on Jenkins servers. Execute the below command on Powershell window on Jenkins server. The command will add all the remote machine to the trusted list.<\/p>\n<p><code>Set-Item WSMan:\\localhost\\Client\\TrustedHosts *<\/code><\/p>\n<p>Along with the commands, we would also need to enable remote script execution also. To enable execution of Powershell scripts remotely execute the following command in Powershell window on Jenkins server.<\/p>\n<p><code>Set-ExecutionPolicy RemoteSigned \u2013Force<\/code><\/p>\n<p>We will have to install a new plugin named EnvInject Plugin for transferring variables e.g. passwords.<\/p>\n<ul>\n<li>Login to Jenkins and navigate to <strong>Manage Jenkins &gt; Manage Plugins<\/strong><\/li>\n<li>Click on the <strong>Available<\/strong> tab and Enter <code>EnvInject<\/code> in the filter box<\/li>\n<li>Select the plugin showing by name <a href=\"https:\/\/wiki.jenkins-ci.org\/display\/JENKINS\/PowerShell+Plugin\">PowerShell Plugin<\/a><\/li>\n<li>Select Download<strong> now and install after restart<\/strong><\/li>\n<\/ul>\n<p><strong>Creating a job to restart Windows time service:<\/strong><\/p>\n<ul>\n<li>On Jenkins interface, click <strong>New Item<\/strong><\/li>\n<li>Enter Remote Powershell scripts for the job name. Select <strong>Freestyle project<\/strong><\/li>\n<li>Tick <strong>This build is parameterized<\/strong>. Create following parameters\n<ul>\n<li>Type: String Parameter<\/li>\n<li>Name: ServerIp\/Hostname<\/li>\n<li>Description: Remote machine&#8217;s IP address.<\/li>\n<li>Type: String Parameter<\/li>\n<li>Name: UserName<\/li>\n<li>Type: Password Parameter<\/li>\n<li>Name: Password<\/li>\n<\/ul>\n<\/li>\n<li>Now, Click <strong>Add Parameter<\/strong> list and select the <strong>Choice Parameter<\/strong>. Enter the options on new lines inside the <strong>Choices<\/strong> text box. Also, provide description for the options mentioned:<\/li>\n<\/ul>\n<p>[js]# Configure build failure on errors on the remote machine similar to set -x on bash script<br \/>\n$ErrorActionPreference = &#8216;Stop&#8217;<\/p>\n<p># Create a PSCredential Object using the &quot;Username&quot; and &quot;Password&quot; variables created on job<br \/>\n$Password = $env:Password | ConvertTo-SecureString -AsPlainText -Force<br \/>\n$creddentials = New-Object System.Management.Automation.PSCredential -ArgumentList $env:UserName, $Password<\/p>\n<p>.<br \/>\n# It depends on the type of job you are executing on the remote machine as to if you want to use &quot;-ErrorAction Stop&quot; on your Invoke-Command.<br \/>\nInvoke-Command -ComputerName $env:Computer -Credential $credentials -ScriptBlock {<br \/>\nRestart-Service -Name W32Time<br \/>\n}[\/js]<\/p>\n<p>Run the build and check the Windows logs on remote computer specified to verify if the Windows Time service has got restarted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jenkins has been primarily used for automating jobs and tasks on Linux servers. In this blog, we will be configuring Jenkins to execute Powershell scripts on Windows. This blog will not be covering Jenkins server set up steps on Windows. You can refer to the following link for Jenkins installation. Powershell Plugin Jenkin&#8217;s Powershell plugin [&hellip;]<\/p>\n","protected":false},"author":563,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":91},"categories":[2348,1],"tags":[3215,1682,3213,3216,3214,3212,472,3218,3217],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33704"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/563"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=33704"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33704\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=33704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=33704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=33704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}