Execute PowerShell through an ASP Website

October 22, 2015 at 3:37 pm in ASP.NET, PowerShell by Florent

I will explain to you How to execute a PowerShell script from a website with parameters. In my example, I will create a website to provide the possibility to a user to start a VM on Azure. You can adapt it depending on your needs.

The final look will be this:

image

Before starting the code creation, you MUST download the publishsettings file from Azure. On a computer where the Azure PowerShell module is installed, execute the following command:

Add-AzureAccount
Select-AzureSubscription –SubscriptionId d9703db7-XXXXXX-XXXX-X-XXXX-XXXX
Get-AzurePublishSettingsFile

SNAGHTML47712f

A web page is opened and the download of the file is starting. The file will finish by .publishsettings.

We will now create the ASP.NET code (for your information, I’m not a developer, so I do this code with my personal knowledge and maybe I have done some code mistake Sourire ). Start by creating a new project in Visual Studio. Select ASP.NET Web Application:

image

Choose the empty template:

image

Create a new folder in the Solution Explorer where the PS script will be stored. Navigate to this folder, add the publishsettings file downloaded before and create a new PowerShell script. Add the following code in it and adapt it with the path of your future IIS website:

Param(
[String]
$VMName
)
Import-Module Azure
Import-AzurePublishSettingsFile “C:\inetpub\Start-AzureVM\Sources\BizSpark-10-22-2015-credentials.publishsettings”
$azureVM = Get-AzureVM | Where-Object {$_.Name -eq $VMName}
Start-AzureVM -Name $azureVM.Name -ServiceName $azureVM.ServiceName

image

Add these 2 files in the explorer of Visual Studio:

image

Now, create a new Web Form page:

image

Use the following code to:

  • Add a Text Box
  • Add a button
  • Add a Label

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”default.aspx.cs” Inherits=”Start_AzureVM._default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Start an Azure VM</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr><td><h1>Start an Azure VM</h1></td></tr>
<tr><td><h3>Provide the name of the VM that you want to start</h3></td></tr>

<tr>
<td>
<asp:TextBox ID=”ServerName” runat=”server” TextMode=”SingleLine”></asp:TextBox>
<asp:Button ID=”ExecuteCode” runat=”server” Text=”Start VM” Width=”200″ OnClick=”ExecuteCode_Click” />
</td>
</tr>

<tr>
<td>
<asp:Label ID=”ConfirmationMessage” runat=”server”></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Open now the CS file. Paste the following code to:

  • Execute the PowerShell script with the VM Name as parameter
  • Write a confirmation message

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Start_AzureVM
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void ExecuteCode_Click(object sender, EventArgs e)
{
var SCRIPT_PATH = @”C:\inetpub\Start-AzureVM\PSScript\Start-AzureVM.ps1″;
Process _Proc = new Process();
_Proc.StartInfo.FileName = “Powershell.exe”;
_Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_Proc.StartInfo.Arguments = @”” + SCRIPT_PATH + ” ‘” + ServerName.Text + “‘ “;
_Proc.Start();

ConfirmationMessage.Text = “The VM ” + ServerName.Text + ” is starting. Wait few minutes before trying to connect to it.”;
ServerName.Text = string.Empty;
}
}
}

Open the Web.config file. Change the langversion parameter to 5:

image

We can now publish our website. I will deploy a new VM in Azure to provide this interface through internet. It’s a simple VM where I will install:

  • IIS
  • Azure PowerShell module
  • Web Deploy 3.5
  • .Net Framework

I created 2 more endpoints for my VM. One for the Web Deploy and one for the HTTP:

image

When the creation is finished, you can deploy Azure Powershell module and Web Deploy 3.5 from the Web Platform Installer:

image

Don’t forget to install the framework that you choose when you created the project. In my case, .NET 4.5.2.

For IIS, select the Web Server feature with the ASP.NET 4.5 Role Services and the Management Service:

image

Check if the port 8172 is open in the firewall:

image

Open IIS Manager. On the IIS Server, select IIS Manager Users. Add a windows account that will have the right to connect through the web deploy:

image

Check the box to allow external communication, apply and start the website:

image

Create a new empty site (Start-AzureVM in my case). Don’t forget to change the port of the default web site if you want to use port 80. Choose an Application pool that has the rights to execute a PowerShell script:

image

image

On the new website, select IIS Manager Permissions and allow a new user:

image

Go back to visual studio. Click on Build > Publish Start-AzureVM:

image

Choose where you want to publish your site:

image

Choose the VM where you want to publish the website:

image

Provide the following information:

  • The server name with the public port of Azure
  • The new site name that we created
  • The username/password with the computer name in front
  • The URL if you want to access it when the wizard is closed (optional)

image

You need to accept the certificate when you click on Validate Connection:

image

If you have the following error:

Could not connect to the remote computer (“iis01flo.cloudapp.net”).
On the remote computer, make sure that Web Deploy is installed and that the required process (“The Web Management Service”) is started.

The remote server returned an error: (404) Not Found.

image

Go to the Control Panel and modify the Microsoft Web Deploy 3.5 software and add all features:

image

If the connection is ok, you will be able to see this:

image

Choose options that you want:

image

When you click on the preview button, you will see that VS will do. In my case it’s adding. If you modify just a file, you will see only the file, with the Action modify:

image

Click on Publish. The upload is done:

image

You can navigate to your site URL and you will be able to see this:

image

Provide the name of a VM to start it:

image

After few seconds, you can see that the VM is in starting mode in the Azure Portal:

image

You can check in the event viewer the command execution:

image

I hope this article will help you Sourire