-
Notifications
You must be signed in to change notification settings - Fork 0
/
vROPS_REST_with_Powershell.ps1
129 lines (105 loc) · 4.54 KB
/
vROPS_REST_with_Powershell.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# https://virtualg.uk/getting-started-with-the-vrops-rest-api
<#Authentication
The most challenging part of working out how to use the API was how the authentication works. Essentially, you need to form the authentication URL, create a JSON formatted credential then obtain a session ID for the subsequent requests.
I have put all of this together in the below sample PowerShell code.
You’ll probably not want to store credentials in the script, so be sure to find an alternative way to obtain the password (via Read-Host or a call to your password database API
#>
## Global Variables
$vrops_server = ""
$vrops_username = ""
$vrops_password = ""
## Ignore SSL Certificates
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
## vROps API Strings
$rest_url_base = "https://" + $vrops_server + "/suite-api/api/"
$rest_url_authenticate = "https://" + $vrops_server + "/suite-api/api/auth/token/acquire"
## Create JSON for Authentication
$json_authentication =
"{
""username"": ""$vrops_username"",
""password"": ""$vrops_password""
}"
## Authenticate with vROPs REST API
Try {
$rest_authenticate = Invoke-RestMethod -Method POST -Uri $rest_url_authenticate -Body $json_authentication -ContentType "application/json"
}
Catch {
$_.Exception.ToString()
$error[0] | Format-List -Force
exit
}
## Get Session ID from response
$rest_authentication_session_xml = @{"Authorization"="vRealizeOpsToken "+$rest_authenticate.'auth-token'.token
"Accept"="application/xml"}
<# Get Email Plugins
Once you have authenticated to the API, you can use the $rest_authentication_session_xml variable, containing the Session ID for subsequent requests.
The next code snippet will output all configured vROps email plugins #>
$rest_url_email_plugins = $rest_url_base+"alertplugins"
Try {
$ResourcesXML = Invoke-RestMethod -Method GET -Uri $rest_url_email_plugins -Headers $rest_authentication_session_xml
$plugins = $ResourcesXML.'notification-plugins'.'notification-plugin'
Write-Host "Available Email Plugins:" -ForegroundColor Blue
$plugin_id_array=@()
foreach ($plugin in $plugins ) {
if($plugin.pluginTypeId -ne "StandardEmailPlugin"){
continue;
}
$plugin_id_array += $plugin.pluginId
Write-Host "Plugin Name:" $plugin.name " | Plugin ID" $plugin.pluginId -ForegroundColor White
}
}
Catch {
$_.Exception.ToString()
Write-Host "Error getting email plugins. Exiting" -ForegroundColor Red
exit
}
<#
Get Remote Collectors
Next, you can amend the above code snippet to get all configured remote collectors
#>
$rest_url_remote_collectors = $rest_url_base+"collectors
Try {
$ResourcesXML = Invoke-RestMethod -Method GET -Uri $rest_url_remote_collectors -Headers $rest_authentication_session_xml
$collectors = $ResourcesXML.'collectors'.'collector'
Write-Host "Available Remote Collectors:" -ForegroundColor Blue
$collector_uuid_array=@()
foreach ($collector in $collectors) {
$collector_uuid_array += $collector.uuId
Write-Host "Remote Collector Name:" $collector.name " | Collector ID" $collector.uuId
}
}
Catch {
$_.Exception.ToString()
Write-Host "Error getting remote collectors. Exiting" -ForegroundColor Red
exit
}
#Get Notification Rules
#As with the other code snippets, you can use the API to obtain all the notification rules:
$rest_url_notification_rules = $rest_url_base+"notifications/rules"
Try {
$NotificationXML = Invoke-RestMethod -Method GET -Uri $rest_url_notification_rules -Headers $rest_authentication_session_xml
$notification_rules = $NotificationXML.'notification-rules'.'notification-rule'
Write-Host "Available Notification Rules:" -ForegroundColor Blue
foreach ($notification_rule in $notification_rules) {
Write-Host $notification_rule.name
}
}
Catch {
$_.Exception.ToString()
Write-Host "Error getting notification rules. Exiting" -ForegroundColor Red
exit
}
#Official Documentation
#Once you have tried the above examples, check out the official documentation by appending /suite-api/doc/swagger-ui.html to the end of your vROps install FQDN.
#This will open the vRealize Operations Manager API site and give you all the available API commands which you can substitute in the above code examples.