Aaron
Aaron

Reputation: 1832

Simple Powershell command hangs Visual Studio 2010 in post-build event

I have a one line Powershell command that works perfectly well from the command line.

When I add it into my VS2010 project's post-build commands however, it goes to run, but then just hangs Visual Studio. It is almost as if Visual Studio is just sitting there waiting for the Powershell command to complete, i.e. the command line just needs to press enter to close the window or something.. Because CPU usage is dead low.

Do I have to do anything special to get a normal Powershell command to run happily from within the VS2010 post-build window?

The command is:

[io.file]::WriteAllText('C:\test.js', ((gc C:\test.js) -replace 'HELLO','GOODBYE' -join "`r`n"))

So within VS, I add the prefix to launch it via Powershell, like so:

Powershell [io.file]::WriteAllText('C:\test.js', ((gc C:\test.js) -replace 'HELLO','GOODBYE' -join "`r`n"))

Update

This ended up doing the trick. Thanks for everyone's great input.

Powershell -command "[io.file]::WriteAllText('C:\test.js', ((gc C:\.js) -replace 'HELLO','GOODBYE' -join \"`r`n\"))"

Upvotes: 2

Views: 1693

Answers (2)

Richard
Richard

Reputation: 109005

The post build steps are run by cmd.exe, so you need to ensure everything is escaped to be processed by PowerShell and not cmd.exe.

In your command, cmd.exe will try and perform the pipe and sc itself. A quick test with a text file passed to sc.exe (command line service control utility) indicates it shows help and waits for keyboard input when passed an invalid command line.

I suggest use of full cmdlet names as well to help avoid ambiguity.

The command should be something like (test in an interactive cmd.exe to confirm):

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -command "(get-content C:\www\js\test.min.js) -replace 'HELLO','GOODBYE' | set-content C:\www\js\test.min.js"

"

Upvotes: 3

stej
stej

Reputation: 29449

Try this:

gc C:\www\js\test.min.js -read 0

Or first store result from gc to a local variable and then call sc. There is open read handle that prevents PowerShell from opening the file for writing.

Upvotes: 1

Related Questions