Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Powershell变量传递到XML_Xml_Powershell - Fatal编程技术网

将Powershell变量传递到XML

将Powershell变量传递到XML,xml,powershell,Xml,Powershell,我首先要说的是,我确信这是一场骗局 问题是我很难弄清楚如何修改它以供我使用 我的XML是 <?xml version="1.0"?> <ImageSHDriveConfig> <PartitionsFile>partitions.txt</PartitionsFile> <SSDSettings> <!-- <DriveManufacturer>ASMT</DriveManufacturer>

我首先要说的是,我确信这是一场骗局

问题是我很难弄清楚如何修改它以供我使用

我的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>
由此产生的错误是:

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中

从我试图遵循的示例来看,这个解决方案可能适合:


谢谢你的帮助

您的简单测试代码有什么问题?发生了什么?很好的问题!我补充了之前的遗漏。感谢您指出这一点。因此,
config.xml
是在外部创建的,根据提供的代码,您的脚本将只设置
元素的值?是否保证该元素始终存在,或者有时您的脚本可能正在创建它?如果在使用
[xml]$xml=(Get Content$XMLname)
读取该元素时,该元素在文件中不存在,则相应的属性也将不存在。@BACON脚本仅提供该元素的值。XML中已经存在字段,因此每次读取文件时都保证存在该字段。再加上下次读取该文件时,该元素仍将在上次运行脚本时存在。您的简单测试代码有什么问题?发生了什么?很好的问题!我补充了之前的遗漏。感谢您指出这一点。因此,
config.xml
是在外部创建的,根据提供的代码,您的脚本将只设置
元素的值?是否保证该元素始终存在,或者有时您的脚本可能正在创建它?如果在使用
[xml]$xml=(Get Content$XMLname)
读取该元素时,该元素在文件中不存在,则相应的属性也将不存在。@BACON脚本仅提供该元素的值。XML中已经存在字段,因此每次读取文件时都保证存在该字段。再加上下次读取该文件时,该元素仍将在上次运行脚本时存在。
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
$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