PowerShell to setup Self-Signed SSL on a Website

on Sunday, July 29, 2012

This is pretty much a reprint of http://learn.iis.net/page.aspx/491/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in/

Import-Module WebAdministration

# Setup Certificate Data
$bdate = Get-Date -Format "MM/dd/yyyy"
$edate = ([DateTime]::Now).AddYears(50).ToString("MM/dd/yyyy")

$makecertPath = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
$subject = "CN=www.local.mywebsite.com"

$ipAddress = "127.0.0.7"

# Make the Certificate
cd $makecertPath
./makecert.exe -r -pe -n "$subject" -b $bdate -e $edate -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localMachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

# Get the Certificate for PowerShell
$cert = Get-ChildItem cert:\LocalMachine\My | Where-Object {$_.Subject -eq $subject} | Select-Object -First 1
$thumb = $cert.Thumbprint

# Add Certificate to Website
Set-Location IIS:\SslBindings
Get-Item cert:\LocalMachine\My\$thumb | New-Item $ipAddress!443


Resources:



[1] http://msdn.microsoft.com/en-us/library/bfsktky3.aspx (definition of makecert –sp & –sy)

Commands For Windows Server Core

on Sunday, July 22, 2012

This is a small collection of command line commands that make working with server core easier:

start cmd: open a new command prompt window

taskmgr: the classic command works

notepad: still works, as is always useful

shutdown –l: logoff the server

C:\Program Files\Microsoft Network Monitor 3\netmon.exe: If netmon is installed through the GUI setup, then the installer adds the directory to that path; which makes it even easier to run. But, if you use a command line quiet (/q) install, then you need to type out the whole path.

powershell:

Control VPN Connections Through IIS

on Sunday, July 15, 2012

Using IIS as a service provider, you can call Windows internal Rasdial system to connect, disconnect, and check the status of VPN connections.

Rasdial.exe (http://technet.microsoft.com/en-us/library/ff859533(v=ws.10)) is an internal program which Windows uses to create VPN connections. It’s command line interface can also be used to disconnect a connection or check what connections are currently active. This is a small wrapper website that calls Rasdial, in order to handle VPN connections from a remote location through http.

To set this up, you’ll need an ASP.NET MVC website, and a few changes. This example is using IIS 7.5, ASP.NET MVC 4, on .NET 4.5.

Once the basic ASP.NET MVC website is created, the updates are:

1) Change the Default route:

routes.MapRoute(
    name: "Default",
    url: "{action}/{id}",
    defaults: new { controller = "Vpn", action = "Disconnect", id = UrlParameter.Optional }
);

2) Add a VpnController for the default route to use:


using System;   
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;
using System.Web;
using System.Web.Mvc;
namespace mpfiles.VpnReset.Controllers
{
    public class VpnController : Controller
    {
        private const string DefaultName = "vpn connection name";

        public ActionResult Disconnect(string name = DefaultName)
        {
            var arguments = string.Format("\"{0}\" /d", name);
            return HandleRasDial(arguments);
        }

        public ActionResult Connect(
            string name = DefaultName,
            string username = "username",
            string password = "password"
        ) {
            var arguments = string.Format("\"{0}\" {1} {2}", name, username, password);
            return HandleRasDial(arguments);
        }

        public ActionResult Status(string name = DefaultName)
        {
            return HandleRasDial(string.Empty);
        }

        public ActionResult HandleRasDial(string arguments)
        {
            var startTime = DateTime.Now;
            var message = RasDial(arguments);
            message = message.Replace("\n", "<br />");
            var endTime = DateTime.Now;
            message += "<br />" + String.Format("[{0}]", (endTime - startTime));

            return new ContentResult { Content = message };
        }

        public string RasDial(string arguments)
        {
            var proc = new Process();

            proc.StartInfo.FileName = "rasdial";
            proc.StartInfo.WorkingDirectory = HttpContext.Request.PhysicalApplicationPath;
            proc.StartInfo.Arguments = arguments;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.EnableRaisingEvents = false;

            proc.Start();
            var output = proc.StandardOutput.ReadToEnd();
            output += proc.StandardError.ReadToEnd();
            proc.WaitForExit();
            proc.Dispose();

            return output;
        }

    }
}



I hope someone else will find this useful.


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.