Every now and then I need to write some powershell, but almost always the time between these events is enough to forget almost everything about it.
This is the reason for this blog, where I try to collect every important things what I use when I’m shelling :)
Probably there are going to be further post containing similar snippets.
The caller script creates a nuspec XML file, modifies it and then creates a nuget package based on it.
There are two basic files what I’m going to use: one containing some functions and filters and another one calling them.
The caller:

# This is the way to get the location of the current file
$currentDir = Get-Location
# Not using Import-Module because it doesn't get actualized when changed
. (Join-Path $currentDir NugetFunctions.ps1)

$csprojFile = Create-NuspecFile
Create-Package $csprojFile

The functions:

# The following is a filter for pipes
filter NodesToRemove
    $namesToRemove = 'tags', 'releaseNotes'
    $contentsToRemove = 'DELETE_THIS_LINE'

    if($namesToRemove -contains $ -or $_.InnerText.Contains($contentsToRemove))
        return $_

# The following function is a pipeable function because of the 'process' keyword
function Remove-ChildNodes($xmlParentNode)
        $parentNodeName = $xmlParentNode.Name
        Write-Host "Children of $parentNodeName are going to be removed" 
        $tagName = $_.Name
        Write-Host "The $tagName gets removed"
    end { return $xmlParentNode }

function Create-NuspecFile()
    $currentDir = Get-Location
    . (Join-Path $currentDir ..\..\NuGet.exe) spec -f
    $csprojFile = Get-ChildItem -filter "*.csproj"
    $nuspecFileName = $csprojFile.BaseName + ".nuspec"
    [xml] $nuspecXml = Get-Content $nuspecFileName
    # The following statement does the same as the one below
    # $nodesToDelete = $nuspecXml.package.metadata.ChildNodes | ? { $namesToRemove -contains $ -or $_.InnerText.Contains($contentsToRemove) }
    $nodesToDelete = $nuspecXml.package.metadata.ChildNodes | NodesToRemove
    # The following statement does the same as the one below
    # $nodesToDelete | % { $nuspecXml.package.metadata.RemoveChild($_) > null }
    # The null is needed so that the return value doesn't get extended with the output of this run
    ($nodesToDelete | Remove-ChildNodes $nuspecXml.package.metadata) > null
    $destinationNuspecFilePath = Join-Path (Join-Path $currentDir "\Nuget\") $nuspecFileName

    return $csprojFile
    # This is the way to handle exceptions
        Write-Host "There was an error during the creation of the nuspec file!"
        Write-Host $_.errorid
        Write-Host $_.exception.message
        # You can also use 'continue' if you don't want to break the run

# The following is a commented function where the VerbosePreference is set
function Create-Package($csprojFile, [switch]$verbose)
    Creates a Nuget package to the csproj file.
    .PARAMETER $csprojFile
    The csproj file.

        $VerbosePreference = "Continue"

    $currentDir = Get-Location
    . (Join-Path $currentDir ..\..\NuGet.exe) pack $csprojFile.Name -BasePath Nuget -ExcludeEmptyDirectories

        Write-Host "There was an error during the creation of the nuget package!"
        Write-Host $_.ErrorID
        Write-Host $_.Exception.Message

The XML structure looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="">
        <summary />
        <tags>some tags</tags>

I’m always searching for how to add more help to a function

Get-Help about_comment_based_help

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s