从XML中使用PowerShell读取键值。

huangapple go评论75阅读模式
英文:

read key value from xml using powershell

问题

I wish to read the below xml values for a given key in a secure encrypted manner using powershell.

So, for example, a given key port it should print the value 8814

Likewise for app_name it should print Signup-qa

<?xml version="1.0" standalone="true"?>
<parameters>
<setParameter value="Signup-qa" name="app_name"/>
<setParameter value="8814" name="port"/>
<setParameter value="true" name="debug"/>
</parameters>

I tried the below but I get error.

C:\WINDOWS\system32>powershell # [xml]$xml=Get-Content C:\AMD\setparam.xml

C:\WINDOWS\system32>powershell $xml.SelectSingleNode('//entry[@name="app_name"]').Value
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $xml.SelectSingleNode('//entry[@name=app_name]').Value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Also, wanted to know if this is a secure way [encrypted] to read the key-value?

I will eventually take this to a github actions workflow.

英文:

I wish to read the below xml values for a given key in a secure encrypted manner using powershell.

So, for example, a given key port it should print the value 8814

Likewise for app_name it should print Signup-qa

&lt;?xml version=&quot;1.0&quot; standalone=&quot;true&quot;?&gt;

&lt;parameters&gt;

&lt;setParameter value=&quot;Signup-qa&quot; name=&quot;app_name&quot;/&gt;
&lt;setParameter value=&quot;8814&quot; name=&quot;port&quot;/&gt;
&lt;setParameter value=&quot;true&quot; name=&quot;debug&quot;/&gt;

&lt;/parameters&gt;

I tried the below but I get error.

C:\WINDOWS\system32&gt;powershell # [xml]$xml=Get-Content C:\AMD\setparam.xml

C:\WINDOWS\system32&gt;powershell $xml.SelectSingleNode(&#39;//entry[@name=&quot;app_name&quot;]&#39;).Value
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $xml.SelectSingleNode(&#39;//entry[@name=app_name]&#39;).Value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Also, wanted to know if this is a secure way [encrypted] to read the key-value?

I will eventually take this to a github actions workflow.

答案1

得分: 3

<!-- language-all: sh -->

单独的 PowerShell CLI([`powershell.exe`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe))调用是独立的、不相关的会话,因此您无法在一个调用中创建状态(定义 `$xml` 变量),并期望另一个调用能够看到它。

相反,使用 _单个_ 调用,让 [`Select-Xml`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-Xml) 直接读取和查询您的文件:

powershell -c "(Select-Xml -Literalpath C:\AMD\setparam.xml ''//setParameter[@name=&quot;app_name&quot;]').Node.Value"


注意:

* 如示例所示,您的样本 XML 不是有效的,因为 [`System.Xml.XmlDocument`](https://learn.microsoft.com/en-US/dotnet/api/System.Xml.XmlDocument) (`[xml]`) 类显然需要 `&quot;yes&quot;` - 而不是 `&quot;true&quot;` - 作为 `standalone` 属性值。

* 另外,在上面的 XPath 查询中,我已将 `entry` 替换为 `setParameter` 以匹配您的样本 XML。

* 为了健壮性,整个 `-c` (`-Command`) 参数(如果省略 `-Command` 参数,则参数 `-Command` 是 _隐含的_)<sup>[1]</sup> 都被包含在 `&quot;...&quot;` 中,尽管在这种情况下这并非绝对必要;但是,无论如何,您确实需要转义作为 PowerShell 命令的一部分的 `&quot;`,如 `\&quot;`

---

<sup>[1] 相比之下,[`pwsh`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh),PowerShell (Core) CLI 现在默认使用 `-File`。<sup>
英文:

<!-- language-all: sh -->

Separate PowerShell CLI (powershell.exe) calls are separate, unrelated sessions, so you cannot create state (define the $xml variable) in one call and expect another to see it.

Instead, use a single call, and let Select-Xml read and query your file directly:

powershell -c &quot;(Select-Xml -Literalpath C:\AMD\setparam.xml &#39;//setParameter[@name=\&quot;app_name\&quot;]&#39;).Node.Value&quot;

Note:

  • As shown, your sample XML isn't valid, because the System.Xml.XmlDocument ([xml]) class apparently requires &quot;yes&quot; - not &quot;true&quot; - as the standalone attribute value.

  • Also, in the XPath query above I've replaced entry with setParameter to match your sample XML.

  • For robustness, the entire -c (-Command) argument (parameter -Command is implied if you omit it)<sup>[1]</sup> is enclosed in &quot;...&quot;, which isn't strictly necessary in this case; however, you do need to escape the &quot; that are part of the PowerShell command either way, as \&quot;


<sup>[1] By contrast, pwsh, the PowerShell (Core) CLI, now defaults to -File.</sup>

huangapple
  • 本文由 发表于 2023年5月10日 22:45:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定