1

I use the PowerShell command line below to upload a file to a blob storage and overwrite if already exists:

Set-AzureStorageBlobContent -Confirm:$false -Force

It works well.

For another scenario, I need to do opposite and make sure Set-AzureStorageBlobContent does not overwrite blobs if already exist.

I know that I can use the logic explained here:

How to check if a blob already exists in Azure blob container using PowerShell

However, I am hoping that there is a simpler option in PowerShell.

I expect there should be a way to auto-answer "No" to Powershell's -Confirm prompt.

Is there any PowerShell technique that I can use here?

Allan Xu
  • 7,998
  • 11
  • 51
  • 122

1 Answers1

1

The -confirm prompts specific goal is to force a user to respond, or exempt the user from responding.

# Throw prompt
Remove-Item -Path:'D:\Temp\input - Copy.txt' -Confirm:$true

# Don't throw prompt
Remove-Item -Path:'D:\Temp\input - Copy.txt' -Confirm:$false -Verbose

VERBOSE: Performing the operation "Remove File" on target "D:\Temp\input - Copy.txt".

The discussion you point to is prudent. There is no auto answer to many object interactions, you have to do what the discussion highlights when such options don't exist.

Or as just an if single simple if or try..catch statement.

if((Test-Path 'D:\temp\aliases.htm') -eq $true){
    'Do nothing'
}


if((Test-Path 'D:\temp\aliases.htm') -eq $false){
    'do something'
}

What are you considering, simpler than those examples?

postanote
  • 15,138
  • 2
  • 14
  • 25
  • I was hoping to find a way to autoanswer "No" to the prompt. Based on what you said it is not possible. – Allan Xu Oct 20 '18 at 03:08
  • Tha approach you described works well with local disk scenarios. With Cloud storage, your approach imposes one more HTTP REST round trip. – Allan Xu Oct 20 '18 at 03:09
  • Yep on the cloud stuff, but I am not sitting at my location to hit my cloud instance, hence the reason for using a local disk option. – postanote Oct 20 '18 at 03:19
  • Why not just `if (!(test-path c:\file.txt)) {"do something"}` you could follow with an else statement to throw an error or warning etc – Robert Cotterman Oct 20 '18 at 04:19