Reputation: 13
I am automating SQL Reporting services and i need to add proxy settings to the web.config file. I try to do this in powershell but i am unable to figure out how to do this through powershell.
I need to add the following piece of xml code to the end of an existing web.config XML file
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy bypassonlocal="True" proxyaddress="http://ocs.kennisnet.nl:3128" />
</defaultProxy>
</system.net>
The complete xml file needs to be:
<configuration>
<system.web>
<pages validateRequest="false">
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</controls>
<namespaces>
<remove namespace="System.Linq" />
<remove namespace="System.ComponentModel.DataAnnotations" />
</namespaces>
</pages>
<authentication mode="Windows" />
<identity impersonate="true" />
<compilation defaultLanguage="c#" debug="false" targetFramework="4.0" tempDirectory="E:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\RSTempFiles">
<assemblies>
<clear />
<add assembly="ReportingServicesWebServer" />
<add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add assembly="System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add assembly="System.Web.DynamicData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
<sessionState mode="Off" />
<httpHandlers>
<add verb="*" path="Reserved.ReportServer" type="Microsoft.ReportingServices.WebServer.ReportServiceHttpHandler, ReportingServicesWebServer" />
<add verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, ReportingServicesWebServer, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>
<httpModules>
<clear />
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
<add name="Profile" type="System.Web.Profile.ProfileModule" />
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</httpModules>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
<httpRuntime executionTimeout="9000" requestValidationMode="2.0" />
<securityPolicy>
<trustLevel name="RosettaSrv" policyFile="rssrvpolicy.config" />
</securityPolicy>
<trust level="RosettaSrv" originUrl="" legacyCasModel="true" />
<webServices>
<soapExtensionTypes>
<add type="Microsoft.ReportingServices.WebServer.RsSoapExtension, ReportingServicesWebServer" priority="1" group="1" />
</soapExtensionTypes>
<soapExtensionReflectorTypes>
<add type="Microsoft.ReportingServices.WebServer.RsSoapExtensionReflector, ReportingServicesWebServer" />
</soapExtensionReflectorTypes>
</webServices>
</system.web>
<runtime>
<alwaysFlowImpersonationPolicy enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.ReportingServices.Interfaces" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="8.0.242.0-14.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.ReportingServices.ProcessingCore" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="9.0.242.0-14.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.ReportingServices.ProcessingObjectModel" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="9.0.242.0-14.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="msmgdsrv" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="9.0.0.0-14.0.0.0" newVersion="14.0.0.0" />
<codeBase version="14.0.0.0" href="%ProgramFiles%\Microsoft Analysis Services\AS OLEDB\140\msmgdsrv.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy bypassonlocal="True" proxyaddress="http://ocs.kennisnet.nl:3128" />
</defaultProxy>
</system.net>
</configuration>
I found a lot of code examples but none seem to work, i managed to create bellow code but it doesn't save the file, and i don't know how to add the second key values. Hopefully one of you are more skilled in this than i am ..
$file="web.config"
[xml]$xmlDoc = [System.Xml.XmlDocument](Get-Content $file)
$newEl=$xmlDoc.CreateElement("system.net")
$xmlDoc.configuration.AppendChild($newEl)
$xmlDoc.Save($file)
$newEl=$xmlDoc.CreateElement("defaultProxy")
$nameAtt1=$xmlDoc.CreateAttribute("enabled")
$nameAtt1.psbase.value="true"
$newEl.SetAttributeNode($nameAtt1)
$nameAtt2=$xmlDoc.CreateAttribute("useDefaultCredentials")
$nameAtt2.psbase.value="true"
$newEl.SetAttributeNode($nameAtt2)
$xmlDoc.configuration["system.net"].AppendChild($newEl)
$xmlDoc.Save($file)
The eventual result need to create the complete XML file as shown above.
Please help...
Upvotes: 1
Views: 966
Reputation: 1244
There is also a way to use XElement.
using namespace "System.Xml.Linq"
Add-Type -AssemblyName System.Xml.Linq
$configPath = Convert-Path "web.config"
$newElement = [XElement]@'
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy bypassonlocal="True" proxyaddress="http://ocs.kennisnet.nl:3128" />
</defaultProxy>
</system.net>
'@
$configuration = [XElement]::Load($configPath)
$configuration.Add($newElement)
$configuration.ToString() | Out-File "new.config"
Upvotes: 0
Reputation: 13
I ended up doing the following:
$newXml = @'
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy bypassonlocal="True" proxyaddress="http://ocs.kennisnet.nl:3128" />
</defaultProxy>
</system.net>
</configuration>
'@
$File = ".\web.config"
# Remove last line of file
$Content = Get-Content $File
$Output = $Content[0..($Content.count - 2)]
Out-File -FilePath $File -InputObject $Output
# Add code block to file
Add-Content -Path ".\web.config" -Value $newXml
This is not the nicest thing to use as i assume the last line of the file is always the same but it sure works for me.
To bad i couldn't get the code to work as suggested by rokumaru as this would be the best to use and i hope he will complement this code to get it working.
Upvotes: 0
Reputation: 25001
The following will insert the new XML data after the runtime element:
$webconfig = [xml](Get-Content C:\temp\web.config)
$newxml = [xml](Get-Content c:\temp\new.xml)
$newNode = $webconfig.ImportNode($newxml.'system.net',$true)
$InsertAfterNode = $webconfig.configuration.runtime
$null = $webconfig.configuration.insertAfter($newNode,$InsertAfterNode)
$webconfig.Save("c:\temp\web.config")
$newNode
exists so that we have the correct object type for the InsertAfter()
method. The corresponding ImportNode()
method outputs the object type we want. $true
is passed so that we import all descendant elements of system.net
.
I used full paths when working with the files. I believe the .Save()
method typically requires a full path because it defaults to a specific directory. Please note that this is not generic code and relies on you specifically providing the element from where you want to insert new data.
Upvotes: 2