Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么powershell不保存xml文件?_Xml_Powershell_Powershell 2.0 - Fatal编程技术网

为什么powershell不保存xml文件?

为什么powershell不保存xml文件?,xml,powershell,powershell-2.0,Xml,Powershell,Powershell 2.0,我正在使用powershell查找并替换xml文件属性中的字符串。字符串被正确替换[我通过显示 “在替换$($Item.Node.$attribute)之后” ]但是在配置文件中它没有反映出来。似乎没有正确地使用最新值保存。我尝试了所有的可能性,但奇怪的是,这并不能挽救我的生命。我正在使用powershell版本2 function Set-XMLAttribute { [CmdletBinding()] [OutputType([int])] Param (

我正在使用powershell查找并替换xml文件属性中的字符串。字符串被正确替换[我通过显示

“在替换$($Item.Node.$attribute)之后”

]但是在配置文件中它没有反映出来。似乎没有正确地使用最新值保存。我尝试了所有的可能性,但奇怪的是,这并不能挽救我的生命。我正在使用powershell版本2

function Set-XMLAttribute
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # webconfig file full path
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]$Path,

        # Param2 help description
        [string] $xPath,

        [string] $attribute,

        [string] $valueToFind,

        [string] $ValueToReplace
    )

    Begin { }
    Process
    {
        try 
        {

            If ( Test-path -Path $Path)
            {

                #Loading the file
                $xml = New-Object -TypeName XML
                $xml.Load($Path)

                # Getting all the values
                $Items  = Select-Xml -Path $Path -XPath $xPath
                ForEach ($Item in $Items) 
                {   
                    $Item.Node.$attribute
                    If ($($Item.Node.$attribute).Contains($valueToFind) ){
                        "Before Replace the value $($Item.Node.$attribute)"

                        $Item.Node.$attribute = $($Item.Node.$attribute) -Replace "$valueToFind","$ValueToReplace"


                    }
                    Else {

                        Write-Error "$valueToFind not available"

                    }
                }
                "After replace $($Item.Node.$attribute)"
                "Saving the $Path"
                 $xml.Save((Resolve-Path "$Path"))
            }
            Else 
            {
                Write-Error "$Path is not found"
            }
        }
        catch 
        {

            $_.Exception.Message
            $_.Exception.ItemName
            Write-Error "Set-XMLAttribute function failed."
        }
    }
    End {}
} # End Function Update-XMLAttribute
Set-XMLAttribute -Path "E:\Pshscript\web.Config" -xPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceMetadata" -attribute "externalMetadataLocation" -valueToFind "http" -ValueToReplace "https"

您看到这种情况的原因是您正在对$XML进行更改,它是内存中的一个对象。您必须将这些设置保存到一个文件中以反映它,因此您可以通过调用$XML的.save()方法来实现这一点。我认为这里的问题是,您试图使用以下行进行保存:

$xml.Save((Resolve-Path "$Path"))
我认为这个语法导致了这个问题,因为Resolve Path返回一个带有Path属性的对象。试试这个

$xml.Save((Resolve-Path "$Path").Path)

这不是对这里的ops代码所做的吗?
$xml.Save((解析路径“$Path”))
?@FoxDeploy我已经做了“$xml.Save((解析路径“$Path”))”)。怎么了?啊,我重新阅读了你的代码,同意你的看法。我已经编辑了我的答案,请尝试一下。@FoxDeploy我尝试了你的最新更新,但仍然不起作用。完全奇怪嘿,伙计,你能在pastebin上发布其中一个文件(webconfig文件)让我进一步解决这个问题吗?如果没有相同类型的文件,我很难复制这里应该发生的事情。您是否以管理员身份打开PS控制台?文件的写入权限是否正确?@eduardata是,它以管理员身份打开