本文介绍了在每次部署后使用PowerShell自动增加应用程序.config的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是PowerShell的新手,我想在每次部署和之后更改XML文件中的版本。我指的是Unable to update a value in msbuild proj file using powershell。以下是XML内容:
<?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>
我正在尝试通过以下方式增加AppVersion修订版的价值:
$Test1QAMSBuildFile = 'D: estapplication.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)
运行此脚本后,PowerShell ISE引发错误:
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
如何使用PowerShell自动增加版本。
推荐答案
AppVersion
是<add>
节点的属性值,不是节点的名称。此外,您还希望提取节点的value
属性的值,而不是节点的innerText
属性的值。
,- node name
/
/ ,- attribute name
/ /
/ / ,- attribute value
/ / /
<add key="AppVersion" value="v0.1.7.21.31.144402">
something
</add>
`- inner text
在XPath表达式中选择如下属性:
//node[@attribute='value']
更改这两行:
$node = $Test1QABuildVersion.SelectSingleNode("/configuration/site/key/AppVersion")
$PropertyVersion= $node.InnerText
进入:
$node = $Test1QABuildVersion.SelectSingleNode("/configuration/site/add[@key='AppVersion']")
$PropertyVersion= $node.value
并按如下方式更新版本号:
$node.value = $newVersion
这篇关于在每次部署后使用PowerShell自动增加应用程序.config的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!