Blog

9 04, 2015

Automating azure powershell cmdlets

By |April 9th, 2015|Coding|0 Comments

Administering your azure subscription via powershell cmdlets is a great option for automating tasks such as starting and stopping vms, downloading blobs, etc. In the past I used the Azure-AddAccount cmdlet to authenticate. This is great but not the best option for scheduling your powershell scripts as it requires you to input your azure login credentials. The best option for automating your scripts is to authenticate via an X509 certificate. There are a couple steps to accomplish this:

  1. Download and install Azure Powershell cmdlets
  2. Generate  X509 cert (management cert) for Azure
  3. Upload the cer file to Azure
  4. Create authenticate powershell script

Lets get started, shall we?

Download and install Azure Powershell cmdlets

Grab the latest version of Azure Powershell cmdlets from: http://azure.microsoft.com/en-us/downloads/

Generate X509 cert (management cert) for Azure

Fire up Visual Studio 2010 or 2012 command prompt (run as Administrator) and run the following command, change SampleCompany to a name you can recognize:

Upload the cert file to Azure

Login to your azure account by heading to http://manage.windowsazure.com, browse to Settings on the left side, then click on Management Certificates:

2015-04-09_1107

Click on Upload at the button and specify the .cer file you generated in the previous step.

After the certificate has been uploaded, take a note of the thumbprint property, we will use this later.

For more detailed instructions on this step, see this page: https://msdn.microsoft.com/en-us/library/azure/gg551722.aspx

Create authenticate powershell script

Now that we have our generated certificate uploaded to Azure we can go ahead and create a script to authenticate with that certificate. Fire up your favorite text editor and place the following in there:

Replace the value for the $Thumbprint variable from the information we noted in the previous step.

You can get your subscription id and subscription name from the settings page in Azure as well.

Save the file as “Authenticate.ps1”

In a powershell console, browse to the location where you saved Authenticate.ps1 and run the following command:

Now that you have authenticated, you can run any azure powershell cmdlet such as:

See the Azure Cmdlet reference by browsing to: https://msdn.microsoft.com/en-us/library/azure/jj554330.aspx for a full listing of commands you can now run.

If you have any questions, please feel free to contact me.

10 11, 2014

C# extension method to “prettyify” your xml output

By |November 10th, 2014|Coding|0 Comments

Here is simple c# extension method to format your XML data. This is useful for logging xml data to trace files, etc:

23 10, 2014

Get SqlConnection from DbContext

By |October 23rd, 2014|Coding|0 Comments

I’ve been using Entity Framework for sometime now. But we all know using EF for everything may not be the best option. I decided to use Dapper for certain db queries.

Dapper requires an open SqlConnection object, I didn’t want to have to manage another connection string in my web.config so I looked at options for converting an EF connection string to an ADO connection string.

After some research I came up with the following:

6 08, 2014

Creating aliases in git

By |August 6th, 2014|Coding|0 Comments

If you are like me and use git bash, powershell cmd or some other command line utility for git you quickly learn there is a lot of keystrokes to do repetitive tasks. Creating aliases in git is a convenient way to minimize the amount of keystrokes you will have to make. Here are some alias I have setup that I use on a daily basis:

Alias Name Configuration Setup Example Usage Description
git b git config –global alias.b branch git b master Allows you to easily switch branches
git co git config –global alias.co checkout git co new_feature Checkout a new branch
git undo git config –global alias.undo “reset HEAD~1 –mixed” git undo Undo the last commit.

Each time you create an alias you are adding it to one of 3 config files: local, system and global. In the examples above I add it to the global config file. To learn more about these config files take a look here: git-config

24 07, 2014

Opinionated AngularJS styleguide for teams

By |July 24th, 2014|Coding|0 Comments

Excellent article explaining different styles of writing angularjs modules, controllers: http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/

16 07, 2014

Using Azure Powershell to start and stop a VM

By |July 16th, 2014|Coding|0 Comments

Using an Azure VM is a convenient way to test new software without having to corrupt your own terminal or using more resources for a virtual instance using Virtual Box, VMWare, etc.

As you may already know if a VM is stopped there are no charges for computing cost. However you will still incur a charge for the storage used by the VM (which is significantly less). Using a powershell script to manage the state of these VMs are helpful. I have the shutdown powershell script on a scheduler that runs nightly.

In order to use azure powershell you must do the following:

  • Install Azure PowerShell.
  • Connect to your subscription within Azure Powershell

Fortunately Microsoft has documented this process in an article located at: http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell

After installing feel free to try the following code (make sure to replace “MY_COMPUTER” with the name of your VM:

8 07, 2014

Generating a certificate and private key using openssl

By |July 8th, 2014|Coding|0 Comments

Sometimes you need to generate a quick certificate to test your app. Using openssl you can easily accomplish this, take a look at my example below:

Got a question? Send me a message on twitter: @tekguy

5 07, 2014

Logging off WS-Federation by creating a wsignout1.0 message

By |July 5th, 2014|Coding|0 Comments

Recently I integrated my web app with Azure ACS but was having a difficult time signing out of ACS (deleting my cookie off the .accesscontrol.windows.net server). The following code will create a wsignout1.0 message. Essentially it will construct a url with the action parameter set to “wsignout1.0”. An additional parameter “wreply” allows you to specify a url to redirect to after you have been signed out:

Got a question? Send me a message on twitter: @tekguy

30 06, 2014

Using AngularJs with a jQuery background

By |June 30th, 2014|Coding|0 Comments

Most web developers today have experience using jQuery. If you are new to AngularJs and would like to start learning it I would suggest reading this post on Stack Overflow before reviewing any Angular tutorials. It will help clear up a lot of issues when trying to use both (I don’t recommend it).

11 02, 2014

A visual explanation of SQL joins

By |February 11th, 2014|Coding|0 Comments

To get a better understanding of SQL joins take a look at Coding Horror’s venn diagram display: Click here