-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerOff.ps1
161 lines (133 loc) · 4.57 KB
/
powerOff.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
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
<#
.SYNOPSIS
Powers off Virtual Machines in a vCloud environment
.DESCRIPTION
Powers off Virtual Machines in a vCloud environment asynchronously. if it fails it sends an email to the end user with the error message.
.NOTES
File Name : powerOn.ps1
Author : Glen Towsey - Datacom
Date : 21/02/2019
.LINK
https://github.com/gtowsey/VCD
#>
### secton for handling passwords. password is encrypted using the generateEncryptedPassword.ps1. only the account that encrypts can use.
$username = ''
$password_file = $PSScriptRoot + "\Password.txt"
### log location for erros
$log_location = $PSScriptRoot + "\PowerOffLog.txt"
### csv location for VM's to use
$csv_location = $PSScriptRoot + "\vmList.csv"
### modify depending on location you wish to connect to
$vCloud_server_name = ""
### enter organisation you wish to connect to
$vCloud_organisation = ''
### smtp settings
[string]$smtprelay = ""
[string]$smtpfrom = ""
[string]$smtpto = ""
[string]$smtpsubject = ""
### Check if teh log csv exists elseattempt to create the csv.fail if user doesnt have rights etc.
function send-email ($body){
Send-MailMessage -From $smtpfrom -To $smtpto -Subject $smtpsubject -smtpServer $smtprelay -body $body
add-content $log_location $_.Exception.message
}
###Check if teh log csv exists elseattempt to create the csv.fail if user doesnt have rights etc.
if (!(test-path $log_location)) {
try {
out-file $log_location
}
catch {
#add-content $log_location "$(get-date) unable to create log file in selected path"
send-email "$(get-date) unable to create log file"
exit
}
}
###attempt to import csv data. fail wcript if not found
try {
$csv = import-csv $csv_location
}
catch {
add-content $log_location "$(get-date) unable to import csv"
send-email "$(get-date) unable to import csv"
exit
}
###download powerCLI module of at least version 5.0.1 from the VMWare website
###confirm module exists
Try {
import-module vmware.vimautomation.cloud
}
catch {
add-content $log_location "$(get-date) unable to import vCloud module"
send-email "$(get-date) unable to import vCloud module"
exit
}
###attempt to import password file
try {
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (Get-Content $password_file | ConvertTo-SecureString)
}
catch {
add-content $log_location "$(get-date) unable to open password file"
send-email "$(get-date) unable to open password file"
exit
}
#attempt to connect to vCloud server. exit if failed
try {
connect-Ciserver $vCloud_server_name -credential $credential -org $vCloud_organisation | out-null
}
catch {
add-content $log_location "$(get-date) organisation or credentials incorrect"
send-email "$(get-date) organisation or credentials incorrect"
exit
}
#blank arrays for handlign responses
$results = @()
$errors = @()
### loop through each VM and atatmept to power off. graceful shutdown if enabled in csv. errors are captured and stored.
### Commands are filed async rather than waiting to speed up
foreach ($line in $csv) {
try {
$ErrorActionPreference = "Stop"
if ($line.GracefulShutdown -eq 'no') {
$results += Get-CIVM -name $line.vm | Stop-CIVM -confirm:$false -RunAsync
}
else {
$results += Get-CIVM -name $line.vm | Stop-CIVMGuest -confirm:$false -RunAsync
}
}
catch {
$errors += [PSCustomObject]@{
VM = $line.vm
error = $_.Exception.message
}
}
}
### blank arrays for handling responses
$FailedTasks = @()
### loop through async tasks and confirm all have finished. add any errored
foreach ($result in $results) {
while (!((get-task -id $result.id).FinishTime)) {
start-sleep -seconds 5
}
$CompletedTask = get-task -id $result.id
if ($CompletedTask.state -eq 'error') {
$FailedTasks += $CompletedTask
}
}
### loop through the emails to build up a somewhat valid email
$body = $null
$body += "The following VM's failed to shutdown pre shutdown task:"
$body += $($errors | out-string -width 300)
$body += "The following VM's failed post shutdown task: `n"
foreach ($failedtask in $FailedTasks) {
$body += $FailedTasks.description + "`n"
$body += $FailedTasks.extensiondata.error.message + "`n"
}
### send email with any errors
if ($errors) {
add-content $log_location $($errors | out-string -width 300)
send-email $body
}
### disconnect from vcloud server
Disconnect-CIServer -confirm:$false
### cleanly exit
exit