Calling another executable and capturing output - powershell

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()

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

Related

Powershell script is not installing an exe while i try it runs through TFS Build

I have a powershell script which will install an exe. It works fine when i try it from a powershell ISE console, but it fails when i try it from TFS build step.
Note : My TFS user, agent user and triggering user are same.
Could anybody can shed some light on this issue ?
Start-Process -FilePath $installerFileName -Verb "runas" -ArgumentList $parameter -Wait
The above code is using to install the exe. $parameter is a list of custom parameters.
I am getting below error
[WixSession.GetSession][GetSessionValues]exception: Value cannot be null. Parameter name: s
What kind of exe are you trying to install? Dose that support silent installation? If UI pop up during the installation, then the agent needs to run in interactive mode.
Test on my side with below command to install/unistall the notepad++ with service mode, everyting works as expected:
start-process -FilePath "D:\Software\npp.7.5.8.Installer.x64.exe" -ArgumentList '/S' -Verb runas -Wait
And:
start-process -FilePath "C:\Program Files\Notepad++\uninstall.exe" -ArgumentList '/S' -Verb runas -Wait

Calling a powershell script from InstallShield project

I'm having a weird problem.
I have an InstallShield project (which creates setup.exe) that contains a custom action item - calling a powershell script.
All the script does is to install 3 adobe reader updates (1 exe file and 2 msp files) on top of the already installed Adobe Reader 11.0.0.
When I'm calling the script my self - it works fine.
However, after the setup.exe finishes, it seems like only one update (the exe file) was really installed (the adobe reader version after the install is 11.00.10 which is the result of running only the exe file..).
All 3 adobe updates sit in the same folder and the powershell script first sets it location to this folder.
When running the updates manually after the installation - it also works fine and updates it to 10.00.22 (what it should be).
Any ideas why is this happening?
Here's my powershell script:
Set-Location "C:\myProject\adobeUpdates"
Start-Process .\AdbeRdr11010_en_US.exe -ArgumentList '/q /norestart /sPB /rs /msi' -WindowStyle hidden -Wait
ping 1.1.1.1 -n 1 -w 10000 # Tried to add a delay but wasn't helpful
Start-Process -FilePath “AdbeRdrUpd11021.msp” -ArgumentList '/qn' -Wait
Start-Process -FilePath “AdbeRdrUpd11022_incr.msp” -ArgumentList '/qn' -Wait
Thank you very much
Solved it, this is the working script:
Set-Location "C:\myProject\adobeUpdates"
Start-Process .\AdbeRdr11010_en_US.exe -ArgumentList '/q /norestart /sPB /rs /msi' -WindowStyle hidden -Wait
ping 1.1.1.1 -n 1 -w 10000
Start-Process .\AdbeRdrUpd11021.msp -ArgumentList '/qn' -Wait
Start-Process .\AdbeRdrUpd11022_incr.msp -ArgumentList '/qn' -Wait
I'm not sure what is the different and would love someone to explain but anyway it works just fine now.

Stopping a Sub process without closing the Service

I am using PowerShell start-process command to run a ps1 file as a service. Following is the line to start this process:
start-process PowerShell.exe -arg C:\scripts\scrtip_name.ps1 -WindowStyle Hidden
How do I stop "JUST THIS EXECTUTION" without completely shutting down PowerShell service, since it might be used for other processes at the same time.
You can get the PID if you use Start-Process with the -PassThru flag like this:
$app = start-process PowerShell.exe -arg C:\scripts\scrtip_name.ps1 -WindowStyle Hidden - PassThru
And then close the process like this:
Stop-Process $app.Id
Regards

Run powershell in new window

I would like to run new powershell window with parameters. I was trying to run following:
powershell -Command "get-date"
but everything happens in same console. Is there a simple way to do this?
To open a new PowerShell Window from PowerShell:
Start-Process PowerShell
Or my personal favorite:
Start-Process PowerShell -WindowStyle Maximized
Then you could typeGet-Datewithout having to deal with the -ArgumentList's tendency to close itself. I still haven't found a way to open a new PowerShell process with the -ArgumentList Parameter, without it immediately closing after it runs. For Instance:
Start-Process PowerShell -ArgumentList "Get-Date"
or simply
Start-Process PowerShell -ArgumentList Get-Date
Will Close Immediately after running the process.
In order to get it to wait before closing you could add:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Since the -Wait Parameter seems to do nothing at all in this case.
FYI - PowerShell Suggested Syntax is actually:
Start-Process -FilePath "powershell.exe"
But since PowerShell is a standard Windows Application in the %SystemRoot%\system32 Environment Variables the command line(s) should recognize a simple
Powershell
Command
Use the start command. In a CMD prompt, try:
start powershell -noexit -command "get-date"
For Start/Run (or Win+r) prompt, try:
cmd /c start powershell -noexit -command "get-date"
The -noexit will tell Powershell to, well, not to exit. If you omit this parameter, the command will be executed and you are likely to just see a glimpse of a Powershell window. For interactive use, this is a must. For scripts it is not needed.
Edit:
start is an internal command for CMD. In Powershell it is an alias for Start-Process. These are not the same thing.
As for why the window is black, that's because the shortcut for Powershell.exe is configured to set the background blue.
To call a PowerShell (PS) script in a second terminal window without exiting, you can use a script similar to:
Start-Process PowerShell -ArgumentList "-noexit", "get-date"
or if you need to run another script from a specific location:
Start-Process PowerShell -ArgumentList "-noexit", "-command .\local_path\start_server.ps1"

PowerShell - Start-Process and Cmdline Switches

I can run this fine:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
start-process $msbuild -wait
But when I run this code (below) I get an error:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo"
start-process $msbuild -wait
Is there a way I can pass parameters to MSBuild using start-process? I'm open to not using start-process, the only reason I used it was I needed to have the "command" as a variable.
When I have
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo
on a line by itself, how does that get handled in Powershell?
Should I be using some kind of eval() kind of function instead?
you are going to want to separate your arguments into separate parameter
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
$arguments = "/v:q /nologo"
start-process $msbuild $arguments
Using explicit parameters, it would be:
$msbuild = 'C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe'
start-Process -FilePath $msbuild -ArgumentList '/v:q','/nologo'
EDIT: quotes.
Warning
If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete.
cmd> PowerShell
PS> Start-Process cmd.exe -Wait
Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window:
cmd2> PowerShell
PS> Start-Process cmd.exe -Wait
PS>
The 2nd instance of PowerShell no longer honors the -Wait request and ALL background process/jobs return 'Completed' status even thou they are still running !
I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request.
It appears that any PowerShell which is a 'win32 job' of cmd.exe fails to honor the wait request.
I ran into this with PowerShell version 3.0 on windows 7/x64
I've found using cmd works well as an alternative, especially when you need to pipe the output from the called application (espeically when it doesn't have built in logging, unlike msbuild)
cmd /C "$msbuild $args" >> $outputfile
Unless the OP is using PowerShell Community Extensions which does provide a Start-Process cmdlet along with a bunch of others. If this the case then Glennular's solution works a treat since it matches the positional parameters of pscx\start-process : -path (position 1) -arguments (positon 2).