-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSetAzureStorageBlobTierToArchive.ps1
76 lines (49 loc) · 2.35 KB
/
SetAzureStorageBlobTierToArchive.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
Import-Module AzureRM
#Define storage account information
$StorageAccount = "STORAGEACCOUNTNAME"
$StorageAccountKey = "STORAGEACCOUNTKEY"
$containername = "CONTAINERNAME"
#Create a storage context
$context = New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageAccountKey
#Set variables for processing batches & continuation Token
$MaxReturn = 50000
$Token = $Null
#Define a blog array for reporting
$blobarray = @()
#Create a loop to process the whole container in blob batches of 50,000
do
{
#Process a total of 50,000 Blobs at a time. This is extremely useful for large containers
$blobs = Get-AzureStorageBlob -Container $containername -Context $context -MaxCount $MaxReturn -ContinuationToken $Token
#I schedule this script to run every hour, so I've configured the below filter to only process specific blobs. NBLOBS is short for New Blobs!
$nblobs = $blobs | where {$_.LastModified -gt (Get-Date).AddMinutes(-90)} | Where-Object {$_.ICloudBlob.Properties.StandardBlobTier -eq 'Cool'}
# A 'For' loop to process the filtered out blobs
foreach($nblob in $nblobs) {
#Change the access tier of the newly uploaded blogs
$nblob.ICloudBlob.SetStandardBlobTier("Archive")
#Add these blobs to our array
$blobarray += $nblob
}
if($blobs.Length -le 0) { Break;}
$Token = $blobs[$blobs.Count -1].ContinuationToken;
}
While ($Token -ne $Null)
#Export results of changed blogs to CSV file
$timestamp = Get-date -UFormat %d%m%y%H%M
$fulldate = Get-Date -Format g
$export = "C:\temp\Blob Tier Updates - $containername $timestamp.csv"
$blobarray | Select-Object -Property Name, BlobType, LastModified, Length, ContentType, @{n='AccessTier';e={$_.ICloudBlob.Properties.StandardBlobTier}} | Export-Csv $export -NoTypeInformation
#Email CSV file to pre-determined recipients
#Start-Sleep -s 5
#$smtpServer ="8.8.8.8"
#$file = $export
#$att = new-object Net.Mail.Attachment($file)
#$msg = new-object Net.Mail.MailMessage
#$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#$msg.From = "[email protected]"
#$msg.To.Add("[email protected]")
#$msg.Subject = "$timestamp : Azure Blob Storage Updates"
#$msg.Body = "Report attached for Blob Tier Updates on $containername Storage container on $fulldate"
#$msg.Attachments.Add($att)
#$smtp.Send($msg)
#$att.Dispose()