Friday 8 May 2020

Set-AzStorageBlobContent and Illegal characters in path

Occasionally I upload some content to an Azure Blob storage account for long term archival from a Windows 2016 server using a script (upload-azure.ps1) and the AZ PowerShell modules, specifically Set-AzStorageBlobContent.
When I last ran this 4 months ago, back in Jan 2020, it went fine. I got the error below a month or so ago and just used the web interface to upload instead. Today, when I have lots of content to upload it was time to get the script working. This is what was getting:

Set-AzStorageBlobContent : Failed to open file C:\Upload\Al.7z: Illegal characters in path..
At C:\Upload\upload-azure.ps1:95 char:13
+             Set-AzStorageBlobContent -File $FilePath -Container $cont ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzStorageBlobContent], TransferException
    + FullyQualifiedErrorId : TransferException,Microsoft.WindowsAzure.Commands.Storage.Blob.SetAzureBlobContentCommand
The relevant line 95 from the script is:

Set-AzStorageBlobContent -File $FilePath -Container $containerName -Context $storageAccount.Context -Blob $file.Name -Metadata $Metadata
I updated the Az.Storage PowerShell modules (see the fun and games here) to v1.14.0 and tried again. Same issue. However, it turns out there has been some path handling changes in .Net Framework that turned up on my server of recent. This article hints to it. Checking the basics of path handling with this below points to something more in .Net than in the Az commands:

PS C:\Users\Al> [System.IO.Path]::GetFullPath("\\?\c:\pagefile.sys")
Exception calling "GetFullPath" with "1" argument(s): "Illegal characters in path."
At line:1 char:1
+ [System.IO.Path]::GetFullPath("\\?\c:\pagefile.sys")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException
Then reading this again,, it now has a solution on it that easy to apply - thanks fimcle!

$registryPath = "HKLM:\SOFTWARE\Microsoft.NETFramework\AppContext"
New-Item -Path $registryPath
New-ItemProperty -Path $registryPath -Name "Switch.System.IO.UseLegacyPathHandling" -Value "false"
Run that and open a new PowerShell window and hazaah, I can now upload content to my Blob storage again.

No comments:

Post a Comment