执行powershell脚本以在appSettings中添加两个键时出错

时间:2013-08-02 13:41:07

标签: sharepoint powershell sharepoint-2010 web-config web.config-transform

我想在Sharepoint web.config文件中的appSetting中添加两个键值。

我找到了一个使用以下链接

修改blobcache属性的好例子

http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/14/use-powershell-to-script-changes-to-the-sharepoint-web-config-file.aspx

以及我可以添加新密钥的链接。

http://farhanfaiz.wordpress.com/2011/09/07/sharepoint-2010-powershell-script-to-add-in-web-config/

这是我在Appsetting中添加两个键的代码。

function AddAppSettingKey { 

param( 

    [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] 

    [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind] 

    $WebApplication 


) 

process { 

    Write-Host "Modification starts..." -ForegroundColor Green
    $WebApp = $WebApplication.Read() 
    Write-Host $WebApp

    $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $configMod1.Path = "configuration/appSettings"
    $configMod1.Name =  "add[@name='key']"
    $configMod1.Value = "<add key= 'RedirectToPage' value='Main.aspx' />"
    $configMod1.Sequence = 0
    $configMod1.Type = 1 
    $configMod1.Owner = "AppSettingMod"

    $configMod2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $configMod2.Path = "configuration/appSettings" 
    $configMod2.Name =  "add[@name='key']"
    $configMod2.Value = "<add key= 'isProd' value='false' />"
    $configMod2.Sequence = 0
    $configMod2.Type = 1 
    $configMod2.Owner = "AppSettingMod" 

    $WebApp.WebConfigModifications.Add($configMod1)
    $WebApp.WebConfigModifications.Add($configMod2)  
    $WebApp.Update() 
    $WebApp.Parent.ApplyWebConfigModifications()

     Write-Host "Done......" -ForegroundColor Cyan

    }

}

但我的问题是当我执行代码时,我得到以下错误:

Exception calling "ApplyWebConfigModifications" with "0" argument(s): "'/' is a
n unexpected token.

任何输入都会非常有用。

1 个答案:

答案 0 :(得分:0)

我认为“.Type = 1”是SPWebConfigModificationType.EnsureAttribute,而我应该使用SPWebConfigModificationType.EnsureChildNode(0)

更改为“.Type = 0”并重试。