Public/Invoke-ISNuGetCommand.ps1

function ModuleRoot {$MyInvocation.ScriptName | Split-Path -Parent}

$script:PublicCmdlets = Join-Path (ModuleRoot | Split-Path -Parent) "Public"
$script:PrivateCmdlets = Join-Path (ModuleRoot | Split-Path -Parent) "Private"

. $PrivateCmdlets\Get-NuGetPath.ps1

function Invoke-ISNuGetCommand {
    <#
        .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
    #>

    [CmdletBinding()]
    [OutputType([string[]])]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('list', 'install', 'setApiKey', 'sources', 'config', 'locals', 'update')]
        [string]$Action,

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

    begin {
        $LibraryPath = Join-Path $env:APPDATA "\IntelliSearch\"
        $NUGET_EXE = Get-NuGetPath -LibraryDirectory $LibraryPath
    }

    process {
        $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)
        
        try {
            $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
        }
    }

    end {

    }
}