GCP Snapshot with Conditions(on the 9th snapshot delete the oldest and retain only 8)
1.
#========================================================================================
#
#Run Powershell as Administrator for the Extraction of Log
files
#
#========================================================================================
param([switch]$Elevated)
function Test-Admin
{
$currentUser
= New-Object
Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin)
-eq $false) {
if ($elevated) {
# tried
to elevate, did not work, aborting
} else
{
Start-Process
powershell.exe -Verb
RunAs -ArgumentList
('-noprofile -noexit -file "{0}"
-elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
'running with full privileges'
2.
#========================================================================================
#
#Connect Powershell to Google Cloud Platform
#
#========================================================================================
gcloud auth
login jericagra0923@gmail.com
Screenshot:



3.
#========================================================================================
#
# Set the path and file name for PowerShell transcripts (logs)
to be written to.
#
#========================================================================================
$LogPath = "C:\gcp-logs\"
$LogFile = Get-Date -Format FileDateTimeUniversal
$TranscriptFileName = $LogPath + $LogFile +".txt"
4.
#========================================================================================
#
# Start the transcript.
#
#========================================================================================
Start-Transcript -Path $TranscriptFileName
5.
#========================================================================================
#
#Set the GCP project.
#
#========================================================================================
$Project = "evident-ocean-315608"
6.
#========================================================================================
#
#Set the zone(s) where the disks are that you would like to
take snapshots of.
#
#================================================================================
$Zones = "asia-northeast1-b"
Screenshots for 5 and 6

7.
#========================================================================================
#
#Record the date that the snapshots started.
#
#================================================================================
$StartTime = Get-Date
Screenshots:

8.
#========================================================================================
#
#Do snapshot all of the disks in the zones as well as confirm
#
#================================================================================
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
}
}
Screenshot:
9.
#========================================================================================
#
##-Check the number of snapshots generated
#
#========================================================================================
Write-Host "=========================================="
$snapshotM = Get-GceSnapshot | Measure-Object
Write-Host "total
Number of Snapshots:" $snapshotM.Count
Write-Host "=========================================="
Screenshot:
Comments
Post a Comment