forked from dell/OpenManage-Enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-Network.ps1
More file actions
247 lines (209 loc) · 8.06 KB
/
Copy pathNew-Network.ps1
File metadata and controls
247 lines (209 loc) · 8.06 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
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
245
246
247
<#
_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 create a static group in OME
.DESCRIPTION
This script uses the OME REST API to create a new network
A network consists of a Minimum and Maximum VLAN ID to create a range
Set Minimum and Maximum to the same value to a single VLAN
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. Will be prompted if not supplied.
.PARAMETER ListNetworkTypes
List available Network Types
.PARAMETER ListNetworks
List existing Networks
.PARAMETER ExportExample
Creates a file called New-NetworkExample.csv in the current directory.
Use this file as an example to import.
.PARAMETER InFile
Path to CSV file in format.
*Must include header row with at least the rows in the example below
*Use -ExportExample to create an example CSV file for import
*NetworkType must be an integer value. Use --list-networktypes
*For a single VLAN set VlanMinimum=VlanMaximum
Example:
Name,Description,VlanMaximum,VlanMinimum,NetworkType
VLAN 800,Description for VLAN 800,800,800,1
.EXAMPLE
$cred = Get-Credential
.\New-Network.ps1 -IpAddress 100.79.6.11 -Credentials $cred -ListNetworkTypes
.EXAMPLE
.\New-Network.ps1 -IpAddress 100.79.6.11 -Credentials root -ListNetworkTypes
.EXAMPLE
.\New-Network.ps1 -IpAddress 100.79.6.11 -ListNetworks
.EXAMPLE
.\New-Network.ps1 -IpAddress 100.79.6.11 -ExportExample
.EXAMPLE
.\New-Network.ps1 -IpAddress 100.79.6.11 -InFile "New-NetworkExample.csv"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Net.IPAddress] $IpAddress,
[Parameter(Mandatory)]
[pscredential] $Credentials,
[Parameter(Mandatory = $false)]
[switch] $ExportExample,
[Parameter(Mandatory = $false)]
[switch] $ListNetworks,
[Parameter(Mandatory = $false)]
[switch] $ListNetworkTypes,
[Parameter(Mandatory = $false)]
[string] $InFile
)
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
}
function Get-Networks($BaseUri, $Headers) {
# Display Networks
$Type = "application/json"
$NetworkUrl = $BaseUri + "/api/NetworkConfigurationService/Networks"
$NetworkResp = Invoke-WebRequest -Uri $NetworkUrl -Method Get -Headers $Headers -ContentType $Type
if ($NetworkResp.StatusCode -eq 200) {
$NetworkRespData = $NetworkResp.Content | ConvertFrom-Json
$NetworkRespData = $NetworkRespData.'value'
$NetworkRespData | Select Id, Name, Description, VlanMinimum, VlanMaximum, CreatedBy | Format-Table | Out-String
}
}
function Get-NetworkTypes($BaseUri, $Headers) {
# Display Network Types
$Type = "application/json"
$NetworkTypeUrl = $BaseUri + "/api/NetworkConfigurationService/NetworkTypes"
$NetworkTypeResp = Invoke-WebRequest -Uri $NetworkTypeUrl -Method Get -Headers $Headers -ContentType $Type
if ($NetworkTypeResp.StatusCode -eq 200) {
$NetworkTypeRespData = $NetworkTypeResp.Content | ConvertFrom-Json
$NetworkTypeRespData = $NetworkTypeRespData.'value'
$NetworkTypeRespData | Select Id, Name | Format-Table | Out-String
}
}
function Export-ExampleCSV() {
$Example = [pscustomobject]@{
"Name" = "VLAN 800";
"Description" = "Description for VLAN 800";
"VlanMinimum" = 800;
"VlanMaximum" = 800;
"NetworkType" = 1;
}
$Example | Export-Csv -Path .\New-NetworkExample.csv -NoTypeInformation
Write-Host "Exported example data to $(Get-Location)\New-NetworkExample.csv"
}
Try {
Set-CertPolicy
$BaseUri = "https://$($IpAddress)"
$Type = "application/json"
$Headers = @{}
# Export example CSV file
if ($ExportExample) {
Export-ExampleCSV
exit
}
# Request authentication session token
$AuthToken = Get-Session $IpAddress $Credentials
if ($AuthToken) {
# Successfully created a session, extract token
$Headers."X-Auth-Token" = $AuthToken["token"]
if ($ListNetworks) {
Get-Networks $BaseUri $Headers
exit
}
if ($ListNetworkTypes) {
Get-NetworkTypes $BaseUri $Headers
exit
}
# Check if we want to import from CSV file
if ($InFile -ne "" -and (Test-Path $InFile)) {
Import-CSV $InFile | Foreach-Object {
$Payload = @{
"Name" = $_.Name;
"Description" = $_.Description;
"VlanMaximum" = $_.VlanMinimum -as [int];
"VlanMinimum" = $_.VlanMaximum -as [int];
"Type" = $_.NetworkType -as [int];
}
$PayloadJson = $Payload | ConvertTo-Json
Write-Host "Creating Network from data: $($PayloadJson)"
# Create Network
Try {
$CreateNetworkUrl = $BaseUri + "/api/NetworkConfigurationService/Networks"
$CreateResp = Invoke-WebRequest -Uri $CreateNetworkUrl -Me Post -H $Headers -Con $Type -Body $PayloadJson
if ($CreateResp.StatusCode -eq 201) {
Write-Host "New Network created $($_)"
}
else {
Write-Host $CreateResp.Content
}
}
catch {
Write-Host "Exception: $($_)"
}
}
}
}
else {
Write-Error "Unable to create a session with appliance $($IpAddress)"
}
}
catch {
Write-Error "Exception: $($_)"
}
finally {
if ($AuthToken) {
Remove-Session $IpAddress $Headers $AuthToken["id"]
}
}