英文:
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
<?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.
答案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="app_name"]').Node.Value"
注意:
* 如示例所示,您的样本 XML 不是有效的,因为 [`System.Xml.XmlDocument`](https://learn.microsoft.com/en-US/dotnet/api/System.Xml.XmlDocument) (`[xml]`) 类显然需要 `"yes"` - 而不是 `"true"` - 作为 `standalone` 属性值。
* 另外,在上面的 XPath 查询中,我已将 `entry` 替换为 `setParameter` 以匹配您的样本 XML。
* 为了健壮性,整个 `-c` (`-Command`) 参数(如果省略 `-Command` 参数,则参数 `-Command` 是 _隐含的_)<sup>[1]</sup> 都被包含在 `"..."` 中,尽管在这种情况下这并非绝对必要;但是,无论如何,您确实需要转义作为 PowerShell 命令的一部分的 `"`,如 `\"`
---
<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 "(Select-Xml -Literalpath C:\AMD\setparam.xml '//setParameter[@name=\"app_name\"]').Node.Value"
Note:
-
As shown, your sample XML isn't valid, because the
System.Xml.XmlDocument
([xml]
) class apparently requires"yes"
- not"true"
- as thestandalone
attribute value. -
Also, in the XPath query above I've replaced
entry
withsetParameter
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"..."
, which isn't strictly necessary in this case; however, you do need to escape the"
that are part of the PowerShell command either way, as\"
<sup>[1] By contrast, pwsh
, the PowerShell (Core) CLI, now defaults to -File
.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论