Windows Azure PowerShell: The remote server returned an unexpected response: (400) Bad Request

A few days ago, I was using Windows Azure PowserShell cmdlets to create a new virtual machine, and I got an error that didn’t provide the exception details. I requested some help internally and could eventually have additional information on my exception. Here what I did. Il y a quelques jours, j’étais en train d’utiliser les cmdlets PowerShell Windows Azure pour créer une nouvelle machine virtuelle, et j’ai eu une erreur qui ne donnait pas les détails de l’exception. J’ai demandé de l’aide en interne et ai finalement pu obtenir le détail de l’exception. Voici ce que j’ai fait.
The script and the exception I had were the following: Le script et l’exception que j’avais étaient les suivants:


PS C:\> $advm1 = New-AzureVMConfig -Name $vmName -InstanceSize Small -ImageName $imgName |
>> Add-AzureProvisioningConfig -WindowsDomain -Password $adminPassword –Domain $myDomain `
>> -DomainPassword $domainPassword -DomainUserName $domainUser -JoinDomain $FQDomainName |
>> Set-AzureSubnet -SubnetNames $subNet
PS C:\>
PS C:\> New-AzureVM -VNetName $myVNET -ServiceName $serviceName -VMs $advm1
New-AzureVM : The remote server returned an unexpected response: (400) Bad Request.
At line:1 char:1
+ New-AzureVM -VNetName $myVNET -ServiceName $serviceName -VMs $advm1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureVM], ProtocolException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Management.ServiceManagement.IaaS.PersistentVMs.NewAzureVMCommand


The help I got internally stated that $Error[0].Exception was my ProtocolException and I also had some sample C# code that showed how to get the response stream that contains the interesting exception message. On m’a indiqué en interne que ma ProtocolException se trouvait être $Error[0].Exception et on m’a également fourni du code C# qui montrait comment obtenir le flux qui contenait le message intéressant de l’exception.
So I did the following: Ainsi, j’ai procédé comme suit:


PS C:\> $sr = new-object System.IO.StreamReader($Error[0].Exception.InnerException.Response.GetResponseStream())
PS C:\> $txt = $sr.ReadToEnd()
PS C:\> $txt
<Error xmlns="" xmlns:i=""><Code>BadR
equest</Code><Message>The name is not a valid storage account name.</Message></Error>
PS C:\> $sr.Close()

So I could add the “MediaLocation“ to my “New-AzureVMConfig” to have my virtual machine created. J’ai pu ensuite ajouter le paramètre “MediaLocation” à ma “New-AzureVMConfig” et ai ainsi pu créer ma machine virtuelle.