-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzVmSnapShots.ps1
76 lines (58 loc) · 2.56 KB
/
AzVmSnapShots.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
<#
Création de Snapshots pour les disques de la VM ($VMtoSave) et en conserver un nombre spécifique ($NumberToKeep) selon le type($RetentionType).
Pour utilisation avec Azure Automation Account
$RentionType
J = Journalier (Daily)
H = Hebdomadaire (Weekly)
M = Mensuel (Monthly)
A = Annuel (Yearly)
#>
param (
[Parameter(Mandatory)] [string] $VmToSave,
[Parameter(Mandatory)] [string] $VmResourceGroup,
[Parameter(Mandatory)] [string] $RetentionType,
[Parameter(Mandatory)] [Int16] $NumberToKeep,
[Parameter(Mandatory)] [string] $ResourceGroupStore
)
$SubscriptionID = '' # ID de la subscription
Import-Module Az.Accounts
Import-Module Az.Resources
Import-Module Az.Automation
Import-Module Az.Monitor
Import-Module Az.Compute
Disable-AzContextAutosave -Scope Process
$AzureContext = (Connect-AzAccount -Identity -Subscription $SubscriptionID).context
# Validation
try {
Get-AzResourceGroup -ResourceGroupName $VmResourceGroup -ErrorAction Continue
}
catch { Write-Output "VM Resource Group invalide!";Return}
try {
Get-AzResourceGroup -ResourceGroupName $ResourceGroupStore -ErrorAction Continue
}
catch { Write-Output "Store Resource Group invalide!";Return}
try {
Get-AzVM -Name $VmToSave -ResourceGroupName $VmResourceGroup -ErrorAction Continue
}
catch { Write-Output "Nom de VM invalide!";Return}
if ($RetentionType -notin ("J","H","M","A")) { Write-Output "Type invalide!" ; Return}
# Traitement
$SnapshotPrefix = (Get-Date -DisplayHint Date -format "yyyMMddhhmm_")+$RetentionType+"_"+$VmToSave+"_"
$Tags = @{"Created by"="Automation Account"; "ms-resource-usage"=$VmToSave}
$Disks = (Get-AzDisk | Where-Object DiskState -Contains 'Attached')
$NumberOfDisks = 0
ForEach($_ in $Disks){if ($_.Managedby.Split('/')[8] -Contains $VmToSave)
{
$NumberOfDisks++
$SnapShotConfig = New-AzSnapShotConfig -SourceResourceID $_.ID -Location $_.Location -CreateOption Copy
New-AzSnapShot -ResourceGroupName $ResourceGroupStore -SnapShotName ($SnapshotPrefix+$NumberOfDisks) -SnapShot $SnapShotConfig
}
}
$NumberOfDisks = $NumberOfDisks * $NumberToKeep
$Patern='*_'+$RetentionType+'_'+$VmToSave+'*'
$SnapshotInventory = ((Get-AzSnapshot -ResourceGroupName $ResourceGroupStore | Where-Object Name -like $Patern) | Sort-Object Name -Descending)
$index = 0
For($index = $NumberOfDisks ; $index -LT $SnapshotInventory.Count; $index++)
{
Remove-AzSnapShot -ResourceGroupName $ResourceGroupStore -SnapshotName $SnapshotInventory[$index].Name -Force -Confirm:$False
}