-
Notifications
You must be signed in to change notification settings - Fork 49
/
Get-IdentityPoolUsage.ps1
244 lines (214 loc) · 10.1 KB
/
Get-IdentityPoolUsage.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<#
_author_ = Trevor Squillario <Trevor.Squillario@Dell.com>
Copyright (c) 2022 Dell EMC Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
<#
.SYNOPSIS
Script to get the list of virtual addresses in an Identity Pool
.DESCRIPTION
This script uses the OME REST API to get a list of virtual addresses in an Identity Pool.
Will export to a CSV file called Get-IdentityPoolUsage.csv in the current directory
For authentication X-Auth is used over Basic Authentication
Note that the credentials entered are not stored to disk.
.PARAMETER IpAddress
This is the IP address of the OME Appliance
.PARAMETER Credentials
Credentials used to talk to the OME Appliance
.PARAMETER Id
This is the Identity Pool Id
.PARAMETER OutFile
This is the full path to output the CSV file
.EXAMPLE
$cred = Get-Credential
.\Get-IdentityPoolUsage.ps1 -IpAddress "10.xx.xx.xx" -Credentials $cred
.EXAMPLE
.\Get-IdentityPoolUsage.ps1 -IpAddress "10.xx.xx.xx"
In this instance you will be prompted for credentials to use
.EXAMPLE
.\Get-IdentityPoolUsage.ps1 -IpAddress "10.xx.xx.xx" -Id 3
In this instance you will be prompted for credentials to use
.EXAMPLE
.\Get-IdentityPoolUsage.ps1 -IpAddress "10.xx.xx.xx" -Id 3 -OutFile C:\Temp\export.csv
In this instance you will be prompted for credentials to use
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Net.IPAddress] $IpAddress,
[Parameter(Mandatory)]
[pscredential] $Credentials,
[Parameter(Mandatory = $false)]
[String] $Id,
[Parameter(Mandatory = $false)]
[String] $OutFile
)
function Set-CertPolicy() {
## Trust all certs - for sample usage only
Try {
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
}
catch {
Write-Error "Unable to add type for cert policy"
}
}
$SessionAuthToken = @{}
function Get-Session($IpAddress, $Credentials) {
$SessionUrl = "https://$($IpAddress)/api/SessionService/Sessions"
$Type = "application/json"
$UserName = $Credentials.username
$Password = $Credentials.GetNetworkCredential().password
$UserDetails = @{"UserName" = $UserName; "Password" = $Password; "SessionType" = "API" } | ConvertTo-Json
$SessResponse = Invoke-WebRequest -Uri $SessionUrl -Method Post -Body $UserDetails -ContentType $Type
if ($SessResponse.StatusCode -eq 200 -or $SessResponse.StatusCode -eq 201) {
$SessResponseData = $SessResponse.Content | ConvertFrom-Json
$SessionAuthToken = @{
"token" = $SessResponse.Headers["X-Auth-Token"];
"id" = $SessResponseData.Id
}
}
return $SessionAuthToken
}
function Remove-Session($IpAddress, $Headers, $Id) {
$SessionUrl = "https://$($IpAddress)/api/SessionService/Sessions('$($Id)')"
$Type = "application/json"
$SessResponse = Invoke-WebRequest -Uri $SessionUrl -Method Delete -Headers $Headers -ContentType $Type
}
Try {
Set-CertPolicy
$BaseUri = "https://$($IpAddress)"
$NextLinkUrl = $null
$Type = "application/json"
$Headers = @{}
# Request authentication session token
$AuthToken = Get-Session $IpAddress $Credentials
if ($AuthToken) {
# Successfully created a session, extract token
$Headers."X-Auth-Token" = $AuthToken["token"]
# Display Identity Pools
$IdentityPoolUrl = $BaseUri + "/api/IdentityPoolService/IdentityPools"
$IdentityPoolResp = Invoke-WebRequest -Uri $IdentityPoolUrl -Method Get -Headers $Headers -ContentType $Type
if ($IdentityPoolResp.StatusCode -eq 200) {
$IdentityPoolRespData = $IdentityPoolResp.Content | ConvertFrom-Json
$IdentityPoolRespData = $IdentityPoolRespData.'value'
if ($Id -eq "") {
$IdentityPoolRespData | Select Id, Name | Out-String
$Id = Read-Host "Please Enter Identity Pool Id"
}
}
# Get Identity Pool Usage Sets
$IdentityPoolUsageSetUrl = $BaseUri + "/api/IdentityPoolService/IdentityPools($($Id))/UsageIdentitySets"
$IdentityPoolUsageSetResp = Invoke-WebRequest -Uri $IdentityPoolUsageSetUrl -Method Get -Headers $Headers -ContentType $Type
if ($IdentityPoolUsageSetResp.StatusCode -eq 200) {
$IdentityPoolUsageSetRespData = $IdentityPoolUsageSetResp.Content | ConvertFrom-Json
$IdentityPoolUsageSetRespData = $IdentityPoolUsageSetRespData.'value'
$DeviceData = @()
# Loop through Usage Sets using Id to get Details
foreach ($IdentitySet in $IdentityPoolUsageSetRespData) {
$IdentitySetId = $IdentitySet.IdentitySetId
$IdentitySetName = $IdentitySet.Name
$IdentityPoolUsageDetailUrl = $BaseUri + "/api/IdentityPoolService/IdentityPools($($Id))/UsageIdentitySets($($IdentitySetId))/Details"
$IdentityPoolUsageDetailResp = Invoke-WebRequest -Uri $IdentityPoolUsageDetailUrl -Method Get -Headers $Headers -ContentType $Type
if ($IdentityPoolUsageDetailResp.StatusCode -eq 200) {
$IdentityPoolUsageDetailData = $IdentityPoolUsageDetailResp.Content | ConvertFrom-Json
# Loop through Details appending to object array
foreach ($DeviceEntry in $IdentityPoolUsageDetailData.'value') {
$DeviceDetails = @{
IdentityType = $IdentitySetName
ChassisName = $DeviceEntry.DeviceInfo.ChassisName
ServerName = $DeviceEntry.DeviceInfo.ServerName
ManagementIp = $DeviceEntry.DeviceInfo.ManagementIp
NicIdentifier = $DeviceEntry.NicIdentifier
MacAddress = $DeviceEntry.MacAddress
}
$DeviceData += New-Object PSObject -Property $DeviceDetails
}
# Check if there are multiple pages in response
if ($IdentityPoolUsageDetailData.'@odata.nextLink') {
$NextLinkUrl = $BaseUri + $IdentityPoolUsageDetailData.'@odata.nextLink'
}
# Loop through pages until end
while ($NextLinkUrl) {
$IdentityPoolUsageDetailNextLinkResp = Invoke-WebRequest -Uri $NextLinkUrl -Method Get -Headers $Headers -ContentType $Type
if ($IdentityPoolUsageDetailNextLinkResp.StatusCode -eq 200) {
$IdentityPoolUsageDetailNextLinkData = $IdentityPoolUsageDetailNextLinkResp.Content | ConvertFrom-Json
# Loop through Details appending to object array
foreach ($DeviceEntry in $IdentityPoolUsageDetailNextLinkData.'value') {
$DeviceDetails = @{
IdentityType = $IdentitySetName
ChassisName = $DeviceEntry.DeviceInfo.ChassisName
ServerName = $DeviceEntry.DeviceInfo.ServerName
ManagementIp = $DeviceEntry.DeviceInfo.ManagementIp
NicIdentifier = $DeviceEntry.NicIdentifier
MacAddress = $DeviceEntry.MacAddress
}
$DeviceData += New-Object PSObject -Property $DeviceDetails
}
# Set for nextLink for next iteration
if ($IdentityPoolUsageDetailNextLinkData.'@odata.nextLink') {
$NextLinkUrl = $BaseUri + $IdentityPoolUsageDetailNextLinkData.'@odata.nextLink'
}
else {
$NextLinkUrl = $null
}
}
else {
$NextLinkUrl = $null
Write-Error "Unable to retrieve items from nextLink... Exiting"
}
}
}
else {
Write-Error "Unable to get identity pools... Exiting"
}
}
# Print table to console
$DeviceData | Format-Table | Out-String
# Export to CSV
if ($DeviceData.Count -gt 0) {
if ($OutFile -eq "") {
$DeviceData | Export-Csv -Path "Get-IdentityPoolUsage.csv" -NoTypeInformation
Write-Host "Exported data to $(Get-Location)\Get-IdentityPoolUsage.csv"
}
else {
$DeviceData | Export-Csv -Path $OutFile -NoTypeInformation
Write-Host "Exported data to $($OutFile)"
}
}
else {
Write-Host "No data to display"
}
}
}
else {
Write-Error "Unable to create a session with appliance $($IpAddress)"
}
}
catch {
Write-Error "Exception: $($_)"
}
finally {
if ($AuthToken) {
Remove-Session $IpAddress $Headers $AuthToken["id"]
}
}