【问题标题】:Powershell script to download file with current date as filename from Azure blob用于从 Azure Blob 下载当前日期为文件名的文件的 Powershell 脚本
【发布时间】:2022-01-21 21:08:13
【问题描述】:

我需要 Powershell 脚本从 Azure blob 下载当前日期为文件名的文件,例如

app_09102021.txt
app_10102021.txt
app_11102021.txt

如果我在 2021 年 11 月 10 日运行脚本,文件 app_11102021.txt 应该会被下载。你能帮忙吗?

我尝试了下面的代码,但在第 5 行 $latestblob 出现错误

$container_name = 'XXXX'
$destination_path = 'D:\test\trial'
$Ctx = New-AzStorageContext $XXXX -StorageAccountKey $YYYYYYY
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#$latestBlob = Get-AzStorageBlob  -context $Ctx -Container trial|@{$app_$((Get-Date).tostring("yyyyMMdd")).txt}
# Just download that blob
Get-AzStorageBlobContent -Context $Ctx -Container $container_name -Blob $latestBlob.Name -Destination $destination_path 

【问题讨论】:

  • 只需从今天的日期构造文件名,如$latestBlob = 'app_{0:ddMMyyyy}.txt' -f (Get-Date),然后用作-Blob $latestBlob
  • 嗨,非常感谢,它有效。惊人的。此外,我必须下载日志文件并在下载后从 blob 中删除该文件。需要帮助,请
  • 所以..这是一个新问题..
  • 为此提出了新问题,请帮忙。 stackoverflow.com/q/70422675/17721455
  • 如果我们想要匹配任何名称的文件,例如'app_21122021,bgv_21122021,cxb_21122021,cxb_22122021',所以在运行脚本时它必须下载所有'app_21122021,bgv_21122021,cxb_21122022日期后面的名字。 @Theo

标签: azure-powershell


【解决方案1】:

我需要 Powershell 脚本来下载当前日期为的文件 Azure blob 中的文件名

为此,我们可以按照@Theo 的建议使用以下cmd

$container_name = 'test'
$destination_path = 'C:\Users\Desktop\test'
$Ctx = New-AzStorageContext 'name' -StorageAccountKey 'accesskey='
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$latestBlob = '*_{0:ddMMyyyy}.txt' -f (Get-Date) 

$bloblist = Get-AzStorageBlob -Container $container_name -Context $Ctx -Blob $latestBlob

在我的例子中,存储 blob 包含 3 个文件:

这是今天当前日期的输出:

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2021-09-22
    • 2017-08-20
    • 2019-12-10
    • 2022-01-02
    • 2022-08-05
    • 2021-01-04
    • 2020-01-01
    相关资源
    最近更新 更多