-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_power.ps1
executable file
·130 lines (123 loc) · 5.52 KB
/
check_power.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
#"""
# IMM Domain Power Check Script.
# * Tyson Scott 6/30/2023 - tyscott@cisco.com
#"""
#=============================================================================
# YAML File is a Required Parameter
# Pull in YAML Content
#=============================================================================
param (
[string]$y,
[switch]$force
)
if (-not ($y)) {
throw "Missing parameter -y {yaml_file}"
}
if (-not (Test-Path $y)) {
throw "File $y doesn't exist."
}
Import-Module -Name Cisco.IMC
Import-Module -Name Cisco.UCSManager
Import-module -Name powershell-yaml
$yamlData = Get-Content -Path $y -Raw | ConvertFrom-Yaml
#$folder_path = Split-Path $y
$MenuList = @()
foreach ($item in $yamlData.cabinets) {
foreach ($i in $item.GetEnumerator()) {
$MenuList += $i.key
}
}
Do {
foreach ($MenuItem in $MenuList) {
'{0} - {1}' -f ($MenuList.IndexOf($MenuItem) + 1), $MenuItem
}
$Choice = ''
while ([string]::IsNullOrEmpty($Choice)) {
Write-Host
$Choice = Read-Host 'Please Select a Cabinet to check '
if ($Choice -notin 1..$MenuList.Count) {
[console]::Beep(1000, 300)
Write-Warning ''
Write-Warning (' Your choice [ {0} ] is not valid.' -f $Choice)
Write-Warning (' The valid choices are 1 thru {0}.' -f $MenuList.Count)
Write-Warning ' Please try again ...'
pause
$Choice = ''
}
}
''
'You chose {0}' -f $MenuList[$Choice - 1]
foreach ($domain in $yamlData.cabinets.$($MenuList[$Choice - 1]).domains) {
Write-Host ""
Write-Host "Starting Login for $($domain.name)"
if ($domain.type -eq "IMM") {
If (Test-Path -Path $env:HOME\powercliIMM.Cred) {
$credential = Import-CliXml -Path "$env:HOME\powercliIMM.Cred"
} Else {
$credential = Get-Credential
$credential | Export-CliXml -Path "$env:HOME\powercliIMM.Cred"
}
$HostName ="$($domain.name)"
$Password = ConvertFrom-SecureString -AsPlainText $credential.Password
$JSON = @{User=$credential.UserName; Password=$Password}
$null = Invoke-WebRequest -Uri "https://$HostName/Login" -Method POST -Body ($JSON | ConvertTo-Json) -SkipCertificateCheck -UseBasicParsing -SessionVariable cookie
foreach($chassis in $domain.chassis) {
Write-Host ""
Write-Host "$($domain.name) Chassis $chassis Power Supplies."
$io_card = 'IoCard-{0}-1' -f $chassis
$headers = @{'inventory-type'='Chassis'; 'inventory-id'=$io_card}
$power = Invoke-WebRequest -Uri "https://$HostName/api-explorer/resources/redfish/v1/Chassis/$chassis/Power" -Headers $headers -WebSession $cookie -SkipCertificateCheck
$power = $power.Content | ConvertFrom-Json
$power.PowerSupplies | Add-Member -NotePropertyName Domain -NotePropertyValue $domain.name
$power.PowerSupplies | Add-Member -NotePropertyName Chassis -NotePropertyValue $chassis
$power.PowerSupplies | Format-Table Domain,Chassis,MemberId,Model,SerialNumber,Status
}
} elseif ($domain.type -eq "UCSM") {
Write-Host ""
Write-Host "Starting Login for $($domain.name)"
If (Test-Path -Path $env:HOME\powercliUCSM.Cred) {
$credential = Import-CliXml -Path "$env:HOME\powercliUCSM.Cred"
} Else {
$credential = Get-Credential
$credential | Export-CliXml -Path "$env:HOME\powercliUCSM.Cred"
}
$null = Connect-Ucs -Name $domain.name -Credential $credential
foreach($chassis in $domain.chassis) {
Write-Host ""
Write-Host "$($domain.name) Chassis $chassis Power Supplies."
$regex = 'chassis-{0}' -f $chassis
Get-UcsPsu | Where-Object { $_.Dn -match $regex } | Format-Table Ucs,Dn,Model,Serial,OperState
}
if ($domain.rackmounts) {
if ($domain.rackmounts -eq $true) {
Write-Host ""
Write-Host "$($domain.name) Rackmount Power Supplies."
$regex = 'sys/rack-unit'
Get-UcsPsu | Where-Object { $_.Dn -match $regex } | Format-Table Ucs,Dn,Model,Serial,OperState
}
}
Write-Host ""
Write-Host "$($domain.name) Power Supplies."
Get-UcsPsu | Where-Object { $_.Dn -match 'sys/switch' } | Format-Table Ucs,Dn,Model,Serial,OperState
$null = Disconnect-Ucs
}
}
if ($yamlData.cabinets.$($MenuList[$Choice - 1]).rackmounts) {
foreach ($rackmount in $yamlData.cabinets.$($MenuList[$Choice - 1]).rackmounts) {
Write-Host ""
Write-Host "Starting Login for $($rackmount.name)"
If (Test-Path -Path $env:HOME\powercliIMC.Cred) {
$credential = Import-CliXml -Path "$env:HOME\powercliIMC.Cred"
} Else {
$credential = Get-Credential
$credential | Export-CliXml -Path "$env:HOME\powercliIMC.Cred"
}
$null = Connect-IMC -Name $rackmount.name -Credential $credential
Write-Host ""
Get-ImcPsu | Format-Table Imc,Dn,Pid,Serial,Operability
$null = Disconnect-IMC
}
}
$Answer = Read-Host "Press 1 to rerun the script and choose another cabinet Any other Key to exit."
}
Until ($answer -ne "1")