Powershell 在Nuget uninstall.ps1期间删除PropertyGroup

Powershell 在Nuget uninstall.ps1期间删除PropertyGroup,powershell,nuget,Powershell,Nuget,我有一个Nuget包,它在安装期间向项目XML中添加了一些MSBuild条目。这很好,产生了以下内容(以及其他条目): 这些方法都没有任何效果;脚本运行时不会出错,但不会删除属性元素 通过PowerShell删除MSBuild属性的正确方法是什么?我提出了以下建议: param($installPath, $toolsPath, $package, $project) Set-StrictMode -Version Latest function RemoveMatchingProperty

我有一个Nuget包,它在安装期间向项目XML中添加了一些MSBuild条目。这很好,产生了以下内容(以及其他条目):

这些方法都没有任何效果;脚本运行时不会出错,但不会删除属性元素


通过PowerShell删除MSBuild属性的正确方法是什么?

我提出了以下建议:

param($installPath, $toolsPath, $package, $project)

Set-StrictMode -Version Latest

function RemoveMatchingPropertyGroups($buildProject, $property)
{
    foreach ($pg in $buildProject.Xml.PropertyGroups)
    {
        $firstChild = $pg.FirstChild.Name
        if ($property -eq $firstChild)
        {
            Write-Host "Removing old $firstChild"
            try
            {
                $pg.Parent.RemoveChild($pg)
            }
            catch
            {
            }
        }
    }
} 

Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$buildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1

RemoveMatchingPropertyGroups $buildProject "JsCompilationToolsPath"
基本上需要加载msbuild引擎dll,然后循环所有属性组,如果
PropertyGroup.FirstChild.Name
匹配,则可以删除该属性组。这是通过调用
PropertyGroup.Parent.RemoveChild()
来完成的,
Parent
需要在那里,因为您想要删除PropertyGroup本身,而不仅仅是属性

$msBuildProject.Properties | Where-Object { $_.Name -eq "JsCompilationToolsPath" } | ForEach-Object { $msBuildProject.RemoveProperty($_) }
param($installPath, $toolsPath, $package, $project)

Set-StrictMode -Version Latest

function RemoveMatchingPropertyGroups($buildProject, $property)
{
    foreach ($pg in $buildProject.Xml.PropertyGroups)
    {
        $firstChild = $pg.FirstChild.Name
        if ($property -eq $firstChild)
        {
            Write-Host "Removing old $firstChild"
            try
            {
                $pg.Parent.RemoveChild($pg)
            }
            catch
            {
            }
        }
    }
} 

Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$buildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1

RemoveMatchingPropertyGroups $buildProject "JsCompilationToolsPath"