2014-10-28 64 views
2

我是PowerShell的新手,我想在每次部署和更新後都在xml文件中更改版本。我提到Unable to update a value in msbuild proj file using powershell。下面是XML內容:在每次部署之後使用powershell自動增加application.config版本

<?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:\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) 

運行此腳本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自動增加版本。

回答

1

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 
+0

Thankyou @Ansgar Wiechers。這現在完美。 – 2014-10-28 12:00:20