January 25th, 2011Manage ASP.NET trust settings with Powershell
Been working on various web server build script for IIS7 in Powershell
most of it was VERY easy and more then wel document on the internet, but managing the ASP.NET trusts, but editing the .net web.config file was a bit more tricky
by glueing some code from here and here i managed to create the follwing code
Which assign FULL trust for web site ID 2 for 64 bit V2 .net framework, i am sure anyone can now tweak to suite there own requirements
$webConfigPath = “C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config”
$currentDate = (get-date).tostring(“mm_dd_yyyy-hh_mm_s”) # month_day_year – hours_mins_seconds
$backup = $webConfigPath + “_$currentDate
# Get the content of the config file and cast it to XML and save a backup copy labeled .bak followed by the date
$xml = [xml](get-content $webConfigPath)
#save a backup copy
$xml.Save($backup)
#this was the trick I had been looking for
$root = $xml.get_DocumentElement();
#add node for automation web site
$newlocation = $xml.CreateElement(“location”)
$newlocation.SetAttribute(“Path”, “2″)
$newlocation.SetAttribute(“allowOverride”, “false”)
$newlocation.set_InnerXML(“<system.web><trust level=”"Full”" originUrl=”"”" /></system.web>”)
$root.AppendChild($newlocation)
# Save it
$xml.Save($webConfigPath)
