none
Replace Value in XML file with machines IP address RRS feed

  • Question

  • Hi, i have an XML file named user.config which contains the following.


    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <DeviceMonitor.Properties.Settings>
                <setting name="EchoServerURI" serializeAs="String">
                    <value>http://127.0.0.1:8080</value>
                </setting>
                <setting name="EchoServerUsername" serializeAs="String">
                    <value>username</value>
                </setting>
                <setting name="EncryptedEchoServerPassword" serializeAs="String">
                    <value>0000000000000000</value>
                </setting>
                <setting name="EchoCaptureSettingDescription" serializeAs="String">
                    <value>Capture Device Monitor Adhoc Capture</value>
                </setting>
                <setting name="EchoCaptureSettingDuration" serializeAs="String">
                    <value>15</value>
                </setting>
                <setting name="EchoCaptureProfile" serializeAs="String">
                    <value />
                </setting>
                <setting name="EchoCaptureCourseRef" serializeAs="String">
                    <value />
                </setting>
            </DeviceMonitor.Properties.Settings>
        </userSettings>
    </configuration>

    I need the IP address value to be changed to local machines IP that a powershell script is run on, i can get the IP with :

    $ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1} 
    $ip.ipaddress[0] 

    However i don't know how to have this value injected into the XML, any help will be appreciated

    ( i assume Get-Content / ForEach-Object ?)

    Cheers, 

    Dan 

    Tuesday, September 23, 2014 4:24 AM

Answers

  • Solved with below:
    1. $path = 'C:\temp\testip.xml'
    2.  
    3. $ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
    4. $ipaddress=$ip.ipaddress[0]
    5.  
    6. $xml = [xml](Get-Content $path)
    7. $node = $xml.configuration.usersettings."devicemonitor.properties.settings".setting | where {$_.Name -eq 'EchoServerURI'}
    8. $node.value = 'http://' + $ipaddress + ':8080'
    9. $xml.Save($path)
    Tuesday, September 23, 2014 5:27 AM

All replies

  • Solved with below:
    1. $path = 'C:\temp\testip.xml'
    2.  
    3. $ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
    4. $ipaddress=$ip.ipaddress[0]
    5.  
    6. $xml = [xml](Get-Content $path)
    7. $node = $xml.configuration.usersettings."devicemonitor.properties.settings".setting | where {$_.Name -eq 'EchoServerURI'}
    8. $node.value = 'http://' + $ipaddress + ':8080'
    9. $xml.Save($path)
    Tuesday, September 23, 2014 5:27 AM
  • Be careful as this won't work on multi-homed systems.

    $IP=(get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True').IPAddress[0]
    $xml = [xml](Get-Content $path)
    if($n=$xml.SelectSingleNode("//setting[@name='EchoServerURI']")({
        $n.value="http://$ip:8080"
        $xml.Save($path)
    }
    

    .


    ¯\_(ツ)_/¯

    Tuesday, September 23, 2014 10:51 AM