Public/Invoke-ISNuGetCommand.ps1

<#
.SYNOPSIS
    Invokes a NuGet command
.DESCRIPTION
    The Invoke-ISNuGetCommand cmdlet is a wrapper around NuGet.exe. Running the cmdlet will allow you to run various forms of NuGet commands.
    The output from the cmdlet will be the exact same as a running a regular instance of NuGet.exe with the same parameters.
    If an argument does not require a parameter, pass $null as the parameter.
.EXAMPLE
        $NuGetArguments = @{
            "SomePackageName" = $null
            '-NonInteractive' = $null
            -OutputDirectory' = C:\Temp\
        }
        Invoke-ISNuGetCommand -Action "install" -Arguments $NugetCommand
.INPUTS
    String, Hashtable
.OUTPUTS
    String
#>

function Invoke-ISNuGetCommand
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('list', 'install', 'setApiKey', 'sources', 'config', 'locals', 'update')]
        [string]$Action,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [hashtable]$Arguments
    )

    try
    {
        $argArr = @()
        $argArr += $Arguments.GetEnumerator() | Sort-Object Value | ForEach-Object {
            if (-not $_.Value)
            {
                '"{0}"' -f $_.Key
            }
            else
            {
                '-{0} "{1}"' -f $_.Key, $_.Value
            }
        }
        $argString = $argArr -join ' '

        $stdOutTempFile = [System.IO.Path]::GetTempFileName()
        $stdErrTempFile = [System.IO.Path]::GetTempFileName()

        $startProcessParams = @{
            FilePath               = $NUGET_EXE
            ArgumentList           = "$Action $argString"
            RedirectStandardError  = $stdErrTempFile
            RedirectStandardOutput = $stdOutTempFile
            Wait                   = $true
            PassThru               = $true
            NoNewWindow            = $true
        }

        Write-Verbose -Message ("Running command: {0} {1}" -f $startProcessParams.FilePath, $startProcessParams.ArgumentList)

        $cmd = Start-Process @startProcessParams
        if ($cmd.ExitCode -ne 0)
        {
            throw (Get-Content -Path $stdErrTempFile -Raw)
        } 
        else 
        {
            Get-Content -Path $stdOutTempFile -Raw
        }
    } 
    catch
    {
        $PSCmdlet.ThrowTerminatingError($_)
    } 
    finally
    {
        Remove-Item -Path $stdOutTempFile, $stdErrTempFile -Force -ErrorAction:SilentlyContinue
    }
}