-
Notifications
You must be signed in to change notification settings - Fork 2
/
chapter_14.ps1
109 lines (101 loc) · 2.72 KB
/
chapter_14.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
#region Think Ahead and Build Abstraction “Layers”
#requires -Module HyperV
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$VmName
)
## Use the Start-Vm cmdlet in the HyperV module to start a VM
Start-Vm -Name $VmName
## Do some stuff to the VM here
## Use the Stop-Vm cmdlet in the HyperV module to stop the VM
Stop-Vm -Name $VmName
#requires -Module HyperV, VmWare
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$VmName,
[Parameter()]
[ValidateSet('HyperV','VmWare')]
[string]$Hypervisor
)
switch ($Hypervisor) {
'HyperV' {
## Use the Start-Vm cmdlet in the HyperV module to start a VM
Start-Vm -Name $VmName
## Do some stuff to the VM here
## Use the Stop-Vm cmdlet in the HyperV module to stop the VM
Stop-Vm -Name $VmName
break
}
'VmWare' {
## Use whatever command the VmWare module has in it to start the VM
Start-VmWareVm -Name $VmName
## do stuff to the VmWare VM
## Use whatever command the VmWare module has in it to stop the VM
Stop-VmWareVm -Name $VmName
break
}
default {
"The hypervisor you passed [$_] is not supported"
}
}
#requires -Module HyperV, VmWare
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$VmName
)
function Get-Hypervisor {
## simple helper function to determine the hypervisor
param(
[Parameter()]
[string]$VmName
)
## Some code here to figure out what type of hypervisor this VM is running on
## Return either 'HyperV' or 'VmWare'
}
$hypervisor = Get-Hypervisor -VmName $VmName
switch ($Hypervisor) {
'HyperV' {
## Use the Start-Vm cmdlet in the HyperV module to start a VM
Start-Vm -Name $VmName
## Do some stuff to the VM here
## Use the Stop-Vm cmdlet in the HyperV module to stop the VM
Stop-Vm -Name $VmName
break
}
'VmWare' {
## Use whatever command the VmWare module has in it to start the VM
Start-VmWareVm -Name $VmName
## do stuff to the VmWare VM
## Use whatever command the VmWare module has in it to stop the VM
Stop-VmWareVm -Name $VmName
break
}
default {
"The hypervisor you passed [$_] is not supported"
}
}
#endregion
#region Make Module Functions Return Common Object Types
function Get-Thing {
[CmdletBinding()]
param()
## Do some stuff
[pscustomobject]@{
'Name' = 'XXXX'
'Type' = 'XXXX'
}
}
function Get-OtherThing {
[CmdletBinding()]
param()
[pscustomobject]@{
'Name' = 'XXXX'
'Type' = 'XXXX'
'Property1' = 'Value1'
'Property2' = 'Value2'
}
}
#endregion