Connecting Powershell To Azure Account

Hi,

Apart from managing your Azure setup from management portal you can do it through your PowerShell console as well, additionally it helps in automating your task too, so let’s see how to connect your PowerShell to Azure account.

1. How to list cmdlet modules in PowerShell
Windows PowerShell module exist in two states, either they are loaded or are available within your setup but aren’t loaded yet, first we will see how to list down modules which are loaded in your PowerShell, then we will see how to list down all the cmdlet available in your system but not loaded yet.

2. How to list loaded module in PowerShell
You can use the “Get-Module” cmdlet without any parameters to list down loaded module in Powershell

01-Get-Module-All

3. How to list all modules in PowerShell
To obtain list of all modules available in system but might not be loaded in Windows PowerShell console, you can use the same cmdlet “Get-Module” but with additional parameters

02-Get-Module-List-Available

4. If you do not see Azure cmdlets in your system, than you’ll have to install them, let’s see how to do that.

5. The easiest way is to do it from Microsoft Web Platform installer, the web platform installer will not only install the cmdlet, but it will also resolve all the dependencies and install them as well, so that makes it very easier for you.

So open your preferred browser and go to Microsoft Azure Downloads

Once you are in download section of command line tools you can click on install and that should download the web platform installer, rest of the installation of cmdlet then should be done from web platform installer.

03-Webinstaller-from-microsoft

6. Once you have downloaded the web installer and started the deployment the first screen would look like something as below, from there on rest of is just clicking Next button.

04-Web-installer

 

7. Once you have installed the module you can search in PowerShell for Azure module.

05-Get-command-Azure

8. Let’s connect your powershell to Azure
There are couple of ways you can add the Azure account to Windows PowerShell. You can use the “Add-AzureAccount” cmdlet, which uses Azure Active Directory authentication access tokens, or you can do it through “Import-AzurePublishSettingsFile”, which uses a management certificate, which you have to download first.

8.1
The “Add-AzureAccount” cmdlet makes your Azure Account and its subscriptions available in Windows PowerShell. It’s like logging in to your Azure account in Windows PowerShell.
When you run Add-AzureAccount, it displays an interactive windows that prompts you to sign into your Azure account. This sign-in is valid until the access token expires. When it expires, cmdlet that require access to your account prompt you to run Add-AzureAccount again.
I guess the access token TTL is one hour, but that’s just my guess.

06-Add-Azureaccount

As you can see above, you can simply write “Add-AzureAccount” cmdlet in PowerShell console without any option and it will prompt you for your username and then password.

8.2 Verify your Azure Account
Once you have successfully logged in, it would be good to verify your account and ensure you have logged in to correct account, you might have multiple accounts.

To verify your account, you can run following command.

“Get-AzureSubscription”

08-Get-AzureSubscription

8.3 Connect your Powershell to Azure using “Get-AzurePublishingsettingsFile” and “Import-AzurePublishSettingsFile”

With “Get-AzurePublishingSettingsFile” you have to download the publishing profile from Azure management portal and store it as some secure path.

Once you have downloaded the profile you have to than import in PowerShell with “Import-AzurePublishSettingsFile”, the import command would like something as below.

PS C:\Windows\system32> Import-AzurePublishSettingsFile “Path to your file you downloaded from portal”

Note: Ensure to mention your path in double quotes

8.4 Once you have managed to connect PS to Azure, then let’s verify it for last time.

08-Get-AzureSubscription

9. For some reason if you want to remove this subscription from your PowerShell then you can use the following cmdlet to remove.

#Remove-AzureSubscription

The Remove-AzureSubscription cmdlet deletes an Azure subscription from your subscription data file so Windows PowerShell can’t find it. This cmdlet does not delete the subscription from Microsoft Azure, or change the actual subscription in any way.

Required parameter is -SubscriptionName

so the final cmdlet would be

#Remove-AzureSubscription -SubscriptionName “whatever is your subscription name”

If you have any query/question write to hardeepbhamra@gmail.com

Thanks

Hardeep

Remote Desktop Sitecore Azure Instance

Sitecore Azure is built around Windows Azure cloud services (PaaS) and includes automatic deployment of web servers as well as Microsoft SQL databases. So the point to be noted is does Sitecore supports IaaS? Yes Sitecore supports both the flavours of Windows Azure, hence a sitecore solution can be deployed on either PaaS or IaaS. Also Sitecore and Sitecore Azure should be considered as two different entities and that is where it is said that Sitecore Azure Module is built to support (PaaS) and not IaaS.

