使用ansible.windows.win_shell更改元素文本。

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

Change element text with ansible.windows.win_shell

问题

抱歉,我只能翻译您提供的文本。以下是翻译好的内容:

我尝试使用ansible.windows.win_shell来更改一个元素的文本。

这是我拥有的XML:

    <element-A>
    	<element-B />
    </element-A>

而这是我想要得到的XML:

    <element-A>
    	<element-B> 文本 </element-B>
    </element-A>

我尝试运行的win_shell:

    - name: 一些名称
      win_shell: |
        [xml]$myFile = Get-Content "C:\MyFile.xml"
        $myFile.element-A.element-B = 'TEXT'
        $myFile.Save("C:\MyFile.xml")

我收到的错误:

    "找不到对象上的属性 'element-B'。请验证该属性是否存在且可设置。"

有人能帮忙吗?
英文:

I try to change an element text, using ansible.windows.win_shell.

This is the XML I have:

<element-A>
	<element-B />
</element-A>

and this is the XML I would like to have:

<element-A>
	<element-B> TEXT </element-B>
</element-A>

The win_shell I tried to run:

- name: some-name
  win_shell: |
    [xml]$myFile = Get-Content "C:\MyFile.xml"
    $myFile.element-A.element-B = 'TEXT'
    $myFile.Save("C:\MyFile.xml")

The error I get:

"The property 'element-B' cannot be found on this object. Verify that the property exists and can be set."

Can someone help?

答案1

得分: 0

您知道 win_shell 默认调用 PowerShell。如果要在 PowerShell 中调用带有特殊字符的方法,应该像这样引用方法调用:

$myFile.'element-A'.'element-B' = 'TEXT'

这应该可以正常工作。

英文:

As you know win_shell calls powershell by default. And if you want to call a method with special characters in powershell, you should quote the method call like this:

$myFile.'element-A'.'element-B' = 'TEXT'

this should work.

答案2

得分: 0

使用 SelectSingleNode 解决了我的问题:

       - name: some-name
          win_shell: |
            [xml]$myFile = Get-Content "C:\MyFile.xml"
            $myFile.SelectSingleNode("//element-A/element-B").InnerText = "TEXT"
            $myFile.Save("C:\MyFile.xml")
英文:

Using SelectSingleNode solved the problem for me:

   - name: some-name
      win_shell: |
        [xml]$myFile = Get-Content "C:\MyFile.xml"
        $myFile.SelectSingleNode("//element-A/element-B").InnerText = "TEXT"
        $myFile.Save("C:\MyFile.xml")

huangapple
  • 本文由 发表于 2023年2月19日 21:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500360.html
匿名

发表评论

匿名网友

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

确定