How to Upload Blob to Azure Storage Account using PowerShell

Jul 6 2022 9:24 AM

Based on the script in the link below:

How to Upload Blob Contents in an Azure Storage Account using PowerShell (c-sharpcorner.com)

can i connect with Service principal  as below ?? instead of just Connect-AzAccount  

## Input Parameters  
    $resourceGroupName="azpractice"  
    $storageAccName="azstorageacc1122020"  
    $storageContainerName="container001"  
    $fileName="testdoc"  
    $filePath=".\testdoc.docx"   
 

# automation scripts stay secure BY USING by using Service principals which are non-interactive Azure accounts
# Use the application ID as the username, and the secret as password

    $credentials = Get-Credential
    Connect-AzAccount -ServicePrincipal -Credential $credentials -Tenant <tenant ID>
 
## Function to upload blob contents  
    Function UploadBlobContent  
    {  
            Write-Host -ForegroundColor Green "Uploading blob content.."  
        ## Get the storage account  
             $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName      
         ## Get the storage account context  
             $ctx=$storageAcc.Context    
         ## Upload a file  
             Set-AzStorageBlobContent -Container $storageContainerName -File $filePath -Blob $fileName -Context $ctx -Force      
    }   
  
UploadBlobContent   
 
## Disconnect from Azure Account  
    Disconnect-AzAccount 


Answers (1)