For comparing SaaS, IaaS and PaaS you might read through my another post here which will give you a fair bit of details about all the three services provided by Windows Azure.

The reason to write this post is How can we enable Remote Desktop Connection(RDC) to a WebRole that is deployed using Sitecore Azure? As Sitecore Azure only supports PaaS which might make us think that we cannot manage IIS or connect to the Virtual Machine(VM) which gets created during sitecore azure deployment. Well I was also under the same thinking that what benefits do sitecore azure comes with, if we cannot look what’s setup has been made behind my sitecore website. I was totally wrong until I figured out a way to connect to the VM.

So let’s see How RDC can be enabled on a webrole instance for a sitecore azure supported website?

RDC can be enabled via Windows Azure Management Portal but considering after a successful deployment of Sitecore.

1) Login to Windows Azure Management Portal

2) Click Cloud Services, click the name of the cloud service (in our case it is SitecoreCloudTestTestSaCe01Role01ScB28), and then click Configure.

Azure Cloud Service

3) Click Remote.

Azure Remote

Warning: All role instances will be restarted when you first enable Remote Desktop and click OK (checkmark). To prevent a reboot, the certificate used to encrypt the password must be installed on the role.

To prevent a restart, install a certificate and then return to this dialog (see Using Remote Desktop with Azure Roles for more information). If you choose an existing certificate, then a configuration update will be sent to all the instances in the role.

4) In Configure Remote Desktop dialog select Role of you sitecore website,

Specify UserName, New Password, Confirm Password, Select Create a new certificate and lastly the date of expiration of this certificate.

Remote Desktop

Once you click , background process will be initiated.

Configuring RDC

5) Windows Azure Portal will notify on successful configuration of RDC.

Success RDC

6) There might be a thought in your mind now what to do after configuring RDC. How can you get a .rdp file to connect to the Remote Session? Read further…

Now click on INSTANCES tab and select CONNECT, this will allow you to download a .rdp file.

Connect EDC

 

RDP Download

7) You now just need to double click and open the .rdp file in order to remotely connect to the VM. Provide UserName and Password.

Login

8) On successful login you can now access the VM.

VM Login
9) You can also check IIS and the sitecore website deployed.

IIS VM

Hope this would have been a nice read for you! Enjoy Sitecore and keep sharing.

 

Bundling And Minification – Part-1

Bundling and minification are two techniques used in ASP.NET to improve request load time which in turn increases performance of a website. These techniques improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.). So let’s see what Bundling and Minification means,

Bundling: It’s a simple logical group of files that could be referenced by unique name and being loaded with one HTTP requestor.

Minification: It’s a process of removing unnecessary whitespace, line break and comments from code to reduce its size thereby improving load times.

Let’s now see how these techniques can be implemented into a website supported by ASP.Net.

Install NuGet Packages

Firstly there are two NuGet packages that are required to be installed in your Visual Studio solution.

InstallNuget WebActivatorEx_Nuget

1) WebActivatorEx

Why do we need WebActivatorEx?

For bundling and minifying we need some code in Startup, even before global.asax’s Application_Start method is executed. WebActivator helps in doing so.

How it is done?
– Attribute the class with WebActivator
– Pass the type of the class
– Pass the name of the method to be executed

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(cssjsbundling.App_Start.BundleConfig), "BundleCSSJs")]

namespace cssjsbundling.App_Start
{
    public class BundleConfig
    {
        public static void BundleCSSJs()
        {

        }
    }
}

Dependency?

WebActivatorEx is dependent on Microsoft.Web.Infrastructure NuGet packages (libraries) so it will also get installed in your project along with WebActivatorEx.

2) Microsoft ASP.NET Web Optimization Framework

Why do we need Microsoft ASP.NET Web Optimization Framework?

It introduces a way to bundle and optimize CSS and JavaScripts files. This is what this post is all about so keep reading further.

How it is done?

Below code speaks it all,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(cssjsbundling.App_Start.BundleConfig), "BundleCSSJs")]
namespace cssjsbundling.App_Start
{
    public class BundleConfig
    {
        public static void BundleCSSJs()
        {
            var bundleTable = BundleTable.Bundles;

            bundleTable.Add(new StyleBundle("~/mysite/styles/css")
               .Include("~/content/styles/jquery.ui.accordion.css",

                "~/content/styles/jquery.ui.autocomplete.css",
                "~/content/styles/Site.css"
               ));

            bundleTable.Add(new ScriptBundle("~/mysite/javascripts")
               .Include("~/Content/scripts/jquery-1.8.2.intellisense.js",
               "~/Content/scripts/jquery-1.8.2.js",
               "~/Content/scripts/jquery-ui-1.8.24.js));
        }
    }
}

Dependency?

Microsoft ASP.NET Web Optimization Framework is dependent on few other NuGet packages (libraries) which in turn are dependent on few more packages, so they all gets installed in your project along with Microsoft ASP.NET Web Optimization Framework. Below is the hierarchy,

  • Microsoft ASP.NET Web Optimization Framework
    • Microsoft.Web.Infrastructure
    • WebGrease
      • Antlr
      • Newtonsoft.Json

Hence once your Nuget Packages are installed your project may look like,

NugetPackages

Note: If you run a project and encounter index out of range error you need to update WebGrease folder to latest version.

Dll that get referenced in a project are,

DLLReferences

Bundling

Before Bundling & Minification a project looks like,

Screen-1

Without Bundling & Minification, view source of a page looks like,

Screen-2

Once the NuGet packages are installed we now need to define a folder App_Start and introduce a class which will include all the definitions of css and js bundles.

Create a Bundle Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(cssjsbundling.App_Start.BundleConfig), "BundleCSSJs")]
namespace cssjsbundling.App_Start
{
    public class BundleConfig
    {
        public static void BundleCSSJs()
        {
            var bundleTable = BundleTable.Bundles;

            bundleTable.Add(new StyleBundle("~/mysite/styles/css")
               .Include("~/content/styles/jquery.ui.accordion.css",

                "~/content/styles/jquery.ui.autocomplete.css",
                "~/content/styles/Site.css"
               ));

            bundleTable.Add(new ScriptBundle("~/mysite/javascripts")               
               .Include("~/Content/scripts/jquery-1.8.2.js));
        }
    }
}

Changes to Web.Config file

Scripts and Styles tags will be enabled in .aspx file only if following setting in web.config file is made


  

      
        
      
    
  

Changes to .ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <%: Scripts.Render("~/mysite/javascripts") %>
    <%: Styles.Render("~/mysite/styles/css") %>
</head>
<body>

After successful Bundling & Minification view source of a page now look like,

BundlingDone

Tips

Minified JS

If you have minified versions of your JS files available do include them in the project with the same name as original JS file and add .min.js at the end of the file.

By default Minification will search for a matching file with .min.js and include that. If not, it creates a minified version by its own.

Bundle() Vs ScriptBundle()

There are two methods to add bundles, Bundle() and ScriptBundle() this might get a thinking what is the difference between them, the former will preserve the license headers (comments) whereas the later will strip out all the comments.

Also Bundle() will not minify .js files as compared to ScriptBundle(), thanks to Andy Cohen (my Development Manager) for updating me about this point.

Using Bundle()

bundleTable.Add(new Bundle("~/mysite/javascripts")              
               .Include("~/Content/scripts/jquery-1.8.2.js",
               "~/Content/scripts/jquery-ui-1.8.24.js));

Bundle-1

Using ScriptBundle()

bundleTable.Add(new ScriptBundle("~/mysite/javascripts")              
               .Include("~/Content/scripts/jquery-1.8.2.js",
               "~/Content/scripts/jquery-ui-1.8.24.js"));

ScriptBundle-1

Relative paths used in CSS

webfontscss

In case your css files contains a relative path which might be either referencing webfonts files or images in that case makes sure to use CssRewriteUrlTransform.

So your bundle code would look like

  bundleTable.Add(new StyleBundle("~/mysite/styles/css")
               .Include("~/content/styles/jquery.ui.accordion.css", new CssRewriteUrlTransform()));

Missing this out will give you 404 for the referenced resources in your css.

Debug Mode

Assume you are working on your dev environment and you want an option to debug your js files individually still take advantage of bundling and minification, interested to know how to do that?

It can be achieved in two ways,

1) In web.config file set debug value to true for compilation tag

debug-true

Debug value = false

debug-false

Alternatively set EnableOptimizations = false in your BundleConfig Class.

BundleTable.EnableOptimizations = false;

EnableOptimization-False

EnableOptimization-True

Nice Reads

WebActivator
Microsoft ASP.NET Web Optimization Framework
CodeProject – ASP.NET Web Optimization Framework
Programming CSS: Bundling and Minification
Bundling and Minification in ASP .NET MVC

That’s it this should get you started with Bundling & Minification.