Posted On December 10, 2020

How to Create a New Jenkins Job to Call a PowerShell Script with Credentials

kimconnect 0 comments
blog.KimConnect.com >> Windows >> How to Create a New Jenkins Job to Call a PowerShell Script with Credentials
1. PowerShell & Secured Credentials Support
  1. Check for PowerShell support: Manage Jenkins > Plugin Manager > Installed > set filter = PowerShell > verify that ‘PowerShell plugin’ is on the list. If not, the proceed to part (b). Otherwise, skip to part (c)
  2. Install PowerShell plugin: Manage Jenkins > Manage Plugins > Available > search for keyword ‘PowerShell’ > select PowerShell > click ‘Download now and install’
  3. Check for Credentials support: Manage Jenkins > Plugin Manager > Installed > set filter = Credentials > verify that ‘Credentials’ is on the list. If not, the proceed to part (d). Otherwise, skip part (d)
  4. Install Credentials plugin: Manage Jenkins > Manage Plugins > Available > search for keyword ‘Credentials’ > select Credentials > click ‘Download now and install’ > reoeat for ‘Credentials Binding’ plugin
2. Create a new Jenkins job
  1. New Item > select FreeStyle project > input a job name > Save > Input some values into the Description field
  2. Put a check mark next to ‘Discard old builds’ > set Max # of builds to keep = 10
  3. Select ‘This project is parameterized’ > Add Parameter > choose String Parameter > set name = username, Default value = [username to be called by script] > Add Parameter, again > choose Password Parameter > set name = password, Default value = [password to be called by script]
  4. Under Build Triggers, put a check mark next to Build periodically > set Schedule = H/5 * * * * (every 5 minutes) or 0 2 * * * (everyday at 2am). Here’s a quick overview of Cron expressions on the digit positions:

    1. MINUTES: Minutes in one hour (0-59)
    2. HOURS: Hours in one day (0-23)
    3. DAYMONTH: Day in a month (1-31)
    4. MONTH: Month in a year (1-12)
    5. DAYWEEK: Day of the week (0-7) where 0 and 7 are sunday

      Notice that the the asterisk ‘*’ in the first position is to be substituted with an ‘H’. That is because a hash of the job name as a time value is used to ensure consistent job starting time as well as ‘spreading the load evenly’ with other job starts to avoid overlapping.
  1. Under Build section, click Add build step > select PowerShell > set Command = powershell.exe -File [location of the script file] > Save
3. Create a script to peruse Jenkins credentials variables
# Obtain credentials being passed by Jenkins
$username=$env:username
$password=$env:password
$encryptedPassword=ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$encryptedPassword;

# The rest of the script...

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PEN TEST

Kali Linux: Pen testing Linux Distro: https://www.kali.org/get-kali/ VMware Images: https://www.kali.org/get-kali/#kali-virtual-machines AirCrack-NG: WIFI Acunetix: Web Servers…

Block USB in Windows 7 using Group Policy

Create and assign a group policy to the appropriate OU with this configuration: Computer Configuration…

How To Add or Remove a Path in Windows Environmental Paths

There are 2 functions to Add and Remove, at your convenience: # addEnvironmentalPath.ps1 $pathToAdd='C:\Scripts' $pathToRemove='C:\Scripts'…