Skip to content

Commit

Permalink
Create AutomatedSnapshots.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
rai69 authored Jul 26, 2019
1 parent 359146f commit 8479a4c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions AutomatedSnapshots.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}


$disks=Get-AzureRmDisk -ResourceGroupName "MyResourceGroup" | Select Name,Tags,Id,Location,ResourceGroupName ;
foreach($disk in $disks)
{
foreach($tag in $disk.Tags)
{
if($tag.Snapshot -eq 'True')
{
$snapshotconfig = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $disk.Location -AccountType Premium_LRS;
$SnapshotName=$disk.Name+(Get-Date -Format "yyyy-MM-dd");
$snapshot = New-AzureRmSnapshot -Snapshot $snapshotconfig -SnapshotName $SnapshotName -ResourceGroupName $disk.ResourceGroupName;


}
}
}

$dte = Get-Date;
$dte = $dte.AddDays(-7);
$snapshots=Get-AzureRmSnapshot -ResourceGroupName "MyResourceGroup" | Where-Object {$_.Timestamp -gt $dte}
foreach($snapshot in $snapshots)
{
Remove-AzureRmSnapshot -ResourceGroupName "MyResourceGroup" -SnapshotName $snapshot.Name -Force;
}

0 comments on commit 8479a4c

Please sign in to comment.