Search code examples
powershelldacpacsqlpackage

Calling another executable and capturing output


Using a PowerShell script I'm trying to execute SqlPackage.exe to update a database. The problem is that it spawns a new CMD window and I can't capture the output effectively.

Here is my script:

&$SqlPackage "/Action:Script", "/SourceFile:$($Environment.PackageFile)", "/Profile:$($Environment.PublishProfile)", "/OutputPath:$($Environment.ScriptFile)";

where SqlPackage = "SQLPackage\SqlPackage.exe";.

Strangely when I execute the script on a Windows 2008 R2 web server the output is inline with the PowerShell output, which is ideally what I want. On my Windows 7 machine it spawns a new CMD window.

How can I route the output to the PowerShell window all the time, or at least pipe to another file?

Edit

Full solution with waiting for process to finish:

$p = Start-Process $SqlPackage -ArgumentList @("/Action:Script", "/SourceFile:$($Environment.PackageFile)", "/Profile:$($Environment.PublishProfile)", "/OutputPath:$($Environment.ScriptFile)") -NoNewWindow -Wait -Passthru
$p.WaitForExit()

Solution

  • You should be able to get the result you are looking for if you use Start-Process with the -NoNewWindow parameter:

    Start-Process $SqlPackage -ArgumentList @("/Action:Script", "/SourceFile:$($Environment.PackageFile)", "/Profile:$($Environment.PublishProfile)", "/OutputPath:$($Environment.ScriptFile)") -NoNewWindow
    

    If you need to direct the output to a file you can also use the -RedirectStandardOutput / -RedirectStandardError parameters of Start-Process