Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to update the web.config file through Powershell using the SharePoint class "SPWebConfigModification" however facing the issue mentioned below, please help me to fix this.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$url = New-Object System.Uri("http://serverName:port/")
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url) 
$mod = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification" 
$mod.Path = "configuration/system.web/customErrors"
$mod.Name = "mode"
$mod.Sequence = 0
$mod.Owner = "powershell" 
$mod.Type = "EnsureAttribute"
$mod.Value = "Off"
$webApp.WebConfigModifications.Add($mod)
$webApp.WebService.ApplyWebConfigModifications() 
$webApp.Update()

Error:

Exception calling "ApplyWebConfigModifications" with "0" argument(s): "'' is an invalid expression." At line:1 char:47 + $webApp.WebService.ApplyWebConfigModifications <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

share|improve this question
add comment

6 Answers

up vote 1 down vote accepted

Is this a new Web Application?

Often when dealing with SPWebConfigModification the ApplyWebConfigModifications() call will fail because of an earlier broken SPWebConfigModification that is sitting in the collection.

Try the following to see if there's anything unexpected in there

$webApp.WebConfigModifications | Format-Table Name,Owner

if the only things in there are yours, try $webApp.WebConfigModifications.Clear() first, otherwise you can clear it out by hand.

share|improve this answer
add comment

I found this article where the author is doing the same thing more or less, however he calls Update() BEFORE calling ApplyWebConfigModifications(). You might want to try that.

share|improve this answer
    
I tried that too but still the result is same....! –  Karthikeyan May 12 '10 at 23:42
add comment

I have copied your script I run a test and seems to be working fine (on a web app with port number and also on another without)

Are you running this from the server where front end http://serverName:port/ runs ?

share|improve this answer
    
The script also runs for me using localhost as the url on SharePoint 2010. –  Steve P Oct 13 '10 at 18:35
add comment

It might help to get a more detailed stacktrace of the exception.

detailed-exception-stacktrace-in-powershell

share|improve this answer
add comment

.Type is looking for an Enum, not the string "EnsureAttribute"
The easiest way I've found to handle this is use Visual Studio to determine the value of the enum you're looking for. EnsureAttribute is 1, so change

$mod.Type = "EnsureAttribute" to be
$mod.Type = 1

and you should be good to go.

share|improve this answer
    
Powershell can coerce strings to enums. –  Steve P Oct 17 '10 at 8:28
add comment

This worked for me $webApp.WebConfigModifications.Clear()

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.