【问题标题】:How to Tell When Azure Remove-AzureStorageShare Is Done In PowerShell如何判断 Azure Remove-AzureStorageShare 何时在 PowerShell 中完成
【发布时间】:2017-02-16 22:02:05
【问题描述】:

我正在尝试执行计划的操作,我在 Azure 中创建文件共享并在其中复制一些文件。在我开始这样做之前,如果之前的运行没有清理之前的作业,我想清理它。为此,我找到了方便的 Remove-AzureStorageShare 方法。我的问题是调用此方法后,Azure 有时需要长达 2 分钟才能完成任务。我在 PowerShell 中等待,但我无法在不引发异常的情况下检查 Azure 的共享,然后继续。所以基本上,我希望发生以下操作:

1] 检查 Azure 中的共享,如果存在则删除
2] Azure 完成删除后,重新创建它
3]将我的文件复制到新的共享中

这是我所拥有的,但它不起作用:

Write-Host "STEP  6 : Removing existing Azure Share...";  
# THIS NEXT LINE THROWS AN ERROR IF THE SHARE DOESN'T EXIST
If ((Get-AzureStorageShare -Name $azureShareName -Context $context) {
    Remove-AzureStorageShare 
       -Context $context 
       -Name $azureShareName 
       -Force 
       -ErrorAction SilentlyContinue | Out-Null  
}  

$removed = $false;

While(!$removed) {
    Try {
        # THIS LINE SHOULD THROW AN EXCEPTION SINCE IT'S BEING DELETED
        If ((Get-AzureStorageShare -Name $azureShareName -Context $context) -eq $null) {   
            $removed = $true;
        }
    }
    Catch
    {
        # SINCE THE EXCEPTION IS THROWN, WE WILL SLEEP FOR A FEW...
        Write-Host "STEP 6a : Waiting...still removing.";
        Start-Sleep -s 10;
    }
}

当我尝试再次创建共享时,我收到以下错误:

New-AzureStorageShare : The remote server returned an error: (409) Conflict. HTTP Status Code: 409 - HTTP Error Message: The specified share is being deleted. Try operation later.

【问题讨论】:

    标签: azure-blob-storage azure-powershell


    【解决方案1】:

    感谢您的提问。
    我想我们可以使用这个 PowerShell 来检查 Azure 中的共享:

    Write-Host "STEP  6 : Removing existing Azure Share...";  
    # THIS NEXT LINE THROWS AN ERROR IF THE SHARE DOESN'T EXIST
    If ((Get-AzureStorageShare -Name $azureShareName -Context $ctx)) {
    Remove-AzureStorageShare `
          -Context $ctx `
          -Name $azureShareName `
          -Force `
          -ErrorAction SilentlyContinue | Out-Null  
        }      
    $removed = $false;
    While(!$removed) {
    Try {
         # THIS LINE SHOULD THROW AN EXCEPTION SINCE IT'S BEING DELETED
        If ((Get-AzureStorageShare -Name $azureShareName -Context $ctx) -eq $null) {   
                    $removed = $true;                             
            }
    
         else {
              Write-Host "STEP 6a : Waiting...still removing.";
              Start-Sleep -s 5;
               }
       }
    Catch
                       {
                # SINCE THE EXCEPTION IS THROWN, WE WILL SLEEP FOR A FEW...
                Write-Host "STEP 6b : Waiting...still removing.";
                Start-Sleep -s 5;
            }
        }
    

    如果共享现有的 PowerShell 将显示:等待...仍在删除,否则 PowerShell 将显示错误 404:

    如果您仍有疑问,欢迎在此处回复。谢谢。

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2012-12-04
      相关资源
      最近更新 更多