-
Notifications
You must be signed in to change notification settings - Fork 3
/
new-snap.ps1
62 lines (48 loc) · 2.22 KB
/
new-snap.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
# Set the path and file name for PowerShell transcripts (logs) to be written to.
$LogPath = "c:\logs\powershell\snaps\"
$LogFile = Get-Date -Format FileDateTimeUniversal
$TranscriptFileName = $LogPath + $LogFile +".txt"
# Start the transcript.
Start-Transcript -Path $TranscriptFileName
#Set the GCP project.
$Project = "put-your-gcp-project-here-12345"
#Set the zone(s) where the disks are that you would like to take snapshots of.
$Zones = "us-east1-d", "us-central1-c"
#Record the date that the snapshots started.
$StartTime = Get-Date
#Go snapshot all of the disks in the zones identified above.
foreach ($Zone in $Zones) {
$DisksInZone = Get-GceDisk -Project $Project -zone $Zone | foreach { $_.Name }
foreach ($Disk in $DisksInZone) {
Write-Output "=========================================="
Write-Output "$Zone "-" $Disk"
Write-Output "=========================================="
Add-GceSnapshot -project $Project -zone $Zone $Disk #In the future we could clean this output up a bit.
}
}
#Record the date that the snapshots ended.
$EndTime = Get-Date
#Print out the start and end times.
Write-Output "=========================================="
Write-Output "Started at:" $StartTime
Write-Output "Ended at:" $EndTime
Write-Output "=========================================="
#Stop the transcript.
Stop-Transcript
#Send the PowerShell transcript (log) by email. You can delete this entire section if you don't want log copies delivered by email.
#Google Cloud Platform blocks direct outbound mail on port 25. Reference: https://cloud.google.com/compute/docs/tutorials/sending-mail/
#Mail Server Settings
$smtpServer = "mail.yourdomainname.com"
$smtpPort = "2525" #Don't put 25 here it will not work. See link above.
$att = new-object Net.Mail.Attachment($TranscriptFileName)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
# Set the email from / to / subject / body / etc here:
$msg.From = "[email protected]"
$msg.To.Add("[email protected]")
$msg.Subject = "GCP Snapshot Report"
$msg.Body = "Please see the attached PowerShell transcript."
# Attach the log and ship it.
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()