Giter Site home page Giter Site logo

azure-samples / storage-blobs-powershell-break-locked-lease Goto Github PK

View Code? Open in Web Editor NEW
3.0 18.0 4.0 79 KB

This sample demonstrates how to break the locked lease of blob storage by ARM in Microsoft Azure

Home Page: https://gallery.technet.microsoft.com/How-to-break-the-locked-d01ba283

PowerShell 100.00%

storage-blobs-powershell-break-locked-lease's Introduction

services platforms author
storage
powershell
msonecode

How to break the locked lease of blob storage by ARM in Microsoft Azure (PowerShell)

Introduction

This PowerShell script sample shows how to break the locked lease of blob storage by ARM resources in Microsoft Azure.

For the script of ASM version:
How to break the locked lease of blob storage by ASM in Microsoft Azure (PowerShell)

Prerequisites

  • Windows PowerShell 3.0
  • Windows Azure PowerShell

Scenarios

Microsoft Azure provides functionality of acquiring lock-on blobs to avoid concurrent writing to blobs. Yet in some cases, if the backup fails due to the prolonged or sustained network connectivity failure, the backup process may not be able to gain access to the blob and the blob may remain orphaned. This means that the blob cannot be written to or to be deleted until the lease has been released. Under this condition, you might need to break the lease on a blob, and this script can help you with that.

Import and how to use

  • Run the script in the Windows PowerShell Console, type the command: Import-Module <Script Path> at the prompt. For example, type Import-Module C:\Scripts\BreakBlobLease.psm1
  • If you want to get a list of all cmdlet help topics, type the command Get-Help BreakBlobLease -Full to display the entire help file for this function, such as the syntax, parameters, or examples. This is shown in the following image.

Examples

Example 1: Type BreakBlobLease -StorageAccountName "franktanblobstorage" -ContainerName "bootdiagnostics-vmfrtawin-ea191696-c935-4fa2-8b49-0802a2851187" -BlobName "vm-frta-win10.ea191696-c935-4fa2-8b49-0802a2851187.screenshot.bmp" command in the Windows PowerShell Console.

Script

Login-AzureRmAccount -ErrorAction Stop 
         $storageAccounts = Get-AzureRmStorageAccount -ErrorAction Stop 
 
         $selectedStorageAccount = $storageAccounts | where-object{$_.StorageAccountName -eq $StorageAccountName} 
         If($selectedStorageAccount) 
         { 
            $key = (Get-AzureRmStorageAccountKey -ResourceGroupName $selectedStorageAccount.ResourceGroupName -name $selectedStorageAccount.StorageAccountName -ErrorAction Stop)[0].value 
            $storageContext = New-AzureStorageContext -StorageAccountName $selectedStorageAccount.StorageAccountName -StorageAccountKey $key -ErrorAction Stop 
            $storageContainer = Get-AzureStorageContainer -Context $storageContext -Name $ContainerName -ErrorAction Stop 
            $blob = Get-AzureStorageBlob -Context $storageContext -Container  $ContainerName -Blob $BlobName -ErrorAction Stop          
            $leaseStatus = $blob.ICloudBlob.Properties.LeaseStatus; 
            If($leaseStatus -eq "Locked") 
            { 
                 $blob.ICloudBlob.BreakLease() 
                 Write-Host "Successfully broken lease on '$BlobName' blob." 
            } 
            Else 
            { 
                #$blob.ICloudBlob.AcquireLease($null, $null, $null, $null, $null) 
                Write-Host "The '$BlobName' blob's lease status is unlocked." 
            } 
         } 
         Else  
         { 
             Write-Warning  Write-Warning "Cannot find storage account '$StorageAccountName' because it does not exist. Please make sure thar the name of storage is correct." 
         }

storage-blobs-powershell-break-locked-lease's People

Contributors

aliceqin12 avatar msonecode avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

storage-blobs-powershell-break-locked-lease's Issues

I have an error with this script

BreakBlobLease : Cannot validate argument on parameter 'StorageAccountKey'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.At line:1 char:1

  • BreakBlobLease -StorageAccountName "azvleprd1109sa01" -ContainerName "vhds" -Blo ...
  •   + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,BreakBlobLease
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.