我能';无法获取Powershell脚本以将多个更改保存到xml文件
我试图更改xml文件中的2个innertext,但在运行save命令时,它只保存其中一个更改。我不知道如何将这两个innertext都保存回xml文件 有人能帮我吗 这是我的密码:我能';无法获取Powershell脚本以将多个更改保存到xml文件,xml,powershell,select-xml,Xml,Powershell,Select Xml,我试图更改xml文件中的2个innertext,但在运行save命令时,它只保存其中一个更改。我不知道如何将这两个innertext都保存回xml文件 有人能帮我吗 这是我的密码: Clear-Host $Path = "C:\Program Files (x86)\Netcompany\GetOrganized Outlook Add-in 32-bit\Netcompany.Outlook.properties.config" $XpathAlias = "
Clear-Host
$Path = "C:\Program Files (x86)\Netcompany\GetOrganized Outlook Add-in 32-bit\Netcompany.Outlook.properties.config"
$XpathAlias = "/configuration/properties/configs/array/item/goAlias"
$XpathBaseUrl = "/configuration/properties/configs/array/item/baseUrl"
$CurrentAlias = Select-Xml -Path $Path -XPath $XpathAlias | Select-Object -ExpandProperty Node
$CurrentBaseUrl = Select-Xml -Path $Path -XPath $XpathBaseUrl | Select-Object -ExpandProperty Node
Write-Host $CurrentAlias.InnerText
Write-Host $CurrentBaseUrl.InnerText
Write-Host "Choose what GO Environment the Plugin should use:"
Write-Host
Write-Host " 0) Abort"
Write-Host " 1) Prod"
Write-Host
$validchoice = $false
while (-not $validchoice) {
$ChosenEnvironment = (Read-Host "Choose").Trim().ToUpperInvariant();
if ("0","1","2","3","4" -notcontains $ChosenEnvironment)
{
Write-Host -ForegroundColor Red "Invalid choice"
} else {
$validchoice = $true
}
}
Write-Host
$newsize = $null
switch ($ChosenEnvironment) {
"0" { Write-Host "Abort." }
"1" {
$CurrentBaseUrl.InnerText = "Prod";
$CurrentAlias.InnerText = "Prod";
$CurrentAlias.OwnerDocument.Save($Path);
$CurrentBaseUrl.OwnerDocument.Save($Path);
#Write-Host "GO miljø i Outlook plugin er sat til Prod miljøet"
}
}
您将加载和保存xml文件两次,每次保存只有一次更新,因为您已将xml加载到两个单独的变量中
$CurrentAlias = Select-Xml -Path $Path ..
$CurrentBaseUrl = Select-Xml ..
我将通过以下方式简化代码:
$Path = "C:\Program Files (x86)\Netcompany\GetOrganized Outlook Add-in 32-bit\Netcompany.Outlook.properties.config"
# load the XML file only once
$xml = [System.Xml.XmlDocument]::new()
$xml.Load($Path)
接下来是执行读取主机
和验证所做选择的地方
最后,在有效的选择选项中
# set the new innerText properties (on the one and only xml you have loaded before)
$xml.SelectSingleNode("/configuration/properties/configs/array/item/goAlias").InnerText = "Prod"
$xml.SelectSingleNode("/configuration/properties/configs/array/item/baseUrl").InnerText = "Prod"
# save the xml
$xml.Save($Path)