问题
I am new to powershell and I wanted to change version in an xml file after every deployment and. I referred to Unable to update a value in msbuild proj file using powershell. Below is the xml content:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<site>
<add key="ShowAppConf" value="true"/>
<add key="site.IsCsrfCheck" value="false" />
<add key="EnableDeviceFileAuthentication" value="false" />
<add key="EnableFileAuthentication" value="false" />
<add key="AppVersion" value="v0.1.7.21.31.144402" />
</site>
</configuration>
and I am trying to increase value of revision version of AppVersion by using:
$Test1QAMSBuildFile = 'D:\test\application.config.xml'
[xml]$Test1QABuildVersion = Get-Content $Test1QAMSBuildFile
$node = $Test1QABuildVersion.SelectSingleNode("/configuration/site/key/AppVersion")
$PropertyVersion= $node.InnerText
$UpdateVersion= $PropertyVersion.split(".")
$UpdateVersion[4] = (($UpdateVersion[4] -as [int]) + 1).ToString()
$newVersion = $UpdateVersion -join '.'
$node.InnerText = $newVersion
$Test1QABuildVersion.Save($Test1QAMSBuildFile)
After running this script powershell ise throws error:
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $UpdateVersion= $PropertyVersion.split(".")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Cannot index into a null array.
At line:6 char:1
+ $UpdateVersion[4] = (($UpdateVersion[3] -as [int]) + 1).ToString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
The property 'InnerText' cannot be found on this object. Verify that the property exists and can be set.
At line:8 char:1
+ $node.InnerText = $newVersion
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
How do I increase version automatically using powershell.
回答1:
AppVersion is the value of an attribute of an <add> node, not the name of a node. Also, you want to extract the value of the node's value attribute, not the node's innerText.
,- node name
/
/ ,- attribute name
/ /
/ / ,- attribute value
/ / /
<add key="AppVersion" value="v0.1.7.21.31.144402">
something
</add> \
`- inner text
Attributes are selected in XPath expressions like this:
//node[@attribute='value']
Change these two lines:
$node = $Test1QABuildVersion.SelectSingleNode("/configuration/site/key/AppVersion")
$PropertyVersion= $node.InnerText
into this:
$node = $Test1QABuildVersion.SelectSingleNode("/configuration/site/add[@key='AppVersion']")
$PropertyVersion= $node.value
and update the version number like this:
$node.value = $newVersion
来源:https://stackoverflow.com/questions/26606370/increase-version-of-application-config-automatically-using-powershell-after-ever