-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCreate_user_on_all_ilos.ps1
214 lines (158 loc) · 8.33 KB
/
Create_user_on_all_ilos.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
# -------------------------------------------------------------------------------------------------------
# by [email protected]
# July 2015
#
# Create a User account in all iLOs managed by OneView without using the iLO Administrator local account
#
# OneView administrator account is required.
# iLO modification is done through OneView and iLO SSOsession key using REST POST method
# --------------------------------------------------------------------------------------------------------
#################################################################################
# (C) Copyright 2017 Hewlett Packard Enterprise Development LP #
#################################################################################
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #
# THE SOFTWARE. #
# #
#################################################################################
param([string]$DefaultIP = "myip", [string]$username = "user", [string]$defaultpassword = "pass", [string]$Defaultuser = "newuser", [string]$Defaultuserpassword = "newuserpass")
Clear
Write-Host "Arg: $DefaultIP"
Write-Host "Arg: $username"
Write-Host "Arg: $defaultpassword"
Write-Host "Arg: $Defaultuser"
Write-Host "Arg: $Defaultuserpassword"
#IP address of OneView
#$DefaultIP = "10.0.5.2"
#Clear
write-host ""
write-host "This Script can be used to set up an ILO account on ALL blade ILOs - Should be used for 1st deployment only"
write-host ""
#$IP = Read-Host "Please enter the IP address of your OneView appliance [$($DefaultIP)]"
$IP = ($DefaultIP,$IP)[[bool]$IP]
# OneView Credentials
#$username = "Administrator"
#$defaultpassword = "Start123"
#$password = Read-Host "Please enter the Administrator password for OneView [$($Defaultpassword)]"
$password = ($Defaultpassword,$password)[[bool]$password]
# Import the OneView 3.10 library
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
if (-not (get-module HPOneview.310))
{
Import-module HPOneview.310
}
$PWord = ConvertTo-SecureString –String $password –AsPlainText -Force
$cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $Username, $PWord
# Connection to the Synergy Composer
if ((test-path Variable:ConnectedSessions) -and ($ConnectedSessions.Count -gt 1)) {
Write-Host -ForegroundColor red "Disconnect all existing HPOV / Composer sessions and before running script"
exit 1
}
elseif ((test-path Variable:ConnectedSessions) -and ($ConnectedSessions.Count -eq 1) -and ($ConnectedSessions[0].Default) -and ($ConnectedSessions[0].Name -eq $IP)) {
Write-Host -ForegroundColor gray "Reusing Existing Composer session"
}
else {
#Make a clean connection
Disconnect-HPOVMgmt -ErrorAction SilentlyContinue
$Appplianceconnection = Connect-HPOVMgmt -appliance $IP -PSCredential $cred
}
import-HPOVSSLCertificate
# Creation of the header
$postParams = @{userName=$username;password=$password} | ConvertTo-Json
$headers = @{}
#$headers["Accept"] = "application/json"
$headers["X-API-Version"] = "300"
# Capturing the OneView Session ID and adding it to the header
$key = $ConnectedSessions[0].SessionID
$headers["auth"] = $key
# Capture iLO IP adresses managed by OneView
$iloIPs = Get-HPOVServer | where mpModel -eq iLO4 | % {$_.mpHostInfo.mpIpAddresses[1].address }
# Capture and display iLO IP adresses not supporting REST
$unsupportediLO = Get-HPOVServer | where mpModel -ne iLO4 | % {$_.mpHostInfo.mpIpAddresses[1].address }
$iloIPs
pause
clear
if ($unsupportediLO) {
write-host ""
Write-warning "The following iLO(s) do not support REST API (only iLO 4 are supported) :"
$unsupportediLO}
# Capture iLO User/password to create
#$Defaultuser = "jpswth"
#$Defaultuserpassword = "Start123"
write-host ""
#$user = Read-Host "Please enter the user you want to add to all iLos [$($Defaultuser)]"
$user = ($Defaultuser,$user)[[bool]$user]
write-host ""
#$userpassword = Read-Host "Please enter the password for $user MUST BE 8 or Charactors [$($Defaultuserpassword)]"
$userpassword = ($Defaultuserpassword,$userpassword)[[bool]$userpassword]
#Creation of the body content to pass to iLO
$bodyiloParams = '{"UserName": "'+ $user + '", "Password": "' + $userpassword + '", "Oem": {"Hp": {"Privileges": {"RemoteConsolePriv": true, "VirtualMediaPriv": true, "UserConfigPriv": true, "iLOConfigPriv": true, "VirtualPowerAndResetPriv": true}, "LoginName": "'+ $user + '"}}}'
Write-Host $bodyiloParams
# Added these lines to avoid the error: "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
# due to an invalid Remote Certificate
add-type -TypeDefinition @"
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
Foreach($iloIP in $iloIPs)
{
# Capture of the SSO Session Key
$ilosessionkey = (Get-HPOVServer | where {$_.mpHostInfo.mpIpAddresses[1].address -eq $iloIP} | Get-HPOVIloSso -IloRestSession)."X-Auth-Token"
# Creation of the header using the SSO Session Key
$headerilo = @{}
$headerilo["Accept"] = "application/json"
$headerilo["X-API-Version"] = "3"
$headerilo["X-Auth-Token"] = $ilosessionkey
#Creation of the user account using the hearder and body created previously
Try {
$error.clear()
$testuser = Invoke-WebRequest -Uri "https://$iloIP/rest/v1/AccountService/Accounts" -ContentType "application/json" -Headers $headerilo -Method GET -UseBasicParsing
if ($Error[0] -eq $Null)
{
# User account created if not present
if ($testuser.Content.Contains($user) -ne $True)
{
write-host ""
write-host "User [$user] does not exist in iLO : $iloIP and will be created !"
$rest = Invoke-WebRequest -Uri "https://$iloIP/rest/v1/AccountService/Accounts" -Body $bodyiloParams -ContentType "application/json" -Headers $headerilo -Method POST -UseBasicParsing
write-host ""
Write-Host "[$user] has been created in iLo: $iloIP"
}
# User account not created if already present
else
{
write-host ""
write-warning "User [$user] already exists in iLO : $iloIP !"
}
}
}
#Error is returned if iLO FW is not supported
catch [System.Net.WebException] {
write-host ""
Write-Warning "The firmware of iLO: $iloIP might be too old or the password entered is too short ! [$User] has not been added !" }
}
write-host ""
Read-Host -Prompt "Operation done ! Hit return to close"