Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Ivanti Automation new API PowerShell functions
- .DESCRIPTION
- These functions will help you script Ivanti Automation
- .NOTES
- Workspace-guru.com
- .FUNCTIONS
- - new-IVAconnection
- - get-IVAcompletedjobs
- - get-IVAactivejobs
- - get-IVAscheduledjobs
- - get-IVAagents
- - get-IVAjobstatus
- - get-IVArunbooks
- - start-IVArunbook
- #>
- function new-IVAconnection {
- <#
- .SYNOPSIS
- Get sigin in and get token
- .DESCRIPTION
- sign in and get token
- .PARAMETER Username
- Username for Ivanti Automation
- .PARAMETER Uri
- Ivanti Automation Management Portal URL
- .PARAMETER Password
- Password of Ivanti Automation Management Portal Users
- .EXAMPLE
- new-IVAconnection -Username "API" -Password 'P@ssw0rd' -Uri "https://automation01.fabrikam.com"
- .NOTES
- workspace-guru.com
- #>
- Param(
- [Parameter (Mandatory = $true)]
- [String] $Username,
- [String] $Uri,
- [String] $Password
- )
- $login = "$uri" + "/Automation/API/Auth/Login"
- $json_token = @{'userName' = $Username; 'password' = $Password}
- $json_token = ConvertTo-Json $json_token
- $token = Invoke-WebRequest -URI $login -Method POST -Body $json_token -ContentType 'application/json' | ConvertFrom-Json
- $token = $token.token
- if(!$token) {
- write-error "Could not connect to the API. Please check username, password and uri."
- }else{
- Write-host -ForegroundColor GREEN "Succesfully signed in token is $token"
- Set-Variable -Name Token -value $token -Scope global
- Set-Variable -Name Uri -value $uri -Scope global
- }
- }
- function get-IVAcompletedjobs {
- <#
- .SYNOPSIS
- Gets Ivanti Automation completed jobs from history
- .DESCRIPTION
- Gets Ivanti Automation completed jobs from history
- .EXAMPLE
- get-IVAcompletedjobs
- .NOTES
- Workspace-guru.com
- #>
- Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/History/Search?criteria.jobStatus=Completed | convertfrom-json
- }
- function get-IVAactivejobs {
- <#
- .SYNOPSIS
- Gets Ivanti Automation active jobs
- .DESCRIPTION
- Gets Ivanti Automation Active jobs
- .EXAMPLE
- get-IVAactivejobs
- .NOTES
- Workspace-guru.com
- #>
- Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/Activity/Search?criteria.jobStatus=Active | convertfrom-json
- }
- function get-IVAscheduledjobs {
- <#
- .SYNOPSIS
- Gets Ivanti Automation Scheduled jobs
- .DESCRIPTION
- Gets Ivanti Automation Scheduled jobs
- .EXAMPLE
- get-IVAscheduledjobs
- .NOTES
- Workspace-guru.com
- #>
- Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/Scheduled/Search?criteria.jobStatus=Scheduled | convertfrom-json
- }
- function get-IVAagents {
- <#
- .SYNOPSIS
- Gets Ivanti Automation Agents
- .DESCRIPTION
- Gets Ivanti Automation Agents
- .EXAMPLE
- get-IVAagent | where-object {$._name -eq "Agent01"} | select-object id
- .NOTES
- Workspace-guru.com
- #>
- (Invoke-WebRequest -Method GET -uri $uri/Automation/API/Agents/Search | convertfrom-json).result
- }
- function get-IVAjobstatus {
- <#
- .SYNOPSIS
- Get info for a job if it's running completed or failed
- .DESCRIPTION
- Check if the job is running completed or failed
- .PARAMETER Jobid
- Job GUID
- .EXAMPLE
- get-IVAjobstatus -JobID "6fc691a1-4f33-4c10-90a3-abd34d6e93c6"
- .NOTES
- Workspace-Guru.com
- #>
- Param(
- [Parameter (Mandatory = $true)]
- [String] $Jobid
- )
- $job = Invoke-Restmethod -Method GET -Uri "$uri/Automation/API/Jobs/$jobid"-ContentType 'application/json'
- if ($job.endtime -eq $null) {
- write-output "Job is running..."
- }
- if ($job.jobStatus -eq "Failed") {
- write-output "Job failed"
- Write-Error "Job failed"
- }
- if ($job.jobStatus -eq "Completed") {
- write-output "Job completed"
- }
- }
- function start-IVArunbook {
- <#
- .SYNOPSIS
- Start an Ivanti Automation Run Book
- .DESCRIPTION
- Start an Ivanti Automation Run Book. The run book must already have an agent or you could use the RunBookWho parameter.
- .PARAMETER RunbookID
- Run Book ID example: {8FD2A5E3-1BA4-49CD-9354-D0529F0F09F4}
- .PARAMETER Description
- A free description for the job
- .PARAMETER Paramters
- Enter Ivanti Automation Parameters in object form example: @{'0\Paramter1' = "yes";'0\RunBookWho' = "Agent01"}
- .EXAMPLE
- start-IVArunbook -Description "Test" -RunbookID {8FD2A5E3-1BA4-49CD-9354-D0529F0F09F4}
- .NOTES
- Workspace-Guru.com
- #>
- Param(
- [Parameter (Mandatory = $true)]
- [String] $RunbookID,
- [String] $Description,
- [Parameter (Mandatory = $false)]
- [Object] $Paramters
- )
- # Get all relevant data to create job
- $url = $uri + "/Automation/API/Schedule/Create/" + [uri]::EscapeUriString($runbookID) + "?type=Runbook"
- $result = Invoke-Restmethod -Method GET -Uri $url -ContentType 'application/json' -Headers @{"Authorization" = $Token} -ErrorAction Stop
- # Refresh job
- $result.description = $description
- $result.parameterValues = $paramters
- $url = $uri + "/Automation/API/Schedule/Refresh"
- $body = ConvertTo-Json ($result) -Depth 50
- $result = Invoke-Restmethod -Method PUT -Uri $url -Body $body -ContentType 'application/json' -Headers @{"Authorization" = $token} -ErrorAction Stop
- # Create job
- $url = $uri + "/Automation/API/Schedule/Schedule"
- $body = ConvertTo-Json ($result) -Depth 50
- $result = Invoke-Restmethod -Method POST -Uri $url -Body $body -ContentType 'application/json' -Headers @{"Authorization" = $token} -ErrorAction Stop
- $result
- }
- function get-IVArunbooks {
- <#
- .SYNOPSIS
- Get Ivanti Automation Run Book data
- .DESCRIPTION
- Get Ivanti Automation Run Book data
- .EXAMPLE
- get-IVArunbooks | Where-Object {$_.name -eq "Test"} | select-object id
- .NOTES
- General notes
- #>
- (Invoke-WebRequest -Method GET -uri $uri/Automation/API/Runbooks/Search | convertfrom-json).result
- }
Advertisement
Add Comment
Please, Sign In to add comment