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...
答案 0 :(得分:2)
以下内容将在运行时元素之后插入新的XML数据:
$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
存在,因此我们拥有InsertAfter()
方法的正确对象类型。相应的ImportNode()
方法输出我们想要的对象类型。 $true
被传递,以便我们导入system.net
的所有后代元素。
在处理文件时,我使用了完整路径。我相信.Save()
方法通常需要完整路径,因为它默认为特定目录。请注意,这不是通用代码,它取决于您专门提供要从中插入新数据的元素。
答案 1 :(得分:0)
还有一种使用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"
答案 2 :(得分:0)
我最终做了以下事情:
$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
这不是最好的选择,因为我假设文件的最后一行始终是相同的,但它肯定对我有用。
糟糕的是,我无法像rokumaru所建议的那样使代码正常工作,因为这将是最好的用法,我希望他会补充此代码以使其正常工作。