如何判断 Azure Remove-AzureStorageShare 何时在 PowerShell 中完成

How to Tell When Azure Remove-AzureStorageShare Is Done In PowerShell

我正在尝试执行计划操作,我在 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.

感谢您的提问。
我想我们可以使用这个 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:

如果您还有疑问,欢迎回到这里 post。谢谢