-
Notifications
You must be signed in to change notification settings - Fork 2
/
UCS Inventory Based User Labels for Servers PowerShell Script
384 lines (345 loc) · 11.5 KB
/
UCS Inventory Based User Labels for Servers PowerShell Script
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
https://communities.cisco.com/docs/DOC-37758
This PowerShell/PowerTool script allows you to log into a single or multiple UCS Domains and will create User Labels on all rack and blade servers in those systems with a mini-inventory label. It will consist of the UCS rack or blade friendly name, the quantity and model of processors as friendly names and the quantity of RAM. The maximum length of a User Label is 32 characters so this was about all I could fit.
Here is an example of a generated User Label:
B200-M3 / 2 x E5-2660 / 256GB
The script is based on the post by Doron Chosnek. Thank you!
Change Log:
v0.2 - Initially posted version
v0.2.01 - Added built in help. Command line options. Standard naming convention.
v0.2.03 - Added support for using a saved credentials file
As always, let me know if you have any questions, comments or suggestions!
Joe
<#
.SYNOPSIS
This script will go through each of your blades and rack servers and provide a user label with a basic inventory description.
.DESCRIPTION
This script will go through each of your blades and rack servers and provide a user label with a basic inventory description. Ex. - B200-M3 / 2 x E5-2660 / 256GB
.EXAMPLE
Set-UcsServerInventoryLabels.ps1
This script can be run without any command line parameters. User will be prompted for all parameters and options required
.EXAMPLE
Set-UcsServerInventoryLabels.ps1 -ucs "1.2.3.4" -ucred
-ucs -- UCS Manager IP or Host Name -- Example: "1.2.3.4" or "myucs" or "myucs.domain.local" or "1.2.3.4,5.6.7.8" or "myucs1,myucs2" or "myucs1.domain.local,myucs2.domain.local"
-ucred -- UCS Manager Credential Switch -- Adding this switch will immediately prompt you for your UCSM username and password
All parameters are optional and any skipped will be prompted for during execution
The only prompts that will always be presented to the user will be for User Names and Passwords
.EXAMPLE
Set-UcsServerInventoryLabels.ps1 -ucs "1.2.3.4" -saved "myucscred.csv" -skiperrors
-ucs -- UCS Manager IP or Host Name -- Example: "1.2.3.4" or "myucs" or "myucs.domain.local" or "1.2.3.4,5.6.7.8" or "myucs1,myucs2" or "myucs1.domain.local,myucs2.domain.local"
-savedcred -- UCSM credentials file -- Example: -savedcred "myucscred.csv"
To create a credentials file: $credential = Get-Credential ; $credential | select username,@{Name="EncryptedPassword";Expression={ConvertFrom-SecureString $_.password}} |Export-CSV -NoTypeInformation .\myucscred.csv
Make sure the password file is located in the same folder as the script
-skiperrors -- Tells the script to skip any prompts for errors and continues with 'y'
All parameters are optional and any skipped will be prompted for during execution
The only prompts that will always be presented to the user will be for User Names and Passwords
.NOTES
Author: Joe Martin
Email: [email protected]
Company: Cisco Systems, Inc.
Version: v0.2.03
Date: 7/21/2014
Disclaimer: Code provided as-is. No warranty implied or included. This code is for example use only and not for production
.INPUTS
UCSM IP Address(s) or Hostname(s)
UCSM Username and Password
UCSM Credentials File
.OUTPUTS
None
.LINK
http://communities.cisco.com/people/joemar/content
#>
#Command Line Parameters
param(
[string]$UCSM, # IP Address(s) or Hostname(s). If multiple entries, separate by commas
[switch]$UCREDENTIALS, # UCSM Credentials (Username and Password)
[string]$SAVEDCRED, # Saved UCSM Credentials. To create do: $credential = Get-Credential ; $credential | select username,@{Name="EncryptedPassword";Expression={ConvertFrom-SecureString $_.password}} | Export-CSV -NoTypeInformation .\myucscred.csv
[switch]$SKIPERROR # Skip any prompts for errors and continues with 'y'
)
#Clear the screen
clear-host
#Script kicking off
Write-Output "Script Running..."
Write-Output ""
#Gather any credentials requested from command line
if ($UCREDENTIALS)
{
$cred = Get-Credential -Message "Enter UCSM Credentials"
}
#Change directory to the script root
cd $PSScriptRoot
#Check to see if credential files exists
if ($SAVEDCRED)
{
if ((Test-Path $SAVEDCRED) -eq $false)
{
Write-Output ""
Write-Output "Your credentials file $SAVEDCRED does not exist in the script directory"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
}
#Do not show errors in script
$ErrorActionPreference = "SilentlyContinue"
#$ErrorActionPreference = "Stop"
#$ErrorActionPreference = "Continue"
#$ErrorActionPreference = "Inquire"
#Verify PowerShell Version for script support
$PSVersion = $psversiontable.psversion
$PSMinimum = $PSVersion.Major
if ($PSMinimum -ge "3")
{
}
else
{
Write-Output "This script requires PowerShell version 3 or above"
Write-Output "Please update your system and try again."
Write-Output "You can download PowerShell updates here:"
Write-Output " http://search.microsoft.com/en-us/DownloadResults.aspx?rf=sp&q=powershell+4.0+download"
Write-Output "If you are running a version of Windows before 7 or Server 2008R2 you need to update to be supported"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
#Tell the user what the script does
Write-Output "This script will go through each of your blades and rack servers and provide a"
Write-Output "user label with a basic inventory description."
Write-Output " Example:"
Write-Output " B200-M3 / 2 x E5-2660 / 256GB"
Write-Output ""
#Load the UCS PowerTool
Write-Output "Checking Cisco PowerTool"
$PowerToolLoaded = $null
$Modules = Get-Module
$PowerToolLoaded = $modules.name
if ( -not ($Modules -like "ciscoUcsPs"))
{
Write-Output " Loading Module: Cisco UCS PowerTool Module"
Import-Module ciscoUcsPs
$Modules = Get-Module
if ( -not ($Modules -like "ciscoUcsPs"))
{
Write-Output ""
Write-Output " Cisco UCS PowerTool Module did not load. Please correct his issue and try again"
Write-Output " Exiting..."
exit
}
else
{
Write-Output " PowerTool is now Loaded"
}
}
else
{
Write-Output " PowerTool is already Loaded"
}
#Select UCS Domain(s) for login
if ($UCSM -ne "")
{
$myucs = $UCSM
}
else
{
Write-Output ""
$myucs = Read-Host "Enter UCS system IP or Hostname or a list of systems separated by commas"
}
[array]$myucs = ($myucs.split(",")).trim()
if ($myucs.count -eq 0)
{
Write-Output ""
Write-Output "You didn't enter anything"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
#Make sure we are disconnected from all UCS Systems
Disconnect-Ucs
#Test that UCSM(s) are IP Reachable via Ping
Write-Output "Testing PING access to UCSM"
foreach ($ucs in $myucs)
{
$ping = new-object system.net.networkinformation.ping
$results = $ping.send($ucs)
if ($results.Status -ne "Success")
{
Write-Output " Can not access UCSM $ucs by Ping"
Write-Output " It is possible that a firewall is blocking ICMP (PING) Access. Would you like to try to log in anyway?"
if ($SKIPERROR)
{
$Try = "y"
}
else
{
$Try = Read-Host "Would you like to try to log in anyway? (Y/N)"
}
if ($Try -ieq "y")
{
Write-Output " Will try to log in anyway!"
}
elseif ($Try -ieq "n")
{
Write-Output ""
Write-Output "You have chosen to exit"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
else
{
Write-Output ""
Write-Output "You have provided invalid input"
Write-Output " Exiting..."
Write-Output ""
Disconnect-Ucs
exit
}
}
else
{
Write-Output " Successful access to $ucs by Ping"
}
}
#Log into the UCS System(s)
$multilogin = Set-UcsPowerToolConfiguration -SupportMultipleDefaultUcs $true
Write-Output ""
Write-Output "Logging into UCS"
#Verify PowerShell Version to pick prompt type
$PSVersion = $psversiontable.psversion
$PSMinimum = $PSVersion.Major
if (!$UCREDENTIALS)
{
if (!$SAVEDCRED)
{
if ($PSMinimum -ge "3")
{
Write-Output " Enter your UCSM credentials"
$cred = Get-Credential -Message "UCSM(s) Login Credentials" -UserName "admin"
}
else
{
Write-Output " Enter your UCSM credentials"
$cred = Get-Credential
}
}
else
{
$CredFile = import-csv $SAVEDCRED
$Username = $credfile.UserName
$Password = $credfile.EncryptedPassword
$cred = New-Object System.Management.Automation.PsCredential $Username,(ConvertTo-SecureString $Password)
}
}
foreach ($myucslist in $myucs)
{
Write-Output " Logging into: $myucslist"
$myCon = $null
$myCon = Connect-Ucs $myucslist -Credential $cred
if (($mycon).Name -ne ($myucslist))
{
#Exit Script
Write-Output " Error Logging into this UCS domain"
if ($myucs.count -le 1)
{
$continue = "n"
}
else
{
if ($SKIPERROR)
{
$continue = "y"
}
else
{
$continue = Read-Host "Continue without this UCS domain (Y/N)"
}
}
if ($continue -ieq "n")
{
Write-Output ""
Write-Output "Exiting Script..."
Disconnect-Ucs
exit
}
else
{
Write-Output " Continuing..."
}
}
else
{
Write-Output " Login Successful"
}
sleep 1
}
if ($myCon.count -eq 0)
{
Write-Output ""
Write-Output "You are not logged into any UCSM systems"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
#Generating Labels for Blade Servers
if ((Get-UcsBlade).Count -ne 0)
{
Write-Output ""
Write-Output "Generating User Labels for Blade Servers..."
foreach ($Blade in (Get-UcsBlade | Sort-Object -Property Ucs, ChassisId, SlotId))
{
$Model = (($Blade | Get-UcsCapability).Name -replace "Cisco UCS ", "") -replace " ", "-"
$Long = $Blade | Get-UcsComputeBoard | Get-UcsProcessorUnit -Id 1 | select -ExpandProperty Model
if ($Long -match 'Intel.*?([EXL\-57]+\s*\d{4}L*\b(\sv2)?)')
{
$CPU = $Matches[1] -replace '- ', "-"
}
else
{
$CPU = "<<<Unknown>>>"
}
$RAM = ($Blade.TotalMemory / 1KB)
$NumberOfCPUs = $Blade.NumOfCpus
$CustomLabel = "$Model / $NumberOfCPUs x $CPU / $RAM"+"GB"
$Blade | Set-UcsBlade -UsrLbl $CustomLabel -Force | Out-Null
$Display = " UCS: "+($Blade.Ucs).PadRight(32)+" Chassis: "+$Blade.ChassisId+" Slot: "+$Blade.SlotId+" User Label: "+$CustomLabel
Write-Output $Display
}
}
#Generating Labels for Rack Servers
if ((Get-UcsRackUnit).Count -ne 0)
{
Write-Output ""
Write-Output "Generating User Labels for Rack Servers..."
foreach ($Rack in (Get-UcsRackUnit | Sort-Object -Property Ucs, ServerId))
{
$Model = (($Rack | Get-UcsCapability).Name -replace "Cisco UCS ", "") -replace " ", "-"
$Long = $Rack | Get-UcsComputeBoard | Get-UcsProcessorUnit -Id 1 | select -ExpandProperty Model
if ($Long -match 'Intel.*?([EXL\-57]+\s*\d{4}L*\b(\sv2)?)')
{
$CPU = $Matches[1] -replace '- ', "-"
}
else
{
$CPU = "<<<Unknown>>>"
}
$RAM = ($Rack.TotalMemory / 1KB)
$NumberOfCPUs = $Rack.NumOfCpus
$CustomLabel = "$Model / $NumberOfCPUs x $CPU / $RAM"+"GB"
$Rack | Set-UcsRackUnit -UsrLbl $CustomLabel -Force | Out-Null
$Display = " UCS: "+($Rack.Ucs).PadRight(32)+" Rack Unit: "+$Rack.ServerId+" User Label: "+$CustomLabel
Write-Output $Display
}
}
#There are no blade or rack servers
if (((Get-UcsBlade).Count -eq 0) -and ((Get-UcsRackUnit).Count -eq 0))
{
Write-Output ""
Write-Output "Your UCS System(s) do not contain any blade or rack servers"
Write-Output " Exiting..."
}
#Disconnect from UCSM(s)
Disconnect-Ucs
#Exit the Script
Write-Output ""
Write-Output "Script Complete"
Write-Output " Exiting..."
exit