且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PowerShell检查Azure Blob容器中是否已存在Blob

更新时间:2023-11-26 09:27:40

一种解决方案是在try/catch中包装对 Get-AzureStorageBlob 的调用,并捕获 ResourceNotFoundException 确定该blob不存在.

A solution is to wrap the call to Get-AzureStorageBlob in a try/catch and catch ResourceNotFoundException to determine that the blob doesn't exist.

也不要忘记最后的-ErrorAction Stop.

try
{   
    $blob = Get-AzureStorageBlob -Blob $azureBlobName -Container $azureStorageContainer -Context $azureContext -ErrorAction Stop
}
catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException]
{
    # Add logic here to remember that the blob doesn't exist...
    Write-Host "Blob Not Found"
}
catch
{
    # Report any other error
    Write-Error $Error[0].Exception;
}