return $r; } /** * @param int $page 页数 * @param int $pagesize 每页显示数量 * @return mixed */ function link_find($page = 1, $pagesize = 100) { $arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize); return $arr; } /** * @param $id * @return bool 返回FALSE失败 TRUE成功 */ function link_delete($id) { if (empty($id)) return FALSE; $r = link__delete(array('id' => $id)); link_delete_cache(); return $r; } //--------------------------kv + cache-------------------------- /** * @return mixed 返回全部友情链接 */ function link_get($page = 1, $pagesize = 100) { $g_link = website_get('friends_link'); if (empty($g_link)) { $g_link = link_find($page, $pagesize); $g_link AND website_set('friends_link', $g_link); } return $g_link; } // delete kv and cache function link_delete_cache() { website_set('friends_link', ''); return TRUE; } ?>powershell - How To CopyDownload LATEST FILE to Azure Storage Blob To Azure Windows Virtual Machine - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - How To CopyDownload LATEST FILE to Azure Storage Blob To Azure Windows Virtual Machine - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

To 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论