我无法让我的 Powershell 脚本将多个更改保存到我的 xml 文件
I can't get my Powershell script to save multiple change to my xml file
我尝试更改 xml 文件中的 2 个内部文本,但当我 运行 保存命令时它只会保存其中一个更改。我想不出将两个内部文本都保存回 xml 文件。
有没有人可以帮助我?
这是我的代码:
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)
接下来是 Read-Host
和验证所做选择的地方
最后,在有效的选择选项里面
# 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)
我尝试更改 xml 文件中的 2 个内部文本,但当我 运行 保存命令时它只会保存其中一个更改。我想不出将两个内部文本都保存回 xml 文件。
有没有人可以帮助我?
这是我的代码:
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)
接下来是 Read-Host
和验证所做选择的地方
最后,在有效的选择选项里面
# 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)