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

无法在powershell-WinRM中编辑xml文件,powershell,winrm,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)

我试图编辑一个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"

代码的逻辑应为:

将xml读入webXML 更改所需的节点 将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")

请检查服务器上安装的.NET版本,并检查那里的Powershell版本。将两者更新到电脑的同一位置。出现问题。'$mainXml'是一个XmlElement,'$webXml'是一个XmlDocument。您正在告诉元素保存文档。您应该告诉文档保存自己。$webXML$mainXml在这种情况下,该命令是什么,我对@palledue的解释感到困惑。您可以使用UNC路径访问xml文件吗?[xml]$webXML=Get Content\\ServerName\C$\Deployment\ABCfolder\web.config