#PSTip Azure cmdlets and proxy authentication

Note: This tip requires PowerShell 2.0 or above.

A few days ago, I was working on setting up Kemp Azure VLM. I will talk about this more in a detailed post later.

So, as part of this process, I had to upload the Kemp Azure VLM VHD to my Azure storage using Azure PowerShell cmdlets. However, my home proxy was not letting me do that and there was no way to provide proxy credentials when using Azure cmdlets. So, essentially, things like Add-AzureVHD will fail with a 407 error.

PS C:\> Add-AzureVhd -LocalFilePath C:\Scripts\LoadMaster-VLM-7.0-3-Azure.vhd -Destination "http://mystorage.blob.core.window
Add-AzureVhd : "An exception occurred when calling the ServiceManagement API. HTTP Status Code: 407. Service
Management Error Code: <NONE>. Message: <NONE>. Operation Tracking ID: <NONE>."
At line:1 char:1
+ Add-AzureVhd -LocalFilePath C:\Scripts\LoadMaster-VLM-7.0-3-Azure.vhd -Destination ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : CloseError: (:) [Add-AzureVhd], ServiceManagementClientException
 + FullyQualifiedErrorId : Microsoft.WindowsAzure.Management.ServiceManagement.StorageServices.AddAzureVhdCommand

That is not a very friendly message but there is an easy way to resolve the problem with a little help of .NET:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

The DefaultCredentials property of System.Net.CredentialCache represents the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application.

PS C:\> Add-AzureVhd -LocalFilePath C:\Scripts\LoadMaster-VLM-7.0-3-Azure.vhd -Destination "http://mystorage.
blob.core.windows.net/kemp/kemp.vhd" -Verbose
MD5 hash is being calculated for the file  C:\Scripts\LoadMaster-VLM-7.0-3-Azure.vhd.
MD5 hash calculation is completed.
Elapsed time for the operation: 00:01:40
Creating new page blob of size 34359738880...
Upload failed with exceptions:
Elapsed time for upload: 00:06:50
Filed in: Azure, Columns, Tips and Tricks Tags: , , ,
© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: