/
runWorkflowWithMimeInput.ps1
executable file
·80 lines (66 loc) · 3.39 KB
/
runWorkflowWithMimeInput.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Param(
[string]$user = 'myvROUser',
[string]$pass = 'myPassword',
[string]$vroHost = 'vRO-Server.domain.lab',
[string]$vroPort = '8281',
[string]$wfid = '8A8080808080808080808080808080809C8080800127197184441049b4e6c2cc2',
[string]$apiFormat = 'json', # either xml or json
[Parameter(Mandatory=$true)]
[System.IO.FileInfo] $file
)
#### Make changes above this line AND Lines 56-> 61 #########################################
# Usage:
# If you run the script with no parameters specified, the default values defined above will be used.
# to run with params, See following example: (Should be all one line)
# NOTE: It is not required to specify name of each parameter, but order will need to match the order in the above params section
# PS E:\> .\runWorkflowWithMimeInput.ps1 -user vcoadmin -pass vcoadmin -vroHost vro6.demo.lab -vroPort 8281 -wfid 8A8080808080808080808080808080809C8080800127197184441049b4e6c2cc2 -apiFormat json -file C:\hol\vcd-users.csv
#
# The vcd-users.csv file and the workflow with that id (Import Users from CSV) reside with this script on Github so that you may test before
# customizing to fit your own needs
#
#############################################################################################
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
function Encode-File($string){
$Content = Get-Content -Path $string -Encoding Byte
$encoded = [System.Convert]::ToBase64String($Content)
return $encoded;
}
$body = (Encode-File($file.FullName))
#### Make changes here to build your json body for the POST operation #######################
#
$json = '{"parameters": ['
$json += '{"name":"csvFile","type":"MimeAttachment","scope":"local","value": {"mime-attachment":{"name":"' + $file.Name + '","mime-type":"application/octet-stream","content":"'+$body+'"}}}'
# Modify the line above to have a comma at the end and add more input params as needed, each with a comma at the end, except for last one.
$json += ']}'
# Uncomment the next line to test your generated json, use www.jsonlint.com to validate if necessary
# Write-Output $json
#############################################################################################
$token = ConvertTo-Base64("$($user):$($pass)")
$auth = "Basic $($token)"
$headers = @{"Authorization"=$auth;"Content-Type"="application/$($apiFormat)";"Accept"="application/$($apiFormat)"}
# $body = Get-Content $inputFile -Raw
# write-output "Using body: " + $body
$URL = "https://$($vroHost):$($vroPort)/vco/api/workflows/$($wfid)/executions"
Write-Output $URL
$ret = Invoke-WebRequest -Method Post -uri $URL -Headers $headers -body $json
$headers = $ret.Headers
ForEach ($header in $headers){
Write-Output $header
}