Powershell Command post-build VS2010 - visual-studio-2010

I have a command in my post-build event command line in Visual Studio 2010
Powershell -command .'$(SolutionDir)Powershell\MoveFiles.ps1'
And when the event runs, I get an error that the command "exited with code 1."
However when I run the same command on the command line (see below), with an actual directory instead of the VS2010 macro, it works perfectly.
Powershell -command .'C:\TFS\MyProject\Main\Source\Powershell\MoveFiles.ps1'
So it seems that the problem lies with how VS2010 is executing the command.
What could be causing this problem?
[UPDATE]
I have also tried changing the post-build event to:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell -command .'C:\TFS\MyProject\Main\Source\Powershell\MoveFiles.ps1' and I get the same result as described above. It works when run from the command line but not from VS 2010.

Two things:
Use the -file parameter instead of the -command parameter.
Use double quotes.
powershell.exe -file "$(SolutionDir)Powershell\MoveFiles.ps1"

If you are running on a 64-bit OS, you will need to specify the full path to the 64-bit version of powershell since Visual Studio is a 32-bit app.
There is an answer in this question that has a workaround for the problem.

Related

Executing Powershell from qmake hangs indefinitely

I am trying to execute a Powershell command from within qmake. A simplified .pro file looks like this:
$$system(powershell -NonInteractive -Command \"[long]((date).touniversaltime()-[datetime]'1970-01-01').totalmilliseconds\")
However, running qmake with this file or opening it in QtCreator will create a powershell.exe process which hangs indefinitely. Running Powershell from the command line works without problems, the command prints the expected timestamp and exits immediately.
What could be causing this hang and how to resolve it?
Tested on Windows 7 x64 with Powershell version 2.0 and qmake version 3.1 / Qt 5.9.8.
Don't use the quote.
The possible command is:
$$system(powershell -Command Get-Date -Format "yyyy.MM.dd.HH.mm")

Visual Studio: Run a post build event in the background

I have a powershell script that I want to run when the post-build event is triggered in Visual Studio. However, it is important that the script runs in the background and does not pause the build process while it is running. The other requirement is that I must be able to pass in the project directory with spaces in the directory name.
I have been having trouble getting both to happen at the same time. After much trial and error, I have found a complex solution to running a powershell script in the background, but it doesn't work with spaces in the directory name. Here is what I am currently doing, but I am quite open to something completely different as this seems overly complicated:
In the post-build event in Visual Studio:
powershell -ExecutionPolicy Unrestricted start-process " $(ProjectDir)scripts\post-build.cmd" -ArgumentList $(TargetDir),$(ConfigurationName),$(ProjectDir)
And here is the post-build.cmd:
START /B CMD /C CALL "Powershell.exe" -ExecutionPolicy Unrestricted -file %3scripts\post-build.ps1 %1 %2 %3
I have tried just adding quotes around the arguments, but I keep getting various errors (i.e. missing terminator '"') from Powershell when I do so.
Please forgive my ignorance, I am new to Powershell and Visual Studio.
You can run a Powershell script directly from within a post build event. For example, I’ve done that to digitally sign an executable after the build completes. Here’s the command line I used in my post build step:
powershell.exe -file "$(SolutionDir)sign.ps1" "$(TargetPath)" "..\..\installs\certs\DigitalSignature.pfx"
Notice that there's no need to use a batch file.

Passing current file name to Visual Studio debug command line arguments

I'm creating PowerShell module in VS 2012. So for comfortable debugging in debug project properties I set Start action to start external program PowerShell.exe and in command line arguments I want to add
-Command { Import-Module [MyDllFileName] }. What should I write instead of [MyDllFileName]? There should be my compiled dll.
The answer propsosed in the linked question is still pretty much valid, but you have to think it through a bit.
First of all the actual answer remains: it's simply not possible to get the assembly name onto the debug command line using the project settings.
Second there are a couple of things you can do however:
The debugger command line is stored in the projectname.vcxproj.user file as the LocalDebuggerCommandArguments property. Write a script/extension/... to set that property to $(TargetPath) and off you go.
Based on the solution propsed in the other question: use an external tool with something like devenv /DebugExe powershell.exe - Command { Import-Module $(TargetPath) }.
Like 2, but place a DebugBreak() statement somwhere in your dll and just launch PowerShell, it will coma and ask to attach the debugger when it sees DebugBreak/add.

Use PowerShell for Visual Studio Command Prompt

In a serious intiative to migrate all my command line operations to PowerShell, I would like to avoid using the old fashioned command console for anything. However, the Visual Studio Command prompt has various environment variables and path settings not found in the default command prompt. How could I create a 'Visual Studio PowerShell' with those same settings?
You can use for example this script to import Visual Studio command prompt environment, see the examples in the script documentation comments, e.g. for Visual Studio 2010:
Invoke-Environment '"%VS100COMNTOOLS%\vsvars32.bat"'
Having done that in the beginning of a PowerShell session (from your profile or manually), you get what you ask for in this PowerShell session.
Or you can use the solution provided by Keith Hill in this answer.
have a look at PowerConsole
PowerConsole has been incorporated into NuGet http://nuget.codeplex.com/. You get PowerShell inside Visual Studio and a package management system.
I use this script that I call Initialize-VisualStudio.ps1, i call it in my profile with dot source, to set the environment variables need it, in my actual session:
param([switch]$ArquitectureX86)
if($ArquitectureX86)
{ $arq= "x86"}
else
{ $arq="x64"}
pushd 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC'
cmd /c "vcvarsall.bat $arq&set" |
foreach {
if ($_ -match "=") {
$v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])";
}
}
popd
What I do is create a simple cmd batch command script that looks like this:
call "%VS80COMNTOOLS%vsvars32.bat"
powershell
Then I create a shortcut that invokes this through cmd. The shortcut target looks like:
%windir%\System32\cmd.exe /k "SetupPSBuildEnvironment.cmd"
If you want the console to look like the powershell console, just modify the Layout to your liking in the shortcut properties.
First, check the contents of this folder:
C:/ProgramData/Microsoft/VisualStudio/Packages/_Instances/
There'll be another folder in it with a name consisting of hex digits (e.g. 2a7a9ed6, but that will vary for different MSVC versions). I'll refer to it as <instance_id>.
Then run from PS:
Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell <instance_id> -DevCmdArguments '-arch=x64'
Or you can create a shortcut with the following target:
<path to your powershell.exe> -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell <instance_id> -DevCmdArguments '-arch=x64'}"
Obviously, drop -arch=x64 if you need x86 toolset.
Works for me on Windows 10 with MS Build Tools 16.9.5 and PowerShell 5.1.19041,7.1.3

How to run a PowerShell script

How do I run a PowerShell script?
I have a script named myscript.ps1
I have all the necessary frameworks installed
I set that execution policy thing
I have followed the instructions on this MSDN help page
and am trying to run it like so:
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)
which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.
The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
Prerequisites:
You need to be able to run PowerShell as an administrator
You need to set your PowerShell execution policy to a permissive value or be able to bypass it
Steps:
Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear
Navigate within PowerShell to the directory where the script lives:
PS> cd C:\my_path\yada_yada\ (enter)
Execute the script:
PS> .\run_import_script.ps1 (enter)
Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.
Or you could even run your PowerShell script asynchronously from your C# application.
If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\Test.ps1
NOTE: Here Test.ps1 is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0, this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.
Open PowerShell in administrator mode
Run: set-executionpolicy unrestricted
Open a regular PowerShell window and run your script.
I found this solution following the link that was given as part of the error message: About Execution Policies
Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.
Using cmd (BAT) file:
#echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator:
Make a shortcut pointed to the command prompt (I named it
Administrative Command Prompt)
Open the shortcut's properties and go to the Compatibility tab
Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
An easy way is to use PowerShell ISE, open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script to Powershell.exe
Set Arguments to -File "C:\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.
If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
Give the path of the script, that is, path setting by cmd:
$> . c:\program file\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
You can run from cmd like this:
type "script_path" | powershell.exe -c -
Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

Resources