Public/Invoke-NuGetCommand.ps1

function Invoke-NuGetCommand
{
    <#
        .SYNOPSIS
            Invokes a NuGet command.
        .DESCRIPTION
            Invokes a NuGet command.
        .PARAMETER Command
            A string containing the NuGet command to invoke.
        .PARAMETER Target
            A string containing the target of the NuGet command to invoke.
        .PARAMETER NuGetPath
            A string containing the path to the NuGet executable.
        .PARAMETER Parameters
            An hashtable containing the additionnal parameters to specify to NuGet.
        .PARAMETER Help
            A switch to call help of the specified command.
        .INPUTS
        .OUTPUTS
            PsObject
            Returns the output that is generated by the invoked command (the value of the Command parameter).
        .EXAMPLE
            Invoke-NuGetCommand -Command "pack" -Target ".\package.nuspec" -Parameters @{ "NoDefaultExcludes" = $true ; "OutputDirectory" = "D:\packages" }
 
            Description
            -----------
            This example will build the package from the manifest, not excluding default content, and output it to D:\packages.
        .NOTES
            - This CmdLet does not stop on nuget error, output should be parsed.
            - This function is Windows compatible only
        .LINK
            https://docs.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Command,
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string] $Target,
        [Parameter(Mandatory = $false)]
        [alias("Options")]
        [hashtable] $Parameters, 
        [Parameter(Mandatory = $false)]
        [ValidateScript( { Test-Path $_ } )]
        [string] $NuGetPath = (Get-NuGetPath),
        [Parameter(Mandatory = $false)]
        [switch] $Help
    )
    
    try 
    {
        if ($NuGetPath -like "*.exe")
        {
            $NuGetExe = Split-Path $NuGetPath -Leaf
            $NuGetPath = Split-Path $NuGetPath -Parent
        }
        else { $NuGetExe = "nuget.exe" }

        $NugetCommand = $NuGetExe.Replace(".exe", "")
        $Expression = "./$($NugetCommand) $Command $Target"
        if ($Help) { $Expression += " -h" }
        else
        {
            if ($Parameters)
            {
                $Parameters.Keys | ForEach-Object {
                    if (($Parameters[$_] -eq $true) -or ($Parameters[$_] -eq $false))
                    {
                        if ($Parameters[$_]) { $Expression += " -$_" }
                    } 
                    else
                    {
                        $Expression += " -$_ '$($Parameters[$_])'"
                    }
                }
            }
        }
        Write-Verbose "Invoking command: $Expression"
        Push-Location $NuGetPath
        Invoke-Expression $Expression
    }
    catch
    {
        Write-Error $_
    }
    finally
    {
        Pop-Location
    }
}