Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Automate Tasks with PSake

tags: PowerShell categories: PowerShell-Module

Table of Contents

Install PSake

Find-Module PSake

Install-Module PSake

Get-Module PSake -ListAvailable

Create a Basic Task

Create a new file named ‘psakefile.ps1’

New-Item psakefile.ps1 -ItemType File

Add a single task with the TaskName HelloWord.

$Content = @"
task HelloWorld -Action {
    Write-Host "Hello $($env:USER)" -ForegroundColor Green
}
"@

Add-Content -Path .\psakefile.ps1  -Value $Content -Encoding utf8

Create a Task as file

Create a new file named ‘psakefile.ps1’

New-Item psakefile.ps1 -ItemType File

Add a single task with the TaskName HelloWord.

$Content = @"
task HelloWorld -Action {
    Write-Host "Hello $($env:USER)" -ForegroundColor Green
}
"@

Add-Content -Path .\HelloTest.ps1  -Value $Content -Encoding utf8

Import the file into psakefile.ps1 using the Include Function.

$Content = @"
Include "$PSScriptRoot\HelloTest.ps1"
"@

Add-Content -Path .\psakefile.ps1  -Value $Content -Encoding utf8

Invoke-PSake

Invoke-PSake -BuildFile .\psakefile.ps1 -TaskList 'HelloWorld'

psake version 4.9.0
Copyright (c) 2010-2018 James Kovacs & Contributors

Executing HelloWorld
Hello Tinu

psake succeeded executing .\psakefile.ps1

Build Time Report

Name       Duration
----       --------
HelloWorld 00:00:00.009
Total:     00:00:00.040

Example

<#
.SYNOPSIS
    PSake script

.DESCRIPTION
    PSake script with all tasks to execute

.EXAMPLE
    Invoke-psake -buildFile /Users/Tinu/Temp/psakefile.ps1
#>

# FormatTaskName

# Default task, define the sequence here
Task default -Depends Initialize, DowloadInputFileFromJira, ValidateAndImportCSV, CompareDevOpsDB, UploadInputFileToGit

Properties {
    $Script:DownloadExists  = $false
    $Script:ValidateSuccess = $false
    $Script:CompareSuccess  = $false
}

Task Initialize -PreCondition {
    if([String]::IsNullOrEmpty((Get-InstalledModule -Name JiraPS -ErrorAction SilentlyContinue))){
        $true
    }else{
        $false
    }
} -Action {
    if($error){$error.Clear()}
    Write-Host "[START] Install-Module -Name JiraPS -Scope CurrentUser -AllowClobber..." -ForegroundColor Green
    Install-Module -Name JiraPS -Scope CurrentUser -AllowClobber
} -PostAction {
    Write-Host "[ END ] Install-Module -Name JiraPS" -ForegroundColor Green
}

Task DowloadInputFileFromJira {
    Write-Host "[START] Dowload CSV from Jira..." -ForegroundColor Green
    $Script:DownloadExists = $true
} -PostCondition {$Script:DownloadExists} -PostAction {
    Write-Host "[ END ] Dowload CSV from Jira: $($Script:DownloadExists)" -ForegroundColor Green
}

Task ValidateAndImportCSV -PreCondition {$Script:DownloadExists} {
    Write-Host "[START] Validate and import CSV..." -ForegroundColor Green
    $Script:ValidateSuccess = $true
} -PostCondition {$Script:ValidateSuccess} -PostAction {
    Write-Host "[ END ] Validate and import CSV: $($Script:ValidateSuccess)" -ForegroundColor Green
}

Task CompareDevOpsDB -PreCondition {$Script:ValidateSuccess} {
    Write-Host "[START] Compare CSV with DevOpsDB..." -ForegroundColor Green
    $Script:CompareSuccess = $true
} -PostCondition {$Script:CompareSuccess} -PostAction {
    Write-Host "[ END ] Compare CSV with DevOpsDB: $($Script:CompareSuccess)" -ForegroundColor Green
}

Task UploadInputFileToGit -PreCondition {$Script:CompareSuccess} {
    Write-Host "[START] Upload CSV to git..." -ForegroundColor Green
} -PostAction {
    Write-Host "[ END ] Upload CSV to git" -ForegroundColor Green
}

See also

psake.readthedocs.io

github.com psake

How to Automate Tasks with PSake and PowerShell

[ Top ] [ Blog ]