PowerShell v2 Gathering Details for Updating IIS

Over the past year I have accumulated a lot of scripts working with IIS. To help explain how I come up with some of my settings I have decided to document my approach. The information here pertains to PowerShell v2 and IIS 7/7.5.  To begin the process open up an instance of IIS Server Manager.  In this example, I will be updating the server level SMTP from address.  To demonstrate the tranditional, GUI-based task I will show screenshots of how you would do this in IIS first.

First, highlight the server level object:


In this case I have updated the E-mail address value to a dummy setting.  Next, I switch to the Configuration Editor (the icon with the red arrow below it in the image below).


Once in the configuration editor I navigate to the node I need to update.  In this case, it is system.net/mailSettings/smtp.  Note, the tree levels correlate to slashs in PSPaths.  So, as I have it listed is how you will use it in PowerShell, but, we will get to that.  Below is a screenshot of how to find a specific section in the .config.


With the selected node displayed int he Editor you can directly update values in the .config file via the GUI.  To apply them you have to hit the Apply link button in the top right hand corner of the form.


Notice the Generate Script link button immediately below Apply/Cancel.  This button will only be activated if you have made a change and selected another form field.  Once you have some change that needs to be written down to the .config file click Generate Script and you will be given three options in IIS 7/7.5:

Managed Code (C#)


Scripting (Javascript)


Command Line (AppCmd)


While seeing some autogenerated code is good, the key here is paying attention to a few pieces of data: 
  1. Section: system.net/mailSettings/smtp (taken from the C# view)
  2. Index: this is the value inside the smtpSection brackets, or, the index.  This is effectively the name you use in PowerShell.  In this case, the index is From.
  3. Value: this is whatever is on the right side of the equals sign.  In this case, "youremail@mail.com"
With these raw pieces of data we can figure out how to update the .config with PowerShell.  One key here, however, is recognize exactly which .config file we are writing to with our commands.  In IIS 7/7.5 the easiest way to figure this out is to look at the Scripting (JavaScript) pane and look at the .CommtPath property.  In our case, the value is MACHINE/WEBROOT.  It took me a while to recognize that there are several layers to which you can write your settings from PowerShell. I have some other posts about that and getting into an in depth post about IIS configuration mapping would be a bit of a rabbit trial.  I just felt it was important to note that you must pay attention to where you are writing.  Most of the time people are dealing with application or website level code, so, it will be very rare that you touch machine.config or web.config on an IIS box, but, it is important to know how to do it if you must.  Just keep this gotcha in mind.  

So, to the PowerShell.  To update our settings, we use this command:
Import-Module WebAdministration
Set-WebConfigurationProperty -Name From -Value myemail@email.com -PSPath machine/webroot -Filter system.net/mailSettings/smtp
So, in two lines of code we have performed the same task that would take at least 5 steps in IIS via the GUI. Pretty cool.

Related Posts by Categories

0 comments:

Post a Comment