Sunday, 22 March 2015

Recover Azure VM password (Reset Azure VM password)

If you find you've changed your password on your Azure Virtual Machine, but then subsequently forgotten it (and I swear I was entering the correct password, but apparently not), then you google phrases like "recover azure vm password" etc, you may run across blog posts from Microsoft like the one found here.

There are two things to note which are not entirely obvious from the steps listed there.  One is, if you don't use PowerShell every day you may launch the PowerShell command prompt and not the ISE as they suggest.  You do in fact want the ISE, not the command prompt.  For example on a Windows 2008 server machine the ISE is under Accessories... Windows PowerShell... Windows PowerShell ISE.

The other thing is, if you're trying to reset the password of your VM, the VM actually has to be powered up.  (Duh!)  In retrospect this is obvious; how can you reset the password of your VM if the VM is not actually powered up?  You can't.

But if it is powered up (which you do by going to the Azure management portal, going to "All Items", clicking on the VM in question, and clicking the "Start" button), wait for it to power up (I find I have to refresh the page to actually see the status), then go through the steps in those MS blog posts, then you'll see different behaviour in the PowerShell ISE.  When you get to the last step, to run the snippet that actually resets the machine, you'll be given a list of VMs and you can pick the one you want.  I actually only had one VM so that's the one I picked.  But the behaviour obviously makes much more sense given that the VM is now powered up and able to receive the PowerShell commands that remotely cause its password to be reset for the specified account.

Finally one more thing:  there's a point where they tell you to issue this command:

Select-AzureSubscription –Default $subscription

I actually got an error at that point; PowerShell was complaining about the parameters.  I found that if I simply did not enter the "-Default" parameter it still worked.  In other words I typed

Select-AzureSubscription $subscription

Anyway the procedure did ultimately work and allowed me to reset my password.  It was a relief to find I could still access my VM and didn't have to start from scratch rebuilding it.  (Incidentally if you're not that far along building your VM, you can simply delete the VM and create a new one, but obviously after a certain investment of time that option becomes less attractive).

Update 2015-09-14:  you can now use the windows azure portal user interface to reset your password.  This appears to work well, albeit slowly.





Thursday, 19 March 2015

Elmah + ASP.NET WebAPI tip...

I had one of those situations today where you're looking at various blog posts and stack overflow posts and everybody's essentially saying, "it's easy, you just add the NuGet package, issue a couple lines of configuration code, and it works."  But I was scratching my head for awhile until I connected the dots.  So I thought I'd post a small tip here in hopes of saving somebody a minute or two.

So, if you want to add ELMAH to your ASP.NET WebAPI project to log unhandled exceptions, a bit of googling will quickly lead you to the elmah-contrib-webapi project, and helpful posts such as this one and this one, as well as various stack overflow posts.  I tried the stuff recommended there but for some reason couldn't get it working.  Having used ELMAH previously on an MVC project, I knew that the basic steps are:


  • Install a NuGet package
  • Tweak your web.config
  • Point to where you want to log to (on my previous project it was logging to SQL Server, but in this case the client wanted to log to XML files).
If you do all that, then what you get is, when an unhandled exception occurs (which you can simulate by simply throwing a new Exception in C# code), the error gets logged, and you get a handy web page at the URL http://yourwebsite/elmah.axd).

So here's what wasn't entirely obvious about the Elmah Contrib WebAPI instructions in those various posts:  if your web API happens to still live in the context of an MVC site, as is the case on this project, then I found to get the desired behaviour, I actually had to install TWO NuGet packages, one for the web API, and one for the MVC site.  (In our VS solution, the WebApi functionality happens to reside in the context of an MVC project; it's an AngularJS website, but the MVC routing is simply used to go to the index.html page for the single page app).

Anyway, so here's what I did that made it work:

  • Installed the Elmah.Contrib.WebApi package using the NuGet package manager, as described here.  (I also modified the Application_Start method as shown on that same web page).
  • Installed the Elmah.MVC package as described here.
  • Added the following line of code to the WebApiConfig class as described here.

config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());

  • Modified the web.config to configure Elmah as desired, as described here.  (In my case I used the XML logging).
  • Then, to test, I added a "throw new Exception( )" statement to a method in a WebApi controller.
  • Then, when you're browsing to your WebApi project (say you push the "go" button in VisualStudio to debug, then when the web page comes up, you add "/elmah" onto the end of the URL in the address bar of the browser; at this point Elmah should be working, and you should see the Elmah page.
  • Then I pointed the Angular site to a page where, upon pushing the submit button, it would call that method and throw the exception.
  • Now, you should see the exceptions being displayed on the Elmah page, and (in my case) the XML files that are being written to disk.
Hope this helps someone.  
cheers, dB