This repository was archived by the owner on Apr 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_vsphere.ps1
More file actions
215 lines (194 loc) · 14.8 KB
/
template_vsphere.ps1
File metadata and controls
215 lines (194 loc) · 14.8 KB
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
<#
.SYNOPSIS
This is a summary of what the script is.
.DESCRIPTION
This is a detailed description of what the script does and how it is used.
.PARAMETER help
Displays a help message (seriously, what did you think this was?)
.PARAMETER history
Displays a release history for this script (provided the editors were smart enough to document this...)
.PARAMETER log
Specifies that you want the output messages to be written in a log file as well as on the screen.
.PARAMETER debugme
Turns off SilentlyContinue on unexpected error messages.
.PARAMETER vcenter
VMware vCenter server hostname. Default is localhost. You can specify several hostnames by separating entries with commas.
.EXAMPLE
Connect to a vCenter server of your choice:
PS> .\template.ps1 -vcenter myvcenter.local
.LINK
http://www.nutanix.com/services
.NOTES
Author: Stephane Bourdeaud (sbourdeaud@nutanix.com)
Revision: June 19th 2015
#>
#region parameters
######################################
## parameters and initial setup ##
######################################
#let's start with some command line parsing
Param
(
#[parameter(valuefrompipeline = $true, mandatory = $true)] [PSObject]$myParam1,
[parameter(mandatory = $false)] [switch]$help,
[parameter(mandatory = $false)] [switch]$history,
[parameter(mandatory = $false)] [switch]$log,
[parameter(mandatory = $false)] [switch]$debugme,
[parameter(mandatory = $false)] [string]$vcenter
)
#endregion
#region functions
########################
## main functions ##
########################
#this function is used to output log data
Function OutputLogData
{
#input: log category, log message
#output: text to standard output
<#
.SYNOPSIS
Outputs messages to the screen and/or log file.
.DESCRIPTION
This function is used to produce screen and log output which is categorized, time stamped and color coded.
.NOTES
Author: Stephane Bourdeaud
.PARAMETER myCategory
This the category of message being outputed. If you want color coding, use either "INFO", "WARNING", "ERROR" or "SUM".
.PARAMETER myMessage
This is the actual message you want to display.
.EXAMPLE
PS> OutputLogData -mycategory "ERROR" -mymessage "You must specify a cluster name!"
#>
param
(
[string] $category,
[string] $message
)
begin
{
$myvarDate = get-date
$myvarFgColor = "Gray"
switch ($category)
{
"INFO" {$myvarFgColor = "Green"}
"WARNING" {$myvarFgColor = "Yellow"}
"ERROR" {$myvarFgColor = "Red"}
"SUM" {$myvarFgColor = "Magenta"}
}
}
process
{
Write-Host -ForegroundColor $myvarFgColor "$myvarDate [$category] $message"
if ($log) {Write-Output "$myvarDate [$category] $message" >>$myvarOutputLogFile}
}
end
{
Remove-variable category
Remove-variable message
Remove-variable myvarDate
Remove-variable myvarFgColor
}
}#end function OutputLogData
#endregion
#region prepwork
# get rid of annoying error messages
if (!$debugme) {$ErrorActionPreference = "SilentlyContinue"}
#check if we need to display help and/or history
$HistoryText = @'
Maintenance Log
Date By Updates (newest updates at the top)
---------- ---- ---------------------------------------------------------------
06/19/2015 sb Initial release.
################################################################################
'@
$myvarScriptName = ".\template.ps1"
if ($help) {get-help $myvarScriptName; exit}
if ($History) {$HistoryText; exit}
#let's make sure the VIToolkit is being used
$myvarPowerCLI = Get-PSSnapin VMware.VimAutomation.Core -Registered
try {
switch ($myvarPowerCLI.Version.Major) {
{$_ -ge 6}
{
Import-Module VMware.VimAutomation.Vds -ErrorAction Stop
OutputLogData -category "INFO" -message "PowerCLI 6+ module imported"
}
5 {
Add-PSSnapin VMware.VimAutomation.Vds -ErrorAction Stop
OutputLogData -category "WARNING" -message "PowerCLI 5 snapin added; recommend upgrading your PowerCLI version"
}
default {throw "This script requires PowerCLI version 5 or later"}
}
}
catch {throw "Could not load the required VMware.VimAutomation.Vds cmdlets"}
#let's load the Nutanix cmdlets
if ((Get-PSSnapin -Name NutanixCmdletsPSSnapin -ErrorAction SilentlyContinue) -eq $null)#is it already there?
{
try {
Add-PSSnapin NutanixCmdletsPSSnapin -ErrorAction Stop #no? let's add it
}
catch {
Write-Warning $($_.Exception.Message)
OutputLogData -category "ERROR" -message "Unable to load the Nutanix snapin. Please make sure the Nutanix Cmdlets are installed on this server."
return
}
}
#endregion
#region variables
#misc variables
$myvarElapsedTime = [System.Diagnostics.Stopwatch]::StartNew() #used to store script begin timestamp
$myvarvCenterServers = @() #used to store the list of all the vCenter servers we must connect to
$myvarOutputLogFile = (Get-Date -UFormat "%Y_%m_%d_%H_%M_")
$myvarOutputLogFile += "OutputLog.log"
#endregion
#region parameters validation
############################################################################
# command line arguments initialization
############################################################################
#let's initialize parameters if they haven't been specified
if (!$vcenter) {$myvarvCenterServers += $env:computername}#assign localhost if no vCenter has been specified
else{$myvarvCenterServers = $vcenter.Split()}#otherwise make sure we parse the argument in case it contains several entries
#endregion
#region processing
################################
## foreach vCenter loop ##
################################
foreach ($myvarvCenter in $myvarvCenterServers)
{
OutputLogData -category "INFO" -message "Connecting to vCenter server $myvarvCenter..."
if (!($myvarvCenterObject = Connect-VIServer $myvarvCenter))#make sure we connect to the vcenter server OK...
{#make sure we can connect to the vCenter server
$myvarerror = $error[0].Exception.Message
OutputLogData -category "ERROR" -message "$myvarerror"
return
}
else #...otherwise show the error message
{
OutputLogData -category "INFO" -message "Connected to vCenter server $myvarvCenter."
}#endelse
if ($myvarvCenterObject)
{
######################
#main processing here#
######################
}#endif
OutputLogData -category "INFO" -message "Disconnecting from vCenter server $vcenter..."
Disconnect-viserver -Confirm:$False #cleanup after ourselves and disconnect from vcenter
}#end foreach vCenter
#endregion
#region cleanup
#########################
## cleanup ##
#########################
#let's figure out how much time this all took
OutputLogData -category "SUM" -message "total processing time: $($myvarElapsedTime.Elapsed.ToString())"
#cleanup after ourselves and delete all custom variables
Remove-Variable myvar* -ErrorAction SilentlyContinue
Remove-Variable ErrorActionPreference -ErrorAction SilentlyContinue
Remove-Variable help -ErrorAction SilentlyContinue
Remove-Variable history -ErrorAction SilentlyContinue
Remove-Variable log -ErrorAction SilentlyContinue
Remove-Variable vcenter -ErrorAction SilentlyContinue
Remove-Variable debugme -ErrorAction SilentlyContinue
#endregion