0
我们正在为我们所有的csproject做延迟签名。打开每个csproject文件来完成这项任务非常耗时,因此我决定编写一个PowerShell脚本。如何添加csproj的Propertygroup?
function New-XMLNode
{
[CmdletBinding()]
[OutputType([string])]
Param
(
# webconfig file full path
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]$Path,
[string] $xPath,
[string] $node,
[string] $LogFile = "$Env:Temp\NewXMLNode.log"
)
try
{
if (-not (Test-Path -Path $Path))
{
throw [System.IO.FileNotFoundException] "$Path not found."
}
Else
{
$xml = New-Object -TypeName XML
$xml.Load($Path)
# Getting all the values
$Items = Select-Xml -XML $xml -XPath $xPath
If ($Items -ne $null) # If xpath is valid
{
ForEach ($Item in $Items)
{
[System.Xml.XmlDocumentFragment] $newnode = $xml.CreateDocumentFragment()
$newnode.InnerXml = $node
$Item.Node.AppendChild($newnode)
}
}
else # if xpath is not valid then log the error
{
Write "$(Get-Date -Format $DateTimeFormat) ## ERROR ## $($MyInvocation.MyCommand.Name) ## Given xpath $xpath for xml $Path is not valid" | Out-File $LogFile -Append
}
$xml.Save($Path)
}
}
catch
{
Write $_.Exception.ErrorRecord | Out-File -FilePath $LogFile -Append
throw "$(Get-Date -Format $DateTimeFormat) ## ERROR ## $($MyInvocation.MyCommand.Name) ## $_ "
}
}
Function Set-StrongNameSign
{
Param
(
$Path="M:\MyProject\MyProject.csproj"
)
$ns = @{ dns = "http://schemas.microsoft.com/developer/msbuild/2003" }
New-XMLNode -Path $Path -xPath "/$ns:Project" -node '<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>'
}
Set-StrongNameSign
当运行这个我得到以下错误。
## ERROR ## New-XMLNode ## Exception calling "AppendChild" with "1" argument(s): "This document already has a 'DocumentElement' node." At line:51 char:13 + throw "$(Get-Date -Format $DateTimeFormat) ## ERROR ## $($MyInvocati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (02/26/2016 11:1...lement' node." :String) [], RuntimeException + FullyQualifiedErrorId : 02/26/2016 11:17:33 ## ERROR ## New-XMLNode ## Exception calling "AppendChild" with "1" argument(s): "This document already has a 'DocumentElement' node."
如何解决这个问题?我需要添加更多的propertygroup和一个Itemgroup。