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

时间:2018-04-03 07:03:04

标签: 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 个答案:

答案 0 :(得分:1)

代码的逻辑应该是:

  1. 将xml读入webXML
  2. 更改所需节点
  3. 将webXML写回文件
  4. 此代码应该有效:

    [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")