forked from dell/OpenManage-Enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-RefreshPowerManagerInventory.ps1
More file actions
183 lines (163 loc) · 7.68 KB
/
Copy pathInvoke-RefreshPowerManagerInventory.ps1
File metadata and controls
183 lines (163 loc) · 7.68 KB
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
<#
_author_ = Ashish Singh <ashish_singh11@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 run default inventory task to determine power management capabilities of devices post OMEnt-Power Manager Installation
.DESCRIPTION
This script fetches Job id of Default Inventory Task and rns the job, post which it iterates till job is completed.
.PARAMETER IpAddress
This is the IP address of the OME Appliance
.PARAMETER Credentials
Credentials used to talk to the OME Appliance
.EXAMPLE
$creds = Get-Credentials
.\Invoke-RefreshPowerManagerInventory.ps1 -IpAddress 192.168.1.93 -Credentials $creds
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Net.IPAddress] $IpAddress,
[Parameter(Mandatory)]
[pscredential] $Credentials
)
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"
}
}
function Get-JobId($Headers) {
$Jobname = 'Default Inventory Task'
$JobId = -1
$JobUrl = "https://$($IpAddress)/api/JobService/Jobs"
$JobResponse = Invoke-WebRequest -Uri $JobUrl -Headers $Headers -Method Get
if ($JobResponse.StatusCode -eq 200) {
Write-Host "Job fetched"
$JobInfo = $JobResponse.Content | ConvertFrom-Json
<# Write-Host "$($JobInfo)"#>
$JobList = $JobInfo.value
$totalJobs = $JobInfo.'@odata.count'
if ($totalJobs -gt 0) {
if ($JobInfo.'@odata.nextLink') {
$NextLinkUrl = $BaseUri + $JobInfo.'@odata.nextLink'
}
while ($NextLinkUrl) {
$NextLinkResponse = Invoke-WebRequest -Uri $NextLinkUrl -Method Get -Headers $Headers -ContentType $Type
if ($NextLinkResponse.StatusCode -eq 200) {
$NextLinkData = $NextLinkResponse.Content | ConvertFrom-Json
$JobList += $NextLinkData.'value'
if ($NextLinkData.'@odata.nextLink') {
$NextLinkUrl = $BaseUri + $NextLinkData.'@odata.nextLink'
}
else {
$NextLinkUrl = $null
}
}
else {
Write-Warning "Unable to get nextlink response for $($NextLinkUrl)"
}
}
}
else {
Write-Warning "Job results are empty"
}
foreach ($jobinfo in $JobList) {
if ($jobinfo.'JobName' -match $JobName) {
$Jobid = $jobinfo.Id
<#Write-Host "$($jobinfo.Id)"#>
$job_match_found = 1
}
}
}
if (!$job_match_found) {
Write-Host "Unable to track running discovery config job"
}
return $JobId
}
function Get-JobStatus($JobId) {
$FailedJobStatuses = @('Failed', 'Warning', 'Aborted', 'Paused', 'Stopped', 'Canceled')
$SLEEP_INTERVAL = 30
$JobSvcUrl = "https://$($IpAddress)/api/JobService/Jobs($($JobId))"
$Type = "application/json"
$Headers = @{ }
$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) {
$Headers."X-Auth-Token" = $SessResponse.Headers["X-Auth-Token"]
do {
$Ctr++
$JobResp = Invoke-WebRequest -Uri $JobSvcUrl -Headers $Headers -ContentType $Type -Method Get
$jobresp.Content
if ($JobResp.StatusCode -eq 200) {
$JobData = $JobResp.Content | ConvertFrom-Json
$JobStatus = $JobData.LastRunStatus.Name
Write-Host "Iteration $($Ctr): Status of Default Inventory Task $($JobId) is $($JobStatus)"
}
Start-Sleep -Seconds $SLEEP_INTERVAL
} until ($JobStatus -ne 'Running')
return $JobStatus
}
}
Try {
Write-Host 'The Power Manager scripts were originally internal Dell scripts we then published externally. If you see this message and are using one of these scripts it would be very helpful if you open an issue on GitHub at https://github.com/dell/OpenManage-Enterprise/issues and tell us you are using the script. We have not dedicated any resources to optimizing them but are happy to do so if we know the community is using them. Likewise if you find a bug in one of these scripts feel free to open an issue and we will investigate.'
Set-CertPolicy
$SessionUrl = "https://$($IpAddress)/api/SessionService/Sessions"
$RunJobUrl = "https://$($IpAddress)/api/JobService/Actions/JobService.RunJobs"
$Type = "application/json"
$Headers = @{ }
$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) {
$Headers."X-Auth-Token" = $SessResponse.Headers["X-Auth-Token"]
$InventoryJobId = Get-JobId($Headers)
$jpl = '[' + $InventoryJobId + ']'
$JobCreationpayload = @{ "JobIds" = $($jpl) } | ConvertTo-Json
$JobCreationpayload = $JobCreationpayload.Replace("`"[", "[").Replace("]`"", "]")
$RunInventoryResponse = Invoke-WebRequest -Uri $RunJobUrl -Method Post -Body $JobCreationpayload -Headers $Headers -ContentType $Type
if ( $RunInventoryResponse.StatusCode -eq 204) {
Write-Host "Performing Inventory ..."
<#Start-Sleep Seconds 10#>
$JobStatus = Get-JobStatus($InventoryJobId)
}
else {
Write-Host "Inventory Job creation Failed..."
}
}
else {
Write-Host "Session creation Failed...."
}
Write-Host 'The Power Manager scripts were originally internal Dell scripts we then published externally. If you see this message and are using one of these scripts it would be very helpful if you open an issue on GitHub at https://github.com/dell/OpenManage-Enterprise/issues and tell us you are using the script. We have not dedicated any resources to optimizing them but are happy to do so if we know the community is using them. Likewise if you find a bug in one of these scripts feel free to open an issue and we will investigate.'
}
catch {
Write-Error "Exception occured - $($_.Exception.Message)"
}