# Remotely editing web.config

**URL:** <https://forums.powershell.org/t/remotely-editing-web-config/4699>\
**Category:** PowerShell Help\
**Created:** [August 16, 2015, 5:16pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699 "2015-08-16T17:16:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashutosh-kanojia](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@ashutosh-kanojia](https://forums.powershell.org/u/ashutosh-kanojia)\
**Post date:** [August 16, 2015, 5:16pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/1 "2015-08-16T17:16:40Z")

</div>

this script edits the datasource value for one of the connection string in configurations and one value in appsettings of a web.config file. its works perfectly when run locally but when passed to invoke-command as a script block to run in on multiple servers it just doesnt do anything and throws error.

```
[CmdletBinding()]
param(
#To point the web.config towards (HDC) database.
[switch]$toHDC,

#To point the web.config towards (EDC) database.
[switch]$toEDC
)

#setting directory location and filepaths and user
$WorkingDir = Get-Location
$path = $WorkingDir.path + "\server.txt"
$cred = Get-Credential
$serverList = Get-content $path
$Logfile = $WorkingDir.Path + "\$(Get-date -Format "yyyy-MM-dd")_Log.txt"

#create new log file if not present
if(!(Test-Path $Logfile)){
    New-Item -Path $Logfile -ItemType File
}

#Create persistent session with the list of servers.
$session = New-PSSession $serverList -Credential $cred

$hdcDB = "A"
$edcDB = "B"

#to switch DB as per parameter and write to log
$script= {

    $webConfig = "C:\temp\Web.config"
    $doc = (Get-Content $webConfig) -as [Xml]
    
    # getting current connection String and Datacentre code
    $root = $doc.get_DocumentElement();
	$root.
    $connection = $root.connectionStrings.add | Where {$_.name -eq "ConnectionKey"}
    $datacentercode = $root.appSettings.add | where { $_.key -eq "DataCentreCode"}
    $connection

    if($toHDC){
        $newconnection=$connection.connectionString.Replace($edcDB,$hdcDB);
        $newconnection
        $datacentercode.value = "HDC"
        Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") USER:$($cred.UserName) INFO:Datasource value changed to $newconnection" | Out-File -FilePath $Logfile -Append
    }

    if($toEDC){
        $newconnection=$connection.connectionString.Replace($hdcDB,$edcDB);
        $datacentercode.value = "EDC"
        Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") INFO:Datasource value changed to $newconnection" | Out-File -FilePath $Logfile -Append
    }
    $newconnection
    $connection.connectionstring= $newconnection

    #save the config file
    $doc.Save($webConfig)

    #Restarts the application pool
    Import-Module WebAdministration
    Restart-WebAppPool ABC
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") INFO:Application pool ABC restarted " | Out-File -FilePath $Logfile -Append
 
}

# Remotely executes the function to switch DB in config file on all the servers listed 
Invoke-Command -Session $session -ScriptBlock $script
#switchdb
```

---

<div class="post-metadata">

**Author:** ![robbiepc30](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbiepc30](https://forums.powershell.org/u/robbiepc30)\
**Post date:** [August 16, 2015, 5:39pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/2 "2015-08-16T17:39:33Z")

</div>

What does the error say?

---

<div class="post-metadata">

**Author:** ![ashutosh-kanojia](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@ashutosh-kanojia](https://forums.powershell.org/u/ashutosh-kanojia)\
**Post date:** [August 16, 2015, 5:45pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/3 "2015-08-16T17:45:33Z")

</div>

Cannot set “connectionString” because only strings can be used as values to set XmlNode properties.  
+ CategoryInfo : NotSpecified: (🙂 , SetValueException  
+ FullyQualifiedErrorId : XmlNodeSetShouldBeAString  
+ PSComputerName : localhost

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 17, 2015, 1:56am UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/4 "2015-08-17T01:56:57Z")

</div>

It’s possible that $newconnection is an XML object and that is where the error is coming from, you could validated with:

```
$newconnection.GetType()
```

To convert it to a string, you could try:

```
$connection.connectionstring= $newconnection.ToString()
```

or

```
$connection.connectionstring= ($newconnection | Out-String)
```

---

<div class="post-metadata">

**Author:** ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)\
**Post date:** [August 17, 2015, 8:04am UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/5 "2015-08-17T08:04:59Z")

</div>

Ankutosh,

The scriptblock is running in a different PowerShell session, so it does not have access to the variables in the parent script. You need to either pass them explicitly to the scriptblock using -Arguments in Invoke-Command and Param in the scriptblock, or implicitly using syntax like $Using:toEDC. The former method is generally preferred as it is easier to test and debug.

---

<div class="post-metadata">

**Author:** ![ashutosh-kanojia](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@ashutosh-kanojia](https://forums.powershell.org/u/ashutosh-kanojia)\
**Post date:** [August 17, 2015, 5:42pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/6 "2015-08-17T17:42:57Z")

</div>

When i try to read just the document and populate some variable in below statements it displays nothing.( commented the ifs and saving document part.)  
Root and connection both variables are still empty i guess thats why not able to set newconnection.

I tried few other ways i can find online but none seems to initiate them, they remain blank.  
Just cant understand whats happending

```
$root = $doc.get_DocumentElement();
	$root
    $connection = $root.connectionStrings.add | Where {$_.name -eq "ConnectionKey"}
    $datacentercode = $root.appSettings.add | where { $_.key -eq "DataCentreCode"}
    $connection
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/remotely-editing-web-config/4699/7 "2024-05-16T20:44:59Z")

</div>


