Skip to main content

IIS Administration using Microsoft.Web.Administration using F#

A friend had mentioned his joy at using Powershell. I guess this is pretty cool and I don't mind Powershell. I sort of missed the boat a little with it because I haven't done any Windows Administration since I used to look after Windows Server 2000 machines (and possibly a couple of 2003). At that time I had a different arsenal to cause untold woe on my fellow colleagues....VBSCRIPT!!!! Boy could I cause trouble with that. With a combination of that, VBA and SQL I used to love creating spider webs of pure madness, once written the apps were tied together so precariously; one false move and the entire thing would explode.... anyway that's a different story.

Back to the Powershell. He was using it to automate IIS (or else I heard what I wanted to so I could try and push F# onto him, who knows?). I have heard various stories of extremely large platform automation scripts being written recently (for example .net rocks interview with Steve Evans) and whilst they seem to be going the right way (code reviews, source control etc....), one thing I don't like about these scripts is their loose typing. One false move, one little misspelling and you are done for. The scripting of infrastructure is business critical so it needs to be treated seriously.

Why not still use scripts but enjoy proper testability, strong typing and a wonderful easy to learn syntax? IT people are perfect candidates to learn F# because they're not tied down with all the ceremony and object orientated nonsense that a normal developer coming from C# suffers from. Also whilst the various Powershell IDE's are ok, they're no match for Visual Studio with the F# Powertools installed.

There are .NET libraries for AWS and Azure and if you are creating resources such as IIS Sites on your infrastructure you can just use Libraries such as Microsoft.Web.Administration for controlling IIS.

Below is an example of creating a live and acceptance site using Microsoft.Web.Administration library. It really is this easy.

I have had a more advanced version of this type of code at work for some time now. I originally made an app to migrate all of our old sites to a new server and automatically create the correct folder structures, sites and application pools.

Available on fssnip http://www.fssnip.net/s1

 1: #r "C:\\git\\bitbucket\\folder-generator\\packages\\Microsoft.Web.Administration.7.0.0.0\\lib\\net20\\Microsoft.Web.Administration.dll"
 2: 
 3: open System
 4: open Microsoft.Web.Administration
 5:                     
 6: let siteName = "doms-wicked-site"
 7: 
 8: let name(siteType: string) = 
 9:     String.Format("{0}_{1}", siteName, siteType)
10:         
11: let serverManager = new ServerManager()
12:         
13: let createSite(siteName: string) = 
14:     serverManager.Sites.Add(siteName, String.Format(@"C:\Sites\{0}", siteName), 80)
15: 
16: let applicationPool(poolName: string) = 
17:     let livePool:ApplicationPool = serverManager.ApplicationPools.Add(poolName)
18:     livePool.Enable32BitAppOnWin64 <- true;
19:     livePool.ManagedRuntimeVersion <- "v4.0";            
20:     livePool
21: 
22: let site(siteName: string, poolName: string) = 
23:     let liveSite:Site = createSite(siteName)
24:     liveSite.ServerAutoStart <- true
25:     liveSite.ApplicationDefaults.ApplicationPoolName <- poolName       
26:     liveSite.TraceFailedRequestsLogging.Enabled <- true;
27:     liveSite.TraceFailedRequestsLogging.Directory <- String.Format(@"C:\IISLogs\{0}", siteName)
28: 
29: let liveName = name("live")
30: let livePool = applicationPool(liveName)            
31: let liveSite = site(liveName, livePool.Name)
32: 
33: let accName = name("acc")
34: let accPool = applicationPool(accName)            
35: site(accName, accPool.Name) |> ignore
36:         
37: serverManager.CommitChanges()   
namespace System
namespace Microsoft
namespace Microsoft.Web
namespace Microsoft.Web.Administration
val siteName : string

Full name: Snippet.siteName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val name : string -> string

Full name: Snippet.name
val siteType : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
type String =
  class
    new : char -> string
    new : char * int * int -> string
    new : System.SByte -> string
    new : System.SByte * int * int -> string
    new : System.SByte * int * int * System.Text.Encoding -> string
    new : char [] * int * int -> string
    new : char [] -> string
    new : char * int -> string
    member Chars : int -> char
    member Clone : unit -> obj
    member CompareTo : obj -> int
    member CompareTo : string -> int
    member Contains : string -> bool
    member CopyTo : int * char [] * int * int -> unit
    member EndsWith : string -> bool
    member EndsWith : string * System.StringComparison -> bool
    member EndsWith : string * bool * System.Globalization.CultureInfo -> bool
    member Equals : obj -> bool
    member Equals : string -> bool
    member Equals : string * System.StringComparison -> bool
    member GetEnumerator : unit -> System.CharEnumerator
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> System.TypeCode
    member IndexOf : char -> int
    member IndexOf : string -> int
    member IndexOf : char * int -> int
    member IndexOf : string * int -> int
    member IndexOf : string * System.StringComparison -> int
    member IndexOf : char * int * int -> int
    member IndexOf : string * int * int -> int
    member IndexOf : string * int * System.StringComparison -> int
    member IndexOf : string * int * int * System.StringComparison -> int
    member IndexOfAny : char [] -> int
    member IndexOfAny : char [] * int -> int
    member IndexOfAny : char [] * int * int -> int
    member Insert : int * string -> string
    member IsNormalized : unit -> bool
    member IsNormalized : System.Text.NormalizationForm -> bool
    member LastIndexOf : char -> int
    member LastIndexOf : string -> int
    member LastIndexOf : char * int -> int
    member LastIndexOf : string * int -> int
    member LastIndexOf : string * System.StringComparison -> int
    member LastIndexOf : char * int * int -> int
    member LastIndexOf : string * int * int -> int
    member LastIndexOf : string * int * System.StringComparison -> int
    member LastIndexOf : string * int * int * System.StringComparison -> int
    member LastIndexOfAny : char [] -> int
    member LastIndexOfAny : char [] * int -> int
    member LastIndexOfAny : char [] * int * int -> int
    member Length : int
    member Normalize : unit -> string
    member Normalize : System.Text.NormalizationForm -> string
    member PadLeft : int -> string
    member PadLeft : int * char -> string
    member PadRight : int -> string
    member PadRight : int * char -> string
    member Remove : int -> string
    member Remove : int * int -> string
    member Replace : char * char -> string
    member Replace : string * string -> string
    member Split : char [] -> string []
    member Split : char [] * int -> string []
    member Split : char [] * System.StringSplitOptions -> string []
    member Split : string [] * System.StringSplitOptions -> string []
    member Split : char [] * int * System.StringSplitOptions -> string []
    member Split : string [] * int * System.StringSplitOptions -> string []
    member StartsWith : string -> bool
    member StartsWith : string * System.StringComparison -> bool
    member StartsWith : string * bool * System.Globalization.CultureInfo -> bool
    member Substring : int -> string
    member Substring : int * int -> string
    member ToCharArray : unit -> char []
    member ToCharArray : int * int -> char []
    member ToLower : unit -> string
    member ToLower : System.Globalization.CultureInfo -> string
    member ToLowerInvariant : unit -> string
    member ToString : unit -> string
    member ToString : System.IFormatProvider -> string
    member ToUpper : unit -> string
    member ToUpper : System.Globalization.CultureInfo -> string
    member ToUpperInvariant : unit -> string
    member Trim : unit -> string
    member Trim : char [] -> string
    member TrimEnd : char [] -> string
    member TrimStart : char [] -> string
    static val Empty : string
    static member Compare : string * string -> int
    static member Compare : string * string * bool -> int
    static member Compare : string * string * System.StringComparison -> int
    static member Compare : string * string * System.Globalization.CultureInfo * System.Globalization.CompareOptions -> int
    static member Compare : string * string * bool * System.Globalization.CultureInfo -> int
    static member Compare : string * int * string * int * int -> int
    static member Compare : string * int * string * int * int * bool -> int
    static member Compare : string * int * string * int * int * System.StringComparison -> int
    static member Compare : string * int * string * int * int * bool * System.Globalization.CultureInfo -> int
    static member Compare : string * int * string * int * int * System.Globalization.CultureInfo * System.Globalization.CompareOptions -> int
    static member CompareOrdinal : string * string -> int
    static member CompareOrdinal : string * int * string * int * int -> int
    static member Concat : obj -> string
    static member Concat : obj [] -> string
    static member Concat<'T> : System.Collections.Generic.IEnumerable<'T> -> string
    static member Concat : System.Collections.Generic.IEnumerable<string> -> string
    static member Concat : string [] -> string
    static member Concat : obj * obj -> string
    static member Concat : string * string -> string
    static member Concat : obj * obj * obj -> string
    static member Concat : string * string * string -> string
    static member Concat : obj * obj * obj * obj -> string
    static member Concat : string * string * string * string -> string
    static member Copy : string -> string
    static member Equals : string * string -> bool
    static member Equals : string * string * System.StringComparison -> bool
    static member Format : string * obj -> string
    static member Format : string * obj [] -> string
    static member Format : string * obj * obj -> string
    static member Format : System.IFormatProvider * string * obj [] -> string
    static member Format : string * obj * obj * obj -> string
    static member Intern : string -> string
    static member IsInterned : string -> string
    static member IsNullOrEmpty : string -> bool
    static member IsNullOrWhiteSpace : string -> bool
    static member Join : string * string [] -> string
    static member Join : string * obj [] -> string
    static member Join<'T> : string * System.Collections.Generic.IEnumerable<'T> -> string
    static member Join : string * System.Collections.Generic.IEnumerable<string> -> string
    static member Join : string * string [] * int * int -> string
  end

Full name: System.String

  type: String
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
String.Format(format: string, args: obj []) : string
String.Format(format: string, arg0: obj) : string
String.Format(provider: IFormatProvider, format: string, args: obj []) : string
String.Format(format: string, arg0: obj, arg1: obj) : string
String.Format(format: string, arg0: obj, arg1: obj, arg2: obj) : string
val serverManager : ServerManager

Full name: Snippet.serverManager

  type: ServerManager
  implements: IDisposable
type ServerManager =
  class
    new : unit -> Microsoft.Web.Administration.ServerManager
    new : bool * string -> Microsoft.Web.Administration.ServerManager
    new : string -> Microsoft.Web.Administration.ServerManager
    member ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
    member ApplicationPoolDefaults : Microsoft.Web.Administration.ApplicationPoolDefaults
    member ApplicationPools : Microsoft.Web.Administration.ApplicationPoolCollection
    member CommitChanges : unit -> unit
    member Dispose : unit -> unit
    member GetAdministrationConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetAdministrationConfiguration : Microsoft.Web.Administration.WebConfigurationMap * string -> Microsoft.Web.Administration.Configuration
    member GetApplicationHostConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetMetadata : string -> obj
    member GetRedirectionConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : string -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : string * string -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : Microsoft.Web.Administration.WebConfigurationMap * string -> Microsoft.Web.Administration.Configuration
    member SetMetadata : string * obj -> unit
    member SiteDefaults : Microsoft.Web.Administration.SiteDefaults
    member Sites : Microsoft.Web.Administration.SiteCollection
    member VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
    member WorkerProcesses : Microsoft.Web.Administration.WorkerProcessCollection
    static member OpenRemote : string -> Microsoft.Web.Administration.ServerManager
  end

Full name: Microsoft.Web.Administration.ServerManager

  type: ServerManager
  implements: IDisposable
val createSite : string -> Site

Full name: Snippet.createSite
val siteName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
property ServerManager.Sites: SiteCollection
ConfigurationElementCollectionBase.Add(element: Site) : Site
SiteCollection.Add(name: string, physicalPath: string, port: int) : Site
SiteCollection.Add(name: string, bindingInformation: string, physicalPath: string, certificateHash: byte []) : Site
SiteCollection.Add(name: string, bindingProtocol: string, bindingInformation: string, physicalPath: string) : Site
val applicationPool : string -> ApplicationPool

Full name: Snippet.applicationPool
val poolName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val livePool : ApplicationPool

  type: ApplicationPool
  inherits: ConfigurationElement
type ApplicationPool =
  class
    inherit Microsoft.Web.Administration.ConfigurationElement
    member AutoStart : bool with get, set
    member Cpu : Microsoft.Web.Administration.ApplicationPoolCpu
    member Enable32BitAppOnWin64 : bool with get, set
    member Failure : Microsoft.Web.Administration.ApplicationPoolFailure
    member ManagedPipelineMode : Microsoft.Web.Administration.ManagedPipelineMode with get, set
    member ManagedRuntimeVersion : string with get, set
    member Name : string with get, set
    member ProcessModel : Microsoft.Web.Administration.ApplicationPoolProcessModel
    member QueueLength : int64 with get, set
    member Recycle : unit -> Microsoft.Web.Administration.ObjectState
    member Recycling : Microsoft.Web.Administration.ApplicationPoolRecycling
    member Start : unit -> Microsoft.Web.Administration.ObjectState
    member State : Microsoft.Web.Administration.ObjectState
    member Stop : unit -> Microsoft.Web.Administration.ObjectState
    member WorkerProcesses : Microsoft.Web.Administration.WorkerProcessCollection
  end

Full name: Microsoft.Web.Administration.ApplicationPool

  type: ApplicationPool
  inherits: ConfigurationElement
property ServerManager.ApplicationPools: ApplicationPoolCollection
ConfigurationElementCollectionBase.Add(element: ApplicationPool) : ApplicationPool
ApplicationPoolCollection.Add(name: string) : ApplicationPool
property ApplicationPool.Enable32BitAppOnWin64: bool
property ApplicationPool.ManagedRuntimeVersion: string
val site : string * string -> unit

Full name: Snippet.site
val liveSite : Site

  type: Site
  inherits: ConfigurationElement
type Site =
  class
    inherit Microsoft.Web.Administration.ConfigurationElement
    member ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
    member Applications : Microsoft.Web.Administration.ApplicationCollection
    member Bindings : Microsoft.Web.Administration.BindingCollection
    member GetWebConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member Id : int64 with get, set
    member Limits : Microsoft.Web.Administration.SiteLimits
    member LogFile : Microsoft.Web.Administration.SiteLogFile
    member Name : string with get, set
    member ServerAutoStart : bool with get, set
    member Start : unit -> Microsoft.Web.Administration.ObjectState
    member State : Microsoft.Web.Administration.ObjectState
    member Stop : unit -> Microsoft.Web.Administration.ObjectState
    member ToString : unit -> string
    member TraceFailedRequestsLogging : Microsoft.Web.Administration.SiteTraceFailedRequestsLogging
    member VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
  end

Full name: Microsoft.Web.Administration.Site

  type: Site
  inherits: ConfigurationElement
property Site.ServerAutoStart: bool
property Site.ApplicationDefaults: ApplicationDefaults
property ApplicationDefaults.ApplicationPoolName: string
property Site.TraceFailedRequestsLogging: SiteTraceFailedRequestsLogging
property SiteTraceFailedRequestsLogging.Enabled: bool
property SiteTraceFailedRequestsLogging.Directory: string
val liveName : string

Full name: Snippet.liveName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val livePool : ApplicationPool

Full name: Snippet.livePool

  type: ApplicationPool
  inherits: ConfigurationElement
val liveSite : unit

Full name: Snippet.liveSite

  type: unit
  implements: IComparable
property ApplicationPool.Name: string
val accName : string

Full name: Snippet.accName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val accPool : ApplicationPool

Full name: Snippet.accPool

  type: ApplicationPool
  inherits: ConfigurationElement
val ignore : 'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
ServerManager.CommitChanges() : unit

Comments

Popular posts from this blog

Making your domain less mutable

This happens regularly to me (and from my anecdotal investigation everyone involved in large / old projects). We need a new piece of functionality. I write it, it's beautiful and I win the internet. I have estimated 8 days (or 22.23 lol-points depending on how you live) and it's only taken 4 days. Ah, but then a very small; mostly ignored and very unimportant detail rears it's cruel head. You need to make it work with the code that exists already. This is normally in the form of saving to some pre-existing entities. Oh dear. You save everything through the various management / service classes that exist already and nothing works. So begins the next couple of days of horror. You find that you didn't set the work = true . Most of my woes in this area are caused by modifications at layer further down (or the stored procedure it finally ends up in) changing the object that I was trying to save or not saving part of the object because of some rule. So many errors

My home office upgrade wish list.

My home office is almost due an upgrade. I have been holding off until my youngest daughter is out of her cot as then we can finally dispatch the enormous monstrosity of a cot out from the kids bedroom and the drawers that are in my office can be banished giving me better access to my wonderful whiteboard. My other improvements will be purchasing a new, larger monitor. I currently work from a single 22ich Samsung which just doesn't cut it anymore, I did have two at some point but I can't recall what I did with it. I really enjoy using a touch screen so I think I will go for one of these 27inch Hannspree models that I have used before. I put a lot of hours in at home and whilst I have a reasonable chair I still tend to suffer with some back problems, so my next port of call will be to get a Varidesk for home. It works an absolute treat at work and just lets me switch stuff up when I feel like it. they take a reasonable amount of desk space up but I tend to leave my desk fairly