-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcloneDisk.ps1
98 lines (82 loc) · 3.09 KB
/
cloneDisk.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
Param (
[string]$boxname,
[string]$diskFrom,
[string]$diskto,
[string]$emailTo,
[string]$smtpServer,
[string]$emailFrom
)
$scriptName = 'cloneDisk.ps1'
cmd /c "exit 0"
# Use executeIgnoreExit to only trap powershell errors, use executeExpression to trap all errors, including $LASTEXITCODE
function execute ($expression) {
$error.clear()
Write-Host "[$(date)] $expression"
try {
Invoke-Expression $expression
if(!$?) { Write-Host "`$? = $?"; emailAndExit 1050 }
} catch { echo $_.Exception|format-list -force; emailAndExit 1051 }
if ( $error[0] ) { Write-Host "`$error[0] = $error"; emailAndExit 1052 }
}
function executeExpression ($expression) {
execute $expression
if (( $LASTEXITCODE ) -and ( $LASTEXITCODE -ne 0 )) {
Write-Host "[$scriptName] ERROR! Exiting with `$LASTEXITCODE = $LASTEXITCODE" -foregroundcolor "red";
emailAndExit $LASTEXITCODE
}
}
function executeIgnoreExit ($expression) {
execute $expression
if (( $LASTEXITCODE ) -and ( $LASTEXITCODE -ne 0 )) { Write-Host "[$scriptName] Warning `$LASTEXITCODE = $LASTEXITCODE" -foregroundcolor "yellow"; cmd /c "exit 0" }
}
# Exception Handling email sending
function emailAndExit ($exitCode) {
if ($smtpServer) {
Send-MailMessage -To "$emailTo" -From "$emailFrom" -Subject "[$scriptName] ERROR $exitCode" -SmtpServer "$smtpServer"
}
exit $exitCode
}
# Informational email notification
function emailProgress ($subject) {
if ($smtpServer) {
Send-MailMessage -To "$emailTo" -From "$emailFrom" -Subject "[$scriptName] $subject" -SmtpServer "$smtpServer"
}
}
Write-Host "`n[$scriptName] ---------- start ----------"
if ($boxname) {
Write-Host "[$scriptName] boxname : $boxname"
} else {
Write-Host "[$scriptName] boxname not supplied"; exit 7020
}
if ($diskFrom) {
Write-Host "[$scriptName] diskFrom : $diskFrom"
} else {
Write-Host "[$scriptName] diskFrom not supplied"; exit 7021
}
if ($diskTo) {
Write-Host "[$scriptName] diskTo : $diskTo"
} else {
Write-Host "[$scriptName] diskTo not supplied"; exit 7022
}
if ($emailTo) {
Write-Host "[$scriptName] emailTo : $emailTo"
} else {
Write-Host "[$scriptName] emailTo : (not specified, email will not be attempted)"
}
if ($smtpServer) {
Write-Host "[$scriptName] smtpServer : $smtpServer"
} else {
Write-Host "[$scriptName] smtpServer : (not specified, email will not be attempted)"
}
if ($emailFrom) {
Write-Host "[$scriptName] emailFrom : $emailFrom"
} else {
Write-Host "[$scriptName] emailFrom : (not specified, email will not be attempted)"
}
emailProgress "Starting disk copy from $diskFrom and clone to $diskDir"
Write-Host "[$scriptName] From Hyper-V image"
executeExpression "cp $diskFrom $diskTo"
$diskDir = "D:\VMs\$boxname" # This is the default used in AtlasPackage
& "C:\Program Files\Oracle\Virtualbox\VBoxmanage.exe" clonehd "$diskTo" "$diskDir\$boxname.vdi" --format vdi
emailProgress "COMLETE disk copy from $diskFrom and clone to $diskDir"
Write-Host "`n[$scriptName] ---------- stop ----------"