Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.26k stars 3.86k forks source link

New-AzDatalakeGen2Item is ambiguous when file already exists #15382

Open waltervos opened 3 years ago

waltervos commented 3 years ago

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

$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

# 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

# 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     : {}
ghost commented 3 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @sumantmehtams.

Issue Details
## 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 : {} ```
Author: waltervos
Assignees: -
Labels: `Data Lake Storage Gen2`, `Service Attention`, `customer-reported`, `needs-triage`, `question`
Milestone: -