Azure / Azure/azure-powershell

New-AzDatalakeGen2Item is ambiguous when file already exists

Open
#15,382 2 comments 0 reactions 0 assignees View on GitHub
act-codegen-extensibility-squad customer-reported Data Lake Storage Gen2 question Service Attention
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

## Description
Behaviour of New-AzDatalakeGen2Item is unclear when a file with a certain name already exists. I would expect the cmdlet to throw when a file already exists, or to overwrite it (preferably with a switch parameter, -Force could perhaps cover this). Right now, the cmdlet simply has no output in this case, which means in my script I've resorted to using

```powershell
$ExistingItem = Get-AzDatalakeGen2Item -FileSystem $FileSystemName -Path $Destination -Context $Context -ErrorAction:SilentlyContinue
if ($ExistingItem) {
$Removed = $ExistingItem | Remove-AzDatalakeGen2Item -Context $Context -PassThru -Force
if ($False -eq $Removed) {
throw ("Unable to remove existing file {0}." -f $ExistingItem.Path)
}
}

$UploadedItem = New-AzDataLakeGen2Item -FileSystem $FileSystemName -Path $Destination -Source $ChildItem.FullName -Context $Context -Force
```

To, either solution is fine (throw and/or provide -Overwrite switch).

## Steps to reproduce

```powershell
# Upload a file:
$File = New-AzDataLakeGen2Item -FileSystem "testfilesystem" -Path "dir1/dir2/file1" -Source "c:\sourcefile.txt" -Force

$File.GetType()
# Should say: (...)AzureDataLakeGen2Item

$NewFile = New-AzDataLakeGen2Item -FileSystem "testfilesystem" -Path "dir1/dir2/file1" -Source "c:\sourcefile.txt" -Force
$File.GetType()
# Says: You cannot call a method on a null-valued expression
```

## Environment data

```
# PsVersionTable:
Name Value
---- -----
PSVersion 5.1.17763.1490
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.1490
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

```

## Module versions

```powershell
# Az.Storage module:
Name : Az.Storage
Path : C:\Program Files\WindowsPowerShell\Modules\Az.Storage\3.5.0\Az.Storage.psd1
Description : Microsoft Azure PowerShell - Storage service data plane and management cmdlets for Azure Resource
Manager in Windows PowerShell and PowerShell Core. Creates and manages storage accounts in Azure
Resource Manager.

For more information on Storage, please visit the following:
https://docs.microsoft.com/azure/storage/
Guid : dfa9e4ea-1407-446d-9111-79122977ab20
Version : 3.5.0
ModuleBase : C:\Program Files\WindowsPowerShell\Modules\Az.Storage\3.5.0
ModuleType : Script
PrivateData : {PSData}
AccessMode : ReadWrite
ExportedAliases : {[Get-AzStorageContainerAcl, Get-AzStorageContainerAcl], [Start-CopyAzureStorageBlob,
Start-CopyAzureStorageBlob], [Stop-CopyAzureStorageBlob, Stop-CopyAzureStorageBlob],
[Enable-AzStorageSoftDelete, Enable-AzStorageSoftDelete]...}
ExportedCmdlets : {[Get-AzStorageAccount, Get-AzStorageAccount], [Get-AzStorageAccountKey, Get-AzStorageAccountKey],
[New-AzStorageAccount, New-AzStorageAccount], [New-AzStorageAccountKey,
New-AzStorageAccountKey]...}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules : {}

```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the existing-file case with New-AzDataLakeGen2Item using the commands in the issue, then inspect the cmdlet's implementation and related tests. Done means the existing-file behavior is explicit and verifiable: it either reports an error or consistently applies the documented overwrite behavior, including the -Force scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, powershell
Domain
cli, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.