locked
Unable to Enter the value of a variable in web.config RRS feed

  • Question

  • I am unable to update the value of $t which either holds enable or disable string.

    $temp = Read-Host "Do you want to enable or disable IsNotificationService "
    $t=$temp.Equals('enable')
    Foreach($Server in Get-Content "C:\temp\Servers.txt")
    {
    Invoke-Command $Server -ScriptBlock {
    $webConfig = "C:\rsweb.config"
    $doc = (Get-Content $webConfig) -as [Xml]
    $doc.configuration.Service.IsNotificationService=$t
    $doc.Save($webConfig)
    $check=$doc.configuration.Service.IsNotificationService
    Write-host "IsNotificationService of " $check
    }
    }

    Below is the error

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

    But when i try to do write-host $t it prints ...same issue is there with $server variable as well.. i am unable to print $server value in Write-host "IsNotificationService of "$server $check


    Best Regards,ACDBA

    Friday, July 24, 2020 6:56 PM

Answers

  • Actually, $t is present and contains a defined value . . . but it's a boolean true or false, not a string. It's not defined in the scriptblock because it hasn't been defined as "$using:t"

    Without changing any of the code in the "For" loop (other than adding the "$using:"), just change the way the way the enable/disable value is acquired:

    Do {
        $temp = Read-Host "Do you want to enable or disable IsNotificationService "
    } Until ($temp -eq 'enable' -or $temp -eq 'disable')
    $t = $temp

    But I'd probably rewrite the script as:

    $temp = ""
    Do {
        $temp = Read-Host "Do you want to enable or disable IsNotificationService "
    } Until ($temp -eq 'enable' -or $temp -eq 'disable')
    
    $Servers = Get-Content "C:\temp\Servers.txt"
    Invoke-Command $Servers -ScriptBlock {
        $webConfig = "C:\rsweb.config"
        $doc = (Get-Content $webConfig) -as [Xml]
        $doc.configuration.Service.IsNotificationService = $using:temp
        $doc.Save($webConfig)
        $check = $doc.configuration.Service.IsNotificationService
        Write-Host "IsNotificationService of " $check
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Saturday, July 25, 2020 2:41 AM

All replies

  • "$t" is not defined and is null.

    Please format and post code correctly if you want us to answer your question.

    Edit your post and follow these instructions:


    \_(ツ)_/

    • Proposed as answer by jrv Saturday, July 25, 2020 2:47 AM
    Friday, July 24, 2020 7:04 PM
  • Actually, $t is present and contains a defined value . . . but it's a boolean true or false, not a string. It's not defined in the scriptblock because it hasn't been defined as "$using:t"

    Without changing any of the code in the "For" loop (other than adding the "$using:"), just change the way the way the enable/disable value is acquired:

    Do {
        $temp = Read-Host "Do you want to enable or disable IsNotificationService "
    } Until ($temp -eq 'enable' -or $temp -eq 'disable')
    $t = $temp

    But I'd probably rewrite the script as:

    $temp = ""
    Do {
        $temp = Read-Host "Do you want to enable or disable IsNotificationService "
    } Until ($temp -eq 'enable' -or $temp -eq 'disable')
    
    $Servers = Get-Content "C:\temp\Servers.txt"
    Invoke-Command $Servers -ScriptBlock {
        $webConfig = "C:\rsweb.config"
        $doc = (Get-Content $webConfig) -as [Xml]
        $doc.configuration.Service.IsNotificationService = $using:temp
        $doc.Save($webConfig)
        $check = $doc.configuration.Service.IsNotificationService
        Write-Host "IsNotificationService of " $check
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Saturday, July 25, 2020 2:41 AM