无法在 powershell -WinRM 中编辑 xml 文件

Unable to edit xml file in powershell -WinRM

我正在尝试编辑一个 xml 文件,当我在本地机器上编辑时,代码块工作得很好

[xml]$webXML = Get-Content "C:\Deployment\ABCfolder\web.config"
$mainXML = $WebXml.configuration."system.webServer".httpRedirect
$mainXML.destination -replace "\w.*","http://localhost/ABCname"
$mainXML.Save($webXML)

但是当我尝试使用 winRM 在目标服务器上 运行 它时,它最终给出错误,代码如下:

$server = Get-Content "C:\Files\server.txt"
foreach ($s in $server){
$session = New-PSSession -ComputerName $s -ThrottleLimit 500
Invoke-Command -Session $session -ScriptBlock {
[xml]$webXML = Get-Content "C:\Deployment\ABCfolder\web.config"
$mainXML = $WebXml.configuration."system.webServer".httpRedirect
$mainXML.destination -replace "\w.*","http://localhost/ABCname"
$mainXML.Save($webXML)

注意:web.config目标服务器上存在文件

错误:

Method invocation failed because [System.Xml.XmlElement] does not contain a method 
named 'Save'.
  + CategoryInfo          : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
+ PSComputerName        : "SomeRandomIP"

代码逻辑应该是:

  1. 将 xml 读入 webXML
  2. 更改所需节点
  3. 将 webXML 写回文件

此代码应该有效:

[xml]$webXML = Get-Content "C:\Deployment\ABCfolder\web.config"
$mainXML = $WebXml.configuration."system.webServer".httpRedirect
$mainXML.destination = $mainXML.destination -replace "\w.*","http://localhost/ABCname"
$webXML.Save("C:\Deployment\ABCfolder\web.config")