· 5 years ago · Apr 28, 2020, 12:50 AM
1$path = "C:\Program Files\WindowsPowerShell\Modules\ITGlueAPI"
2if ([System.IO.Directory]::Exists($path) -eq $false) {
3 Add-Type -AssemblyName System.IO.Compression.FileSystem
4 function Expand-ItglueZip {
5 param([string]$zipfile, [string]$outpath)
6
7 [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
8 }
9 Expand-ItglueZip -zipfile "$PSScriptRoot\ITGlueAPI.zip" -outpath $path
10}
11else {
12 Write-host "Module Exist's Already"
13}
14#####################################################################
15$APIKEy = "$($Env:APIKey)"
16$APIEndpoint = "https://api.itglue.com"
17$FlexAssetName = "SAN Storage - auto"
18Import-module "C:\Program Files\WindowsPowerShell\Modules\ITGlueAPI\ITGlueAPI"
19start-sleep -s 1
20Add-ITGlueBaseURI -base_uri $APIEndpoint
21Add-ITGlueAPIKey $APIKEy
22#some layout options, change if you want colours to be different or do not like the whitespace.
23$TableHeader = "<table class=`"table table-bordered table-hover`" style=`"width:125%`">"
24$TableStyling = "<th>", "<th style=`"background-color:#0097DC`">"
25$Vendor = $Env:Vendor
26#####################################################################
27if ($Vendor -eq "ME4") {
28 function Get-ME4Login {
29 [CmdletBinding(DefaultParameterSetName = 'ME4Address')]
30 param (
31 [Parameter(Mandatory, Position = 0, ParameterSetName = 'ME4Address')]
32 [string]$ME4Address,
33 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 1)]
34 [string]$ME4User,
35 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 2)]
36 [string]$ME4pw
37 )
38 begin {
39 # Declaring used rest method
40 $Method = 'get'
41 # Declaring the login resource
42 $AuthResource = "api/login"
43
44 # Declaring the clearstring password
45 $ClearStringHash = $ME4User + "_" + $ME4pw
46
47 # Declaring the cert
48 add-type @"
49 using System.Net;
50 using System.Security.Cryptography.X509Certificates;
51 public class TrustAllCertsPolicy : ICertificatePolicy {
52 public bool CheckValidationResult(
53 ServicePoint srvPoint, X509Certificate certificate,
54 WebRequest request, int certificateProblem) {
55 return true;
56 }
57 }
58"@
59 $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
60 [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
61 [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
62 }
63 process {
64 $hasher = [System.Security.Cryptography.HashAlgorithm]::Create('sha256')
65 $hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($ClearStringHash))
66 $hashString = [System.BitConverter]::ToString($hash)
67 $HashedKey = $hashString.Replace('-', '').ToLower()
68 $LoginUrl = "https://$ME4Address/$AuthResource/$HashedKey"
69 $Login = Invoke-RestMethod -Uri $LoginUrl -Method $Method -ContentType XML
70 if ($Login.RESPONSE.OBJECT.PROPERTY.'#text'[0] -contains "success") {
71 $Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
72 $Headers.Add("sessionKey", $Login.RESPONSE.OBJECT.PROPERTY.'#text'[2])
73 $Headers.Add("datatype", "json")
74 } return $Headers
75 else {
76 Write-Host "Connection Failed" }
77 }
78 }
79
80
81 function Get-ME4Report {
82 [CmdletBinding(DefaultParameterSetName = 'ME4Address')]
83 param (
84 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0)]
85 [string]$ME4Address,
86 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 1)]
87 [OBJECT]$Headers,
88 [Parameter(Mandatory, ParameterSetName = 'SummaryType')]
89 [ValidateSet('controllers', 'controller-statistics', 'disk-groups', 'disk-group-statistics', 'disk-parameters', 'disks', 'ports', 'service-tag-info', 'configuration')]
90 [string]$SummaryType
91 )
92 begin {
93 # Declaring used rest method
94 $Method = 'get'
95
96 # Declaring the base resource
97 $BaseResource = 'api/show'
98
99 # Declaring the content type
100 $ContentType = 'application/json'
101 }
102 process {
103 $Resource = "https://$ME4Address/$BaseResource/$SummaryType"
104 # Query for address groups
105 $Result = (Invoke-RestMethod -Uri $Resource -Method $Method -ContentType $ContentType -Headers $Headers )
106 return $Result
107 }
108 }
109
110 $obj = Get-ME4Login -ME4Address $Env:IPAddress -ME4User $Env:User -ME4pw $Env:Password | Get-ME4Report -ME4Address $Env:IPAddress -SummaryType configuration
111
112 $controllers = $obj.controllers | Select-Object controller-id, ip-address, ip-gateway, redundancy-mode, position, part-number | ConvertTo-Html -Fragment | Out-String
113 $encloseures = $obj.enclosures | Select-Object enclosure-id, type, number-of-disks, number-of-power-supplies, status, health, health-reason | ConvertTo-Html -Fragment | Out-String
114 $iscsi1 = $obj.controllers.port.'iscsi-port' | Select-Object ip-address, gateway, netmask, mac-address, default-router | ConvertTo-Html -Fragment -PreContent "<h1> Networking </h1>" | Out-String
115 $iscsi = $obj.controllers.port | Select-Object controller, port, port-type, target-id, status, actual-speed, health | ConvertTo-Html -Fragment -PreContent "<h1> Controller Ports </h1>" -PostContent "<br> $iscsi1" | Out-String
116 $sPools = $obj.pools | Select-Object name, total-size, total-avail, overcommit, over-committed, volumes, health, health-reason | ConvertTo-Html -Fragment | Out-String
117 $dGroups = $obj.'disk-groups' | Select-Object name, size, freespace, pool, storage-tier, raidtype, diskcount, status, disk-description, health, health-reason | ConvertTo-Html -Fragment | Out-String
118 $vDisks = $obj.'virtual-disks' | Select-Object name, size, raidtype, storage-type, diskcount, sparecount, status, array-drive-type, health, health-reason | ConvertTo-Html -Fragment | Out-String
119 $pDrives = $obj.drives | Select-Object durable-id, slot, vendor, size, model, serial-number, revision, description, architecture, smart, status, health, ssd-life-left | sort-Object -Property architecture | ConvertTo-Html -Fragment | Out-String
120
121 $FlexAssetBody =
122 @{
123 type = 'flexible-assets'
124 attributes = @{
125 name = $FlexAssetName
126 traits = @{
127 "vendor" = $obj.controllers.vendor[0]
128 "model" = $obj.controllers.model[0]
129 "controllers" = ($TableHeader + $controllers) -replace $TableStyling
130 "enclosures" = ($TableHeader + $encloseures) -replace $TableStyling
131 "iscsi-target-ports" = ($TableHeader + $iscsi) -replace $TableStyling
132 "storage-pools" = ($TableHeader + $sPools) -replace $TableStyling
133 "disk-groups" = ($TableHeader + $dGroups) -replace $TableStyling
134 "virtual-disks" = ($TableHeader + $vDisks) -replace $TableStyling
135 "physical-drives" = ($TableHeader + $pDrives) -replace $TableStyling
136 }
137 }
138 }
139
140 $site = (Get-ITGlueConfigurations -filter_name $env:ComputerName)[0].data.attributes.'organization-id'
141 #Checking if the FlexibleAsset exists. If not, create a new one.
142 $FilterID = (Get-ITGlueFlexibleAssetTypes -filter_name $FlexAssetName).data
143 $ExistingFlexAsset = (Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $Filterid.id -filter_organization_id $site).data | Where-Object { $_.attributes.traits.vendor -eq $obj.controllers.vendor[0] }
144
145 #If the Asset does not exist, we edit the body to be in the form of a new asset, if not, we just upload.
146 if (!$ExistingFlexAsset) {
147 $FlexAssetBody.attributes.add('organization-id', $site)
148 $FlexAssetBody.attributes.add('flexible-asset-type-id', $FilterID.id)
149 Write-Host "Creating new flexible asset"
150 New-ITGlueFlexibleAssets -data $FlexAssetBody
151
152 }
153 else {
154 Write-Host "Updating Flexible Asset"
155 Set-ITGlueFlexibleAssets -id $ExistingFlexAsset.id -data $FlexAssetBody
156 }
157}
158
159if ($Vendor -eq "Nimble") {
160 function Get-NimbleLogin {
161 [CmdletBinding(DefaultParameterSetName = 'NimbleAddress')]
162 param (
163 [Parameter(Mandatory, Position = 0, ParameterSetName = 'NimbleAddress')]
164 [string]$NimbleAddress,
165 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 1)]
166 [string]$NimbleUser,
167 [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 2)]
168 [string]$Nimblepw
169 )
170 begin {
171 add-type @"
172 using System.Net;
173 using System.Security.Cryptography.X509Certificates;
174 public class TrustAllCertsPolicy : ICertificatePolicy {
175 public bool CheckValidationResult(
176 ServicePoint srvPoint, X509Certificate certificate,
177 WebRequest request, int certificateProblem) {
178 return true;
179 }
180 }
181"@
182 $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
183 [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
184 [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
185 }
186 process {
187 $data = @{
188 username = $NimbleUser
189 password = $Nimblepw
190 }
191
192 $body = ConvertTo-Json (@{ data = $data })
193
194 $uri = "https://" + $NimbleAddress + ":5392/v1/tokens"
195 $token = Invoke-RestMethod -Uri $uri -Method Post -Body $body
196 $token = $token.data.session_token
197 return $token
198 }
199 }
200
201 function Get-NimbleReport {
202 [CmdletBinding(DefaultParameterSetName = 'NimbleAddress')]
203 param (
204 [Parameter(Mandatory, Position = 0, ParameterSetName = 'NimbleAddress')]
205 [string]$NimbleAddress,
206 [Parameter(Mandatory, Position = 1)]
207 [string]$SummaryType
208 )
209 $token = Get-NimbleLogin -NimbleAddress $Env:IPAddress -NimbleUser $Env:User -Nimblepw $Env:Password
210 $header = @{ "X-Auth-Token" = $token }
211 $uri = "https://" + $NimbleAddress + ":5392/v1/$SummaryType"
212 $result = Invoke-RestMethod -Uri $uri -Method Get -Header $header
213 return $result
214 }
215
216 $model = Get-NimbleReport -NimbleAddress $Env:IPAddress -SummaryType "arrays/detail"
217 $controllers = Get-NimbleReport -NimbleAddress $Env:IPAddress -SummaryType "controllers/detail" | Select-Object -ExpandProperty data |
218 Select-object support_address, support_netmask, power_status, fan_status, temperature_status, ctrlr_side, state | ConvertTo-Html -Fragment | Out-String
219
220 $encloseures = Get-NimbleReport -NimbleAddress $Env:IpAddress -SummaryType "shelves/detail" | Select-Object -ExpandProperty data |
221 Select-Object chassis_type, model, model_ext, psu_overall_status, fan_overall_status | ConvertTo-Html -Fragment | Out-String
222
223 $iscsiInt = Get-NimbleReport -NimbleAddress $Env:IpAddress -SummaryType "network_interfaces/detail" | Select-Object -ExpandProperty data |
224 select-object name, port, controller_name, link_speed, link_status, mtu, nic_type | Where-Object { $_.mtu -eq "9000" } | ConvertTo-Html -Fragment -PreContent "<h2> Controller Ports </h2> <br>" | Out-String
225
226 $iscsi = Get-NimbleReport -NimbleAddress $Env:IpAddress -SummaryType "initiators/detail" | Select-Object -ExpandProperty data |
227 Select-Object access_protocol, initiator_group_name, iqn, label | ConvertTo-Html -Fragment -PreContent "<h2> Networking </h2>" -PostContent "$iscsiInt" | out-string
228
229 $dGroups = Get-NimbleReport -NimbleAddress $Env:IpAddress -SummaryType "volumes/detail" | select-object -expandproperty data |
230 select-object name, online, app_category, perfpolicy_name, target_name, space_usage_level, size, total_usage_bytes | Foreach-object -process {
231 $_.size = ("{0:N0}" -f ($_.size / 1024) + "GB" ); $_
232 } | Foreach-object -process {
233 $_.total_usage_bytes = ("{0:N0}" -f ($_.total_usage_bytes / 1GB) + "GB" ); $_
234 } | convertto-html -fragment | out-string
235
236 $pdrives = Get-NimbleReport -NimbleAddress $Env:IpAddress -SummaryType "disks/detail" | select-object -expandproperty data | Select-object slot, model, serial, firmware_version, state, raid_state |
237 convertto-html -Fragment | Out-String
238
239 $FlexAssetBody =
240 @{
241 type = 'flexible-assets'
242 attributes = @{
243 name = $FlexAssetName
244 traits = @{
245 "vendor" = "Nimble"
246 "model" = $model.data.extended_model
247 "controllers" = ($TableHeader + $controllers) -replace $TableStyling
248 "enclosures" = ($TableHeader + $encloseures) -replace $TableStyling
249 "iscsi-target-ports" = ($TableHeader + $iscsi) -replace $TableStyling
250 "disk-groups" = ($TableHeader + $dGroups) -replace $TableStyling
251 "physical-drives" = ($TableHeader + $pDrives) -replace $TableStyling
252 }
253 }
254 }
255
256 $site = (Get-ITGlueConfigurations -filter_name $env:ComputerName).data.attributes.'organization-id'
257 #Checking if the FlexibleAsset exists. If not, create a new one.
258 $FilterID = (Get-ITGlueFlexibleAssetTypes -filter_name $FlexAssetName).data
259 $ExistingFlexAsset = (Get-ITGlueFlexibleAssets -filter_flexible_asset_type_id $Filterid.id -filter_organization_id $site).data | Where-Object { $_.attributes.traits.vendor -eq "Nimble" }
260
261 #If the Asset does not exist, we edit the body to be in the form of a new asset, if not, we just upload.
262 if (!$ExistingFlexAsset) {
263 $FlexAssetBody.attributes.add('organization-id', $site)
264 $FlexAssetBody.attributes.add('flexible-asset-type-id', $FilterID.id)
265 Write-Host "Creating new flexible asset"
266 New-ITGlueFlexibleAssets -data $FlexAssetBody
267
268 }
269 else {
270 Write-Host "Updating Flexible Asset"
271 Set-ITGlueFlexibleAssets -id $ExistingFlexAsset.id -data $FlexAssetBody
272 }
273}