-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageretention.ps1
54 lines (45 loc) · 2.03 KB
/
storageretention.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
$subscriptionNameArray=@("OSTC Shanghai Dev","OSTC Shanghai Test","OSTC Shanghai PM")
[System.Collections.ArrayList]$vhdNotModifiedList = @()
Foreach($subscriptionName in $subscriptionNameArray)
{
$existingSubscription = Get-AzureSubscription -SubscriptionName $subscriptionName
if($existingSubscription -eq $null)
{
Clear-AzureProfile -Force
Add-AzureAccount
}
Select-AzureSubscription $subscriptionName
$storageAccounts = Get-AzureStorageAccount
Foreach($storageAccount in $storageAccounts)
{
$storagPrimaryKey = (Get-AzureStorageKey -StorageAccountName $storageAccount.StorageAccountName).Primary
$storageContext = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storagPrimaryKey
$storageContainers = Get-AzureStorageContainer -Context $storageContext
Foreach($storageContainer in $storageContainers){
Write-Host($storageContainer)
$storageBlobs = Get-AzureStorageBlob -Container $storageContainer.Name -Context $storageContext
Foreach($storageBlob in $storageBlobs){
$now=Get-Date
$now=$now.ToUniversalTime()
$lastModifiedTime = $storageBlob.LastModified.UtcDateTime
if($storageBlob.Name.EndsWith(".vhd"))
{
if($lastModifiedTime -lt $now.AddDays(-30))
{
Write-Host ("more than 30 days not modified $lastModifiedTime should be deleted")
$vhdNotModifiedList.Add($storageAccount.StorageAccountName+".blob.core.windows.net/" + $storageContainer.Name +"/"+ $storageBlob.Name)
#Write-Host ($storageBlob)
}
}
}
}
}
}
$output=""
$outputFile="storageblobnotmodifiedfor30days.txt"
Foreach($blobToBeDelete in $vhdNotModifiedList)
{
$output+=$blobToBeDelete
$output+="`r`n"
}
Set-Content -Encoding UTF8 -Path $outputFile -Value $output