admin管理员组文章数量:1406139
I’m working on a script to download files from Azure Blob Storage using Managed Identity for authentication. Here’s the part of the script that works for me so far:
Connect-AzAccount -identity
$responseID = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=/' `
-Headers @{Metadata="true"}
$val = $response.Content | ConvertFrom-Json
$access_token = $content.access_token
$Path = "C:\Venkat"
$url = ".txt"
$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$RequestHeader.Add("Authorization", "Bearer $access_token")
$RequestHeader.Add("x-ms-version", "2019-02-02")
$result = Invoke-WebRequest -Uri $url -Headers $RequestHeader
$result.content
$output = $result.content
Invoke-WebRequest -Headers $header -Uri $url -OutFile "C:\Venkat\Test.txt" -PassThru
The above code uses Managed Identity to authenticate, and I created a virtual machine and assigned the necessary RBAC role for my Storage Account via the Azure Portal. This downloads the file that is stated.
My Issue: Now, I want to always retrieve the latest file from the Blob Storage container rather than specifying the file name (Test.txt) explicitly. How can I modify this script to dynamically download always the latest file from the container?
I’d greatly appreciate any guidance or solutions. Thanks in advance!
I’m working on a script to download files from Azure Blob Storage using Managed Identity for authentication. Here’s the part of the script that works for me so far:
Connect-AzAccount -identity
$responseID = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://storage.azure/' `
-Headers @{Metadata="true"}
$val = $response.Content | ConvertFrom-Json
$access_token = $content.access_token
$Path = "C:\Venkat"
$url = "https://Storageaccount.blob.core.windows/testcontainer/Test.txt"
$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$RequestHeader.Add("Authorization", "Bearer $access_token")
$RequestHeader.Add("x-ms-version", "2019-02-02")
$result = Invoke-WebRequest -Uri $url -Headers $RequestHeader
$result.content
$output = $result.content
Invoke-WebRequest -Headers $header -Uri $url -OutFile "C:\Venkat\Test.txt" -PassThru
The above code uses Managed Identity to authenticate, and I created a virtual machine and assigned the necessary RBAC role for my Storage Account via the Azure Portal. This downloads the file that is stated.
My Issue: Now, I want to always retrieve the latest file from the Blob Storage container rather than specifying the file name (Test.txt) explicitly. How can I modify this script to dynamically download always the latest file from the container?
I’d greatly appreciate any guidance or solutions. Thanks in advance!
Share Improve this question asked Mar 6 at 11:28 Michael BrownMichael Brown 375 bronze badges 2- This is the error I get - the containername is correct! Invoke-WebRequest : ContainerNotFoundThe specified container does not exist. Time:2025-03-06T14:48:57.2854175Z At line:26 char:13 + $blobList = Invoke-WebRequest -Uri $listBlobsUrl -Headers $RequestHea ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand – Michael Brown Commented Mar 6 at 14:50
- Could you please screenshot of container name from portal? – Venkatesan Commented Mar 7 at 13:23
1 Answer
Reset to default 1To dynamically download the latest file from an Azure Blob Storage container, you can list the blobs in the container, sort them by their last modified date, and then download the most recent one.
# Authenticate using Managed Identity
Connect-AzAccount -identity
# Get the access token
$responseID = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://storage.azure/' -Headers @{Metadata="true"}
$val = $responseID.Content | ConvertFrom-Json
$access_token = $val.access_token
# Define the storage account and container
$storageAccount = "Storageaccount"
$container = "testcontainer"
# List all blobs in the container
$listBlobsUrl = "https://$storageAccount.blob.core.windows/$container?restype=container&comp=list"
$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$RequestHeader.Add("Authorization", "Bearer $access_token")
$RequestHeader.Add("x-ms-version", "2019-02-02")
$blobList = Invoke-WebRequest -Uri $listBlobsUrl -Headers $RequestHeader
$xml = [xml]$blobList.Content
# Find the latest blob
$latestBlob = $xml.EnumerationResults.Blobs.Blob | Sort-Object -Property LastModified -Descending | Select-Object -First 1
# Download the latest blob
$latestBlobName = $latestBlob.Name
$downloadUrl = "https://$storageAccount.blob.core.windows/$container/$latestBlobName"
$outputPath = "C:\Venkat\$latestBlobName"
Invoke-WebRequest -Uri $downloadUrl -Headers $RequestHeader -OutFile $outputPath -PassThru
本文标签:
版权声明:本文标题:powershell - How To CopyDownload LATEST FILE to Azure Storage Blob To Azure Windows Virtual Machine - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744979427a2635723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论