param
(
[
Parameter
(
Mandatory
=
$true
)]
[string]
$storageAccount
,
[
Parameter
(
Mandatory
=
$true
)]
[string]
$storageKey
,
[
Parameter
(
Mandatory
=
$true
)]
[string]
$blobContainer
)
$storageAssemblyPath
=
$pwd
.Path +
"Microsoft.WindowsAzure.Storage.dll"
$restoreLeaseId
=
"BAC2BAC2BAC2BAC2BAC2BAC2BAC2BAC2"
$bytes
=
[System.IO.File]
::ReadAllBytes(
$storageAssemblyPath
)
[System.Reflection.Assembly]
::Load(
$bytes
)
$cred
=
New-Object
'Microsoft.WindowsAzure.Storage.Auth.StorageCredentials'
$storageAccount
,
$storageKey
$client
=
New-Object
'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'
"https://$storageAccount.blob.core.windows.net"
,
$cred
$container
=
$client
.GetContainerReference(
$blobContainer
)
$allBlobs
=
$container
.ListBlobs(
$null
,1)
$lockedBlobs
= @()
foreach
(
$blob
in
$allBlobs
)
{
$blobProperties
=
$blob
.Properties
if
(
$blobProperties
.LeaseStatus
-eq
"Locked"
)
{
$lockedBlobs
+=
$blob
}
}
if
(
$lockedBlobs
.Count
-eq
0)
{
Write-Host
" There are no blobs with locked lease status"
}
if
(
$lockedBlobs
.Count
-gt
0)
{
write-host
"Breaking leases"
foreach
(
$blob
in
$lockedBlobs
)
{
try
{
$blob
.AcquireLease(
$null
,
$restoreLeaseId
,
$null
,
$null
,
$null
)
Write-Host
"The lease on $($blob.Uri) is a restore lease"
}
catch
[Microsoft.WindowsAzure.Storage.StorageException]
{
if
(
$_
.Exception.RequestInformation.HttpStatusCode
-eq
409)
{
Write-Host
"The lease on $($blob.Uri) is not a restore lease"
}
}
Write-Host
"Breaking lease on $($blob.Uri)"
$blob
.BreakLease($(
New-TimeSpan
),
$null
,
$null
,
$null
) |
Out-Null
}
}