Workspace-Guru

IvantiAutomationFunctions.ps1

Jun 1st, 2019
7,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.     Ivanti Automation new API PowerShell functions
  4.    
  5.     .DESCRIPTION
  6.     These functions will help you script Ivanti Automation
  7.    
  8.     .NOTES
  9.     Workspace-guru.com
  10.  
  11.     .FUNCTIONS
  12.     - new-IVAconnection
  13.     - get-IVAcompletedjobs
  14.     - get-IVAactivejobs
  15.     - get-IVAscheduledjobs
  16.     - get-IVAagents
  17.     - get-IVAjobstatus
  18.     - get-IVArunbooks
  19.     - start-IVArunbook
  20.     #>
  21.  
  22. function new-IVAconnection {
  23.     <#
  24.     .SYNOPSIS
  25.     Get sigin in and get token
  26.    
  27.     .DESCRIPTION
  28.     sign in and get token
  29.    
  30.     .PARAMETER Username
  31.     Username for Ivanti Automation
  32.    
  33.     .PARAMETER Uri
  34.     Ivanti Automation Management Portal URL
  35.    
  36.     .PARAMETER Password
  37.     Password of Ivanti Automation Management Portal Users
  38.    
  39.     .EXAMPLE
  40.     new-IVAconnection -Username "API" -Password 'P@ssw0rd' -Uri "https://automation01.fabrikam.com"
  41.    
  42.     .NOTES
  43.     workspace-guru.com
  44.     #>
  45.    
  46.     Param(
  47.         [Parameter (Mandatory = $true)]
  48.         [String] $Username,
  49.         [String] $Uri,
  50.         [String] $Password
  51.     )
  52.    
  53.     $login = "$uri" + "/Automation/API/Auth/Login"
  54.     $json_token = @{'userName' = $Username; 'password' = $Password}
  55.     $json_token = ConvertTo-Json $json_token
  56.  
  57.     $token = Invoke-WebRequest -URI $login -Method POST -Body $json_token -ContentType 'application/json' | ConvertFrom-Json
  58.     $token = $token.token
  59.  
  60.     if(!$token) {
  61.  
  62.         write-error "Could not connect to the API. Please check username, password and uri."
  63.  
  64.     }else{
  65.  
  66.         Write-host -ForegroundColor GREEN "Succesfully signed in token is $token"
  67.  
  68.         Set-Variable -Name Token -value $token -Scope global
  69.         Set-Variable -Name Uri -value $uri -Scope global
  70.     }
  71.    
  72.    
  73. }
  74.  
  75. function get-IVAcompletedjobs {
  76.     <#
  77.    .SYNOPSIS
  78.     Gets Ivanti Automation completed jobs from history
  79.    
  80.    .DESCRIPTION
  81.    Gets Ivanti Automation completed jobs from history
  82.    
  83.    .EXAMPLE
  84.    get-IVAcompletedjobs
  85.    
  86.    .NOTES
  87.    Workspace-guru.com
  88.    #>
  89.  
  90.     Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/History/Search?criteria.jobStatus=Completed | convertfrom-json
  91. }
  92.  
  93. function get-IVAactivejobs {
  94.     <#
  95.    .SYNOPSIS
  96.     Gets Ivanti Automation active jobs
  97.    
  98.    .DESCRIPTION
  99.    Gets Ivanti Automation Active jobs
  100.    
  101.    .EXAMPLE
  102.    get-IVAactivejobs
  103.    
  104.    .NOTES
  105.    Workspace-guru.com
  106.    #>
  107.     Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/Activity/Search?criteria.jobStatus=Active | convertfrom-json
  108. }
  109.  
  110. function get-IVAscheduledjobs {
  111.     <#
  112.   .SYNOPSIS
  113.    Gets Ivanti Automation Scheduled jobs
  114.  
  115.   .DESCRIPTION
  116.   Gets Ivanti Automation Scheduled jobs
  117.  
  118.   .EXAMPLE
  119.   get-IVAscheduledjobs
  120.  
  121.   .NOTES
  122.   Workspace-guru.com
  123.   #>
  124.     Invoke-WebRequest -Method GET -uri $uri/Automation/API/Jobs/Scheduled/Search?criteria.jobStatus=Scheduled | convertfrom-json
  125. }  
  126.  
  127. function get-IVAagents {
  128.     <#
  129.    .SYNOPSIS
  130.     Gets Ivanti Automation Agents
  131.    
  132.    .DESCRIPTION
  133.    Gets Ivanti Automation Agents
  134.  
  135.    .EXAMPLE
  136.    get-IVAagent | where-object {$._name -eq "Agent01"} | select-object id
  137.    
  138.    .NOTES
  139.    Workspace-guru.com
  140.    #>
  141.     (Invoke-WebRequest -Method GET -uri $uri/Automation/API/Agents/Search | convertfrom-json).result
  142. }
  143.  
  144. function get-IVAjobstatus {
  145.     <#
  146.    .SYNOPSIS
  147.     Get info for a job if it's running completed or failed
  148.    
  149.    .DESCRIPTION
  150.    Check if the job is running completed or failed
  151.    
  152.    .PARAMETER Jobid
  153.    Job GUID
  154.    
  155.    .EXAMPLE
  156.    get-IVAjobstatus -JobID "6fc691a1-4f33-4c10-90a3-abd34d6e93c6"
  157.    
  158.    .NOTES
  159.    Workspace-Guru.com
  160.    #>
  161.    
  162.    
  163.     Param(
  164.         [Parameter (Mandatory = $true)]
  165.         [String] $Jobid
  166.     )
  167.     $job = Invoke-Restmethod -Method GET -Uri "$uri/Automation/API/Jobs/$jobid"-ContentType 'application/json'
  168.      
  169.     if ($job.endtime -eq $null) {
  170.         write-output "Job is running..."
  171.     }
  172.    
  173.     if ($job.jobStatus -eq "Failed") {
  174.         write-output "Job failed"
  175.         Write-Error "Job failed"
  176.     }
  177.    
  178.     if ($job.jobStatus -eq "Completed") {
  179.         write-output "Job completed"
  180.     }
  181.  
  182. }
  183.  
  184. function start-IVArunbook {
  185.     <#
  186. .SYNOPSIS
  187.  Start an Ivanti Automation Run Book
  188.  
  189. .DESCRIPTION
  190. Start an Ivanti Automation Run Book. The run book must already have an agent or you could use the RunBookWho parameter.
  191.  
  192. .PARAMETER RunbookID
  193. Run Book ID example: {8FD2A5E3-1BA4-49CD-9354-D0529F0F09F4}
  194.  
  195. .PARAMETER Description
  196. A free description for the job
  197.  
  198. .PARAMETER Paramters
  199. Enter Ivanti Automation Parameters in object form  example: @{'0\Paramter1' = "yes";'0\RunBookWho' = "Agent01"}
  200.  
  201. .EXAMPLE
  202. start-IVArunbook -Description "Test" -RunbookID {8FD2A5E3-1BA4-49CD-9354-D0529F0F09F4}
  203.  
  204. .NOTES
  205. Workspace-Guru.com
  206. #>
  207.  
  208.     Param(
  209.         [Parameter (Mandatory = $true)]
  210.         [String] $RunbookID,
  211.         [String] $Description,
  212.         [Parameter (Mandatory = $false)]
  213.         [Object] $Paramters
  214.     )
  215.  
  216.  
  217.     # Get all relevant data to create job
  218.     $url = $uri + "/Automation/API/Schedule/Create/" + [uri]::EscapeUriString($runbookID) + "?type=Runbook"
  219.     $result = Invoke-Restmethod -Method GET -Uri $url -ContentType 'application/json' -Headers @{"Authorization" = $Token} -ErrorAction Stop
  220.      
  221.     # Refresh job
  222.     $result.description = $description
  223.     $result.parameterValues = $paramters
  224.  
  225.     $url = $uri + "/Automation/API/Schedule/Refresh"
  226.     $body = ConvertTo-Json ($result) -Depth 50
  227.     $result = Invoke-Restmethod -Method PUT -Uri $url -Body $body -ContentType 'application/json' -Headers @{"Authorization" = $token} -ErrorAction Stop
  228.  
  229.     # Create job
  230.     $url = $uri + "/Automation/API/Schedule/Schedule"
  231.     $body = ConvertTo-Json ($result) -Depth 50
  232.     $result = Invoke-Restmethod -Method POST -Uri $url -Body $body -ContentType 'application/json' -Headers @{"Authorization" = $token} -ErrorAction Stop
  233.     $result
  234. }
  235.  
  236. function get-IVArunbooks {
  237.     <#
  238.     .SYNOPSIS
  239.     Get Ivanti Automation Run Book data
  240.    
  241.     .DESCRIPTION
  242.     Get Ivanti Automation Run Book data
  243.    
  244.     .EXAMPLE
  245.     get-IVArunbooks | Where-Object {$_.name -eq "Test"} | select-object id
  246.    
  247.     .NOTES
  248.     General notes
  249.     #>
  250.    
  251.     (Invoke-WebRequest -Method GET -uri $uri/Automation/API/Runbooks/Search | convertfrom-json).result
  252.    
  253. }
Advertisement
Add Comment
Please, Sign In to add comment