1

I'll start by saying that I'm sure this is a dupe of Inserting a PowerShell variable in XML

The problem is I am having a hard time figuring out how to modify this for my use.

My XML is

<?xml version="1.0"?>
<ImageSHDriveConfig>
  <PartitionsFile>partitions.txt</PartitionsFile>
  <SSDSettings>
    <!-- <DriveManufacturer>ASMT</DriveManufacturer> -->
    <DriveManufacturer>ASMT</DriveManufacturer>
    <DriveMinSize>127000000000</DriveMinSize>
    <DriveMaxSize>129000000000</DriveMaxSize>
  </SSDSettings>
  <ImageSettings ImageType="WIM">
     <ImagePath>Z:\Public\Users\test\OnwardImages\V1WIM\RS_CLIENT_TEAM\19508.1000.191030-1730\</ImagePath>
    <!-- <ImagePath>BionicKnee-TH2_RELEASE</ImagePath> -->
    <WIMFileName>install.wim</WIMFileName>
    <SWMFileName>install.swm</SWMFileName>
    <SWMPattern>install*.swm</SWMPattern>
  </ImageSettings>
</ImageSHDriveConfig>

my very simple test code is:

$branchName = Read-Host -Prompt "What is the branch name of the install.wim are we transfering?"
$buildNumber = Read-Host -Prompt "What is the build number of the install.wim are we transfering?"
$ImagePath = "Z:\Public\Users\test\OnwardImages\V1WIM\$branchName\$buildNumber"
$XMLname = "C:\OnwardInjecting\Tests\config.xml"

[xml]$xml=(Get-Content $XMLname)
$XML.ImageSettings.ImagePath="$ImagePath\"
$XML.save("$XMLname")

The resulting error is:

The property 'ImagePath' cannot be found on this object. Verify that the property exists and can be set.
At C:\OnwardInjecting\tests\xmltest.ps1:7 char:1
+ $XML.ImageSettings.ImagePath="$ImagePath"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

how do I pass the Powershell $ImagePath into XML?

The solution that looks like it might fit from the example I'm trying to follow from Inserting a PowerShell variable in XML is:

$str = @'
<config xmlns="http://www.sap.com/lmsl/slp">
  <parameter>
    <id>SidAdmUserName</id>
    <value>$SIDadm</value>
  </parameter>
</config>
'@

$str.Replace('$SIDadm', $SIDadm)

But I can not figure out how to fit it into my code. This seems like such a simple ask … but the answer doesn't appear to be as simple as I'd expect.

Tried something now... still going off of that same example... I tried...

$branchName = Read-Host -Prompt "What is the branch name of the install.wim are we transfering?"
$buildNumber = Read-Host -Prompt "What is the build number of the install.wim are we transfering?"
$ImagePath = "Z:\Public\Users\test\OnwardImages\V1WIM\$branchName\$buildNumber"
$XMLname = "C:\OnwardInjecting\Tests\config.xml"

[xml]$xml = @'
<ImageSHDriveConfig>
  <PartitionsFile>partitions.txt</PartitionsFile>
  <SSDSettings>
    <!-- <DriveManufacturer>ASMT</DriveManufacturer> -->
    <DriveManufacturer>ASMT</DriveManufacturer>
    <DriveMinSize>127000000000</DriveMinSize>
    <DriveMaxSize>129000000000</DriveMaxSize>
  </SSDSettings>
  <ImageSettings ImageType="WIM">
    <ImagePath>Path</ImagePath>
    <!-- <ImagePath>BionicKnee-TH2_RELEASE</ImagePath> -->
    <WIMFileName>install.wim</WIMFileName>
    <SWMFileName>install.swm</SWMFileName>
    <SWMPattern>install*.swm</SWMPattern>
  </ImageSettings>
</ImageSHDriveConfig>
'@

$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('ns', $xml.DocumentElement.NamespaceURI)

$xpath = '/ns:ImageSHDriveConfig/ns:ImageSettings[ns:ImagePath/text()="Path"]/ns:WIMFileName/ns:SWMFileName/ns:SWMPattern'

$xml.SelectSingleNode($xpath, $nsm).'#text' = $ImagePath
$xml.Save([Console]::Out)

This time getting the error:

The property '#text' cannot be found on this object. Verify that the property exists and can be set.
At C:\OnwardInjecting\tests\XMLTest2.ps1:30 char:1
+ $xml.SelectSingleNode($xpath, $nsm).'#text' = $ImagePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Thank you for your help!

Rj McKay
  • 53
  • 1
  • 6
  • What's wrong with your simple test code? What happens? – Mathias R. Jessen Nov 01 '19 at 17:05
  • Very good question! I added that previous omission. Thank you for pointing that out. – Rj McKay Nov 01 '19 at 17:13
  • So `config.xml` is created externally and, based on the code provided, your script will only be setting the value of the `` element? Is that element guaranteed to always exist, or sometimes your script might be creating it? If that element doesn't exist in the file when its read with `[xml]$xml=(Get-Content $XMLname)` then the corresponding property won't exist, either. – Lance U. Matthews Nov 01 '19 at 18:30
  • @BACON the script is only providing the value of the element. The XML already exists with the field in it so it's guaranteed to exist every time the file is read. Plus the next time that file is read, the element will still be there from the last time the script was run. – Rj McKay Nov 01 '19 at 20:12

0 Answers0