Skip to content

Instantly share code, notes, and snippets.

@lipkau
Last active January 3, 2018 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lipkau/f43bf4be92eb9b88a671cd087d1af262 to your computer and use it in GitHub Desktop.
Save lipkau/f43bf4be92eb9b88a671cd087d1af262 to your computer and use it in GitHub Desktop.
[AtlassianPS] [JiraPS] Set up the environment for JiraPS (Install, Import, Login)
function Initialize-Environment {
<#
.SYNOPSIS
Initialize the module
.DESCRIPTION
Setup the environment to work with JiraPS
.EXAMPLE
Initialize-Environment
-----------
Initialize the environment with default values
.EXAMPLE
Initialize-Environment -User "admin"
-----------
Initialize the environment with a non-default username
.EXAMPLE
Initialize-Environment -Server "https://corp.com"
-----------
Initialize the environment with a non-default server address
.EXAMPLE
Initialize-Environment -Force
-----------
Initialize the environment including updating the module
.NOTES
This functions need the "BetterCredentials" module
#>
[CmdletBinding()]
param (
# Username for authentication
[ValidateNotNullOrEmpty()]
[String] $UserName = "admin", # Set default value for the user you use the most
# Server address
[ValidateNotNullOrEmpty()]
[URi] $ServerURi = "https://powershell.atlassian.net", # Set default value for the server you use
# Use Bais auth for every request
#
# (Using the default Credentials. Specify in the command to overwrite them)
[Bool] $UseBasicAuthentication = $false, # Change to $true if wanted
# Force the environment to be updated
[Switch] $Force,
# Have the session returned
[Switch] $Passthru
)
begin {
if (!(
(Get-Module BetterCredentials -ListAvailable) -and
(Get-Module JiraPS -ListAvailable)
)) {
$Force = $True
}
if ($Force) {
# Force the installation of the latest version of the modules
Install-Module -Name BetterCredentials -Scope CurrentUser -AllowClobber -Force
Install-Module -Name JiraPS -Scope CurrentUser -Force
}
# Get credentials with "BetterCredentials" and store them if they are new
Import-Module BetterCredentials -Force
$cred = Get-Credential -UserName $UserName -Store
# Set Jira server and create session
Import-Module JiraPS -Force
Set-JiraConfigServer $ServerURi
if ($UseBasicAuthentication) {
Write-Verbose "Setting Default Parameter Values"
Get-Command -Module JiraPS |
Where-Object { $_.Parameters -and $_.Parameters.ContainsKey("Credential") } |
ForEach-Object { $global:PSDefaultParameterValues["$($_.Name):Credential"] = $cred }
}
else {
Write-Verbose "Creating a session"
$session = New-JiraSession -Credential $cred
}
if ($session -and $Passthru) {
Write-Output $session
}
}
}
# Create an alias for the function - feel free to customize
New-Alias -Name "InitEnv" -Value "Initialize-Environment" -ErrorAction SilentlyContinue
# Call the function - for already initializing
Initialize-Environment @args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment