当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 在 Web.config AppSettings 中添加条目

作者:精品下载站 日期:2024-12-14 14:09:29 浏览:12 分类:玩电脑

PowerShell 在 Web.config AppSettings 中添加条目


SharePoint Web 应用程序(或任何基于 ASP.NET 构建的网站)使用 Web.config 文件来存储和检索网站配置,例如数据库连接字符串、安全性等。web.config 文件实际上是一个带有 .config 扩展名的 XML 文件,通常位于 Web 应用程序的根目录。

您有时可能需要添加/更新 web.config 条目。我们可以使用 PowerShell 自动更改 SharePoint Web 应用程序的 web.config 文件。此 PowerShell 脚本使您能够更新 web.config 文件。

PowerShell 在 web.config 的 AppSettings 部分中添加条目

web.config 中的 AppSettings 部分可用于读取/更新 SharePoint 自定义的配置值。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Config Parameters
$WebAppURL = "https://intranet.crescent.com"
$KeyName = "LicenseKey"
$KeyValue = "91e727402aa24e769e2779c155168e3"

#Get the web application
$WebApp = Get-SPWebApplication $WebAppURL

#Create new object for Web.config settings
$WebConfigModification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$WebConfigModification.Path = "configuration/appSettings"
$WebConfigModification.Name = "add[@key='$KeyName'][@value='$KeyValue']"
$WebConfigModification.Value = "<add key='$KeyName' value='$KeyValue' />"
$WebConfigModification.Sequence = 0
$WebConfigModification.Type = 0       
$WebConfigModification.Owner = "SharePointChartKey"
 
#Add "AppSettings" Entry to Web application's web.config
$WebApp.WebConfigModifications.add($WebConfigModification)
$WebApp.Update()
$WebApp.Parent.ApplyWebConfigModifications() 

这会向 AppSettings 部分添加一个新条目。

[玩转系统] PowerShell 在 Web.config AppSettings 中添加条目

Web.config 更改不需要重新启动服务器!

请注意,web.config 文件更改始终存在风险!如果您停止“Microsoft SharePoint Foundation Web 应用程序”服务,它们可能会丢失。由于它是特定于计算机的,因此您必须在多服务器环境中的每个 WFE 中重复更改。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