OpsaC - Operating as PowerShell code
published: December 27, 2020 author: Tinu tags: PowerShell categories: PowerShell-Module
Find-Module PSake
Install-Module PSake
Get-Module PSake -ListAvailable
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 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 -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
<#
.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
}
How to Automate Tasks with PSake and PowerShell