передача переменной Powershell в XML - PullRequest
0 голосов
/ 01 ноября 2019

Начну с того, что уверен, что это обман Вставка переменной PowerShell в XML

Проблема в том, что мне трудно понять, какизмените это для моего использования.

Мой XML -

<?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>

Мой очень простой тестовый код:

$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 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

как передать Powershell $ ImagePath в XML?

Решение, которое выглядит так, как будто оно может соответствовать примеру, который я пытаюсь следовать из Вставка переменной PowerShell в XML is:

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

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

Но я не могу понять, как вписать его в мой код. Это похоже на такой простой вопрос ... но ответ не так прост, как я ожидал.

Попробовал что-то сейчас ... все еще отклоняясь от того же примера ... Я пытался. ..

$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)

На этот раз получаю ошибку:

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

Спасибо за помощь!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...