0
$Context = (Get-AzStorageAccount -ResourceGroupName "TestServices-RG" -Name "devstestsa").Context

$file=Get-AZStorageFile -ShareName "test-file-share" -Context $Context -Path "/test.json"

The above command works but it throws error if file does not exist. Is there any way to suppress this error.

Error: The specified resource does not exist. HTTP Status Code: 404 - HTTP Error Message: The specified resource does not exist. ErrorCode: | ResourceNotFound ErrorMessage: The specified resource does not exist. RequestId: 50521f9b-701a-0034-6a8a-fba24a000000 Time: Fri, 05 Feb 2021 | 12:12:22 GMT

Get-AzStorageFile: untitled:Untitled-1:4:10 Line | 4 | $file=Get-AZStorageFile -ShareName "test-file-share" -Context … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The specified resource does not exist. HTTP Status Code: 404 - HTTP Error Message: The specified resource does not exist. ErrorCode: | ResourceNotFound ErrorMessage: The specified resource does not exist.

suraj_123
  • 115
  • 13
  • you can put it in `try - catch` code block. – Ivan Glasenberg Feb 05 '21 at 07:01
  • 1
    Does using the `-ErrorAction Ignore` common parameter on the Get-AzStorageFile command work for you? [About CommonParameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.1) – Daniel Feb 05 '21 at 07:35

1 Answers1

1

You can try to use the following PowerShell script

$account=Get-AzStorageAccount -ResourceGroupName "" -Name ""

$share=Get-AzStorageShare -Name "share2" -Context $account.Context

$share.ShareClient.GetRootDirectoryClient().GetFileClient("test.json").Exists()

enter image description here enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39