-
Notifications
You must be signed in to change notification settings - Fork 4
/
PowerCLI-User-Cmdlets.psm1
467 lines (411 loc) · 13.8 KB
/
PowerCLI-User-Cmdlets.psm1
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<#
.Synopsis
Get a list of VM Consoles and display them graphically.
.DESCRIPTION
A graphical utility for connecting to a vCenter server and connecting to available HTML5 consoles
.EXAMPLE
Get-VMConsoles vcenter.contoso.com
.EXAMPLE
Another example of how to use this cmdlet
#>
function Get-VMConsoles
{
[CmdletBinding()]
[OutputType([int])]
Param
(
)
Begin
{
}
Process
{
$defaultvCenter = "vcenter"
$viServer = $null
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 10/24/2013 11:00 AM
########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$hostnameTextbox = New-Object System.Windows.Forms.TextBox
$connectButton = New-Object System.Windows.Forms.Button
$exitButton = New-Object System.Windows.Forms.Button
$openButton = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$exitButton_OnClick=
{
if ($global:viServer) {
$global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
}
$form1.Close()
}
$connectButton_OnClick=
{
$vCenter = $hostnameTextbox.Text
$global:viServer.IsConnected
if ($viServer.IsConnected) {
Write-Host "Disconnecting from $vCenter"
$global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
$openButton.Enabled = $false
}
$global:viServer = Connect-VIServer $vCenter -ErrorAction SilentlyContinue
if ($global:viServer.IsConnected) {
Write-Host "Successfully connected to $vCenter"
$openButton.Enabled = $true
}
else {
[System.Windows.Forms.MessageBox]::Show("Failed to connect to vCenter server '$vCenter'", "Connection Failed")
$global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
$openButton.Enabled = $false
}
}
$openButton_OnClick=
{
$Selection = Get-VM | Out-GridView -OutputMode Multiple
$Selection | Foreach { Open-VMConsoleWindow $_ }
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 108
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "VM Console Viewer"
$hostnameTextbox.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 16
$hostnameTextbox.Location = $System_Drawing_Point
$hostnameTextbox.Name = "hostnameTextbox"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 156
$hostnameTextbox.Size = $System_Drawing_Size
$hostnameTextbox.TabIndex = 3
$hostnameTextbox.Text = $defaultvCenter
$form1.Controls.Add($hostnameTextbox)
$connectButton.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 174
$System_Drawing_Point.Y = 14
$connectButton.Location = $System_Drawing_Point
$connectButton.Name = "connectButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 98
$connectButton.Size = $System_Drawing_Size
$connectButton.TabIndex = 2
$connectButton.Text = "Connect"
$connectButton.UseVisualStyleBackColor = $True
$connectButton.add_Click($connectButton_OnClick)
$form1.Controls.Add($connectButton)
$exitButton.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 72
$exitButton.Location = $System_Drawing_Point
$exitButton.Name = "exitButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 260
$exitButton.Size = $System_Drawing_Size
$exitButton.TabIndex = 1
$exitButton.Text = "Quit"
$exitButton.UseVisualStyleBackColor = $True
$exitButton.add_Click($exitButton_OnClick)
$form1.Controls.Add($exitButton)
$openButton.DataBindings.DefaultDataSourceUpdateMode = 0
$openButton.Enabled = $False
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 43
$openButton.Location = $System_Drawing_Point
$openButton.Name = "openButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 260
$openButton.Size = $System_Drawing_Size
$openButton.TabIndex = 0
$openButton.Text = "Open VM Consoles"
$openButton.UseVisualStyleBackColor = $True
$openButton.add_Click($openButton_OnClick)
$form1.Controls.Add($openButton)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
}
End
{
}
}
<#
.Synopsis
Report on VMHost Name, Model, and BIOS.
.DESCRIPTION
Gathers all VMHosts under the connected vCenter server and outputs each host's Name, Model and BIOS Version.
Optionally, generates CSV output in the specified directory.
Based on http://www.shogan.co.uk/vmware/three-powercli-scripts-for-information-gathering-vms-hosts-etc/ by Sean Duffy (@shogan85)
EXAMPLE
Get-VMHostBIOSInfo
.EXAMPLE
Get-VMHostBIOSInfo -CSV H:\
#>
function Get-VMHostBIOSInfo
{
[CmdletBinding()]
[OutputType([System.Array])]
Param
(
# Directory to store optional CSV report in.
[string]
$CSV = ''
)
Begin
{
}
Process
{
$Report = @()
New-VIProperty -Name BIOSVersion -ObjectType VMHost -ValueFromExtensionProperty 'Hardware.BiosInfo.BiosVersion' -Force | Out-Null
$VMHosts = Get-VMHost | Select Name, Model, BIOSVersion
foreach ($VMHost in $VMHosts)
{
$Row = New-Object -Type PSObject -Property @{
Name = $VMHost.Name
Model = $VMHost.Model
BiosVersion = $VMHost.BIOSVersion
}
$Report += $Row
}
if ($CSV -ne '') {
$File = $CSV + 'VMHostInfo.csv'
$Report | Export-Csv -Path $File -NoTypeInformation
}
return $Report
}
End
{
}
}
<#
.Synopsis
Report on VMHosts ESX(i) version info.
.DESCRIPTION
Gathers all VMHosts under the connected vCenter server and outputs each host's Name, ESX(i) Version, and ESX(i) Build.
Optionally, generates CSV output in the specified directory.
Based on http://www.shogan.co.uk/vmware/three-powercli-scripts-for-information-gathering-vms-hosts-etc/ by Sean Duffy (@shogan85).
EXAMPLE
Get-VMHostESXInfo
.EXAMPLE
Get-VMHostESXInfo -CSV H:\
#>
function Get-VMHostESXInfo
{
[CmdletBinding()]
[OutputType([System.Array])]
Param
(
# Directory to store optional CSV report in.
[string]
$CSV = ''
)
Begin
{
}
Process
{
$Report = @()
New-VIProperty -Name BuildVersion -ObjectType VMHost -ValueFromExtensionProperty 'Config.Product.Build' -Force | Out-Null
$VMHosts = Get-VMHost | Select Name, Version, BuildVersion | Sort Name
foreach ($VMHost in $VMHosts)
{
$Row = New-Object -Type PSObject -Property @{
Name = $VMHost.Name
Version = $VMHost.Version
Build = $VMHost.BuildVersion
}
$Report += $Row
}
if ($CSV -ne '') {
$File = $CSV + 'VMHostVersionInfo.csv'
$Report | Export-Csv -Path $File -NoTypeInformation
}
return $Report
}
End
{
}
}
<#
.Synopsis
Get VM Hardware details
.DESCRIPTION
Get the VM Hardware details for all VMs in a specified datacenter.
Optionally, generates CSV output in the specified directory.
Based on http://www.shogan.co.uk/vmware/three-powercli-scripts-for-information-gathering-vms-hosts-etc/ by Sean Duffy (@shogan85)
.EXAMPLE
Get-VMHardwareInfo -Datacenter 'lab'
.EXAMPLE
Get-VMHardwareInfo -Datacenter 'lab' -CSV 'H:\'
.EXAMPLE
Get-Datacenter | Get-VMHardwareInfo
#>
function Get-VMHardwareInfo
{
[CmdletBinding()]
[OutputType([System.Array])]
Param
(
# Datacenter to run the report against.
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
$DataCenter,
# Directory to store optional CSV report in.
[string]
$CSV = ''
)
Begin
{
}
Process
{
$Report = @()
New-VIProperty -Name HWVersion -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.Version' -Force | Out-Null
$VMs = Get-Datacenter $DataCenter | Get-VM | Select Name, HWVersion | Sort Name
foreach ($VM in $VMs)
{
$Row = New-Object -Type PSObject -Property @{
Name = $VM.Name
HWVersion = $VM.HWVersion
}
$Report += $Row
}
if ($CSV -ne '') {
$File = $CSV + 'VMHardwareInfo-' + $DataCenter + '.csv'
$Report | Export-Csv -Path $File -NoTypeInformation
}
return $Report
}
End
{
}
}
<#
.Synopsis
Provide a Google Chart of VM resource usage on a VMHost
.DESCRIPTION
Create a Google Chart of VM resource usage on a VMHost and display it in the user's browser. A per-VMhost output file is saved for future reference.
Based on http://hostilecoding.blogspot.com/2014/03/vmware-vm-stats-using-powercli-and.html by @HostileCoding
.EXAMPLE
Get-VMHostGoogleChart -VMHost esxi -FileLocation 'H:\'
.EXAMPLE
Get-VMHostGoogleChart -VMHost esxi -FileLocation 'H:\' -Stat 'disk.usage.average'
.EXAMPLE
Get-VMHost | Get-VMHostGoogleChart -FileLocation 'H:\'
#>
function Get-VMHostGoogleChart
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# VMHost to chart. Name must be as it appears in the vCenter inventory, or on the VMHost itself.
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
$VMHost,
# Location to save HTML output.
[Parameter(Mandatory=$true)]
[string]
$FileLocation,
# Stat to measure.
#
# Available stats:
#
# cpu.usage.average
# cpu.usagemhz.average
# mem.usage.average * Default
# disk.usage.average
# net.usage.average
[string]
$Stat = "mem.usage.average"
)
Begin
{
}
Process
{
$HtmlHeader = @"
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
//What we are measuring
['Virtual Machine', 'Average Usage'],
"@
$VMs = Get-VMHost -Name $VMHost | Get-VM | Where-Object PowerState -match "PoweredOn" #Retrieves all powered on VMs from a specific host
foreach ($VM in $VMs) {
$Value = Get-Stat -Entity $VM.Name -Stat $Stat -Start (Get-Date).AddHours(-24) -MaxSamples (10) -IntervalMins 10 | Measure-Object Value -Average
$Data += "['$VM', $($Value.Average)],"
}
$HtmlFooter = @"
]);
var options = {
title: '$Stat', //Chart Title
pieHole: 0.4, //Option regarding this specific kind of chart
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id='title'>PowerCLI Google Charts</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>
<div id="box1">
<div id='boxheader'>$Stat</div>
<div id='boxcontent'>
<div id="donutchart" style="width: 900px; height: 500px;"></div>
</div>
</div>
</body>
</html>
"@
# Generate the HTML file
$FileName = $FileLocation + '\' + $VMHost + '.' + $stat + '.html'
$HtmlHeader + $Data + $HtmlFooter | Out-File $FileName
Start-Process $FileName
}
End
{
}
}