Remotely editing web.config

Welcome Forums General PowerShell Q&A Remotely editing web.config

This topic contains 5 replies, has 4 voices, and was last updated by

3 years, 9 months ago.

  • Author
  • #28659

    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    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.

    #To point the web.config towards (HDC) database.
    #To point the web.config towards (EDC) database.
    #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();
        $connection = $root.connectionStrings.add | Where {$ -eq "ConnectionKey"}
        $datacentercode = $root.appSettings.add | where { $_.key -eq "DataCentreCode"}
            $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
            $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
        $connection.connectionstring= $newconnection
        #save the config file
        #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
  • #28660

    Topics: 5
    Replies: 28
    Points: 1
    Rank: Member

    What does the error say?

  • #28661

    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    Cannot set "connectionString" because only strings can be used as values to set XmlNode properties.
    + CategoryInfo : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : XmlNodeSetShouldBeAString
    + PSComputerName : localhost

  • #28667

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

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


    To convert it to a string, you could try:

    $connection.connectionstring= $newconnection.ToString()


    $connection.connectionstring= ($newconnection | Out-String)
  • #28671

    Topics: 9
    Replies: 62
    Points: 6
    Rank: Member


    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.

  • #28689

    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    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();
        $connection = $root.connectionStrings.add | Where {$ -eq "ConnectionKey"}
        $datacentercode = $root.appSettings.add | where { $_.key -eq "DataCentreCode"}

The topic ‘Remotely editing web.config’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort