Iis – Edit .NET 4.0 Machine Key settings at a computer wide level in IIS 7.0

.net-4.0asp.netiisiis-7iis-7.5

I'm writing an implementation plan involving changing ASP.NET machine key settings for .NET 4.0 on servers that are running IIS 7.0.

I'd prefer to have the setting changed via IIS Manager.

On my local machine, running IIS 7.5 this is easy. Go into IIS Manager, and in the Actions pane on the right hand right is a command Change .NET Framework Version. I can change this between .NET 2.0 and .NET 4.0, and this changes which machine level web.config I am editing when I change the machine key from the IIS Manager home page.

However IIS 7.0 does not have the Change .NET Framework Version command anywhere I can see. I therefore can't see any way for the .NET 4.0 machine key setting to be changed, other than manually editing C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config

So two questions

  1. Is there an equivalent to the IIS Manager 7.5 command "Change .NET Framework Version" in IIS Manager 7.0?
  2. Is there any other way to set the machine key settings in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config without manually editing the file on an IIS 7.0 machine?

Best Answer

It should be possible to script such a process, I would probably start with PSH as it has inbuilt XML support and access to .NET (useful for generating new keys if they are being specified).

But the first thing is to get the list of virtual directories at which to start. This is mostly easily done with appcmd:

# Use array constructor to force an array, event with only one result
# format: VDIR "name" (physicalPath:path)
$vdirs = @(C:\Windows\system32\inetsrv\appcmd.exe list vdir)
$vdirs = $vdirs | Where-Object { -not [string]::IsNullOrEmpty($_) } | Foreach-Object -Process {
  if ($_ -match "physicalPath:([^)]+)\)$") {
   $matches[1]
  }
}

At this point $vdirs is an array of folders mapped to IIS.

Within each of these search for web.config files:

$configFiles = $vdirs | Foreach-Object -Process {
  Get-ChildItem -Path $_ -Recurse -Filter "web.config" | Foreach-Object -Process { $_.FullName }
}

Which is an array of the full path names of the web.config files. At this point you can use PowerShell's XML support to examine or update any part of the file you like