Is it possible to set up a .NET project with a post build event to execute a powershell script? I am using this script to generate some files.
Also can I pass whethe
Instead of messing with system-wide settings and having to differentiate between 32 and 64-bit environments, a much easier and more reliable approach is to specify the ExecutionPolicy
in the call to PowerShell, as follows:
C:\Users\xyz>PowerShell -ExecutionPolicy Unrestricted
PS C:\Users\xyz> Get-ExecutionPolicy
Unrestricted
PS C:\Users\xyz> exit
C:\Users\xyz>PowerShell -ExecutionPolicy RemoteSigned
PS C:\Users\xyz> Get-ExecutionPolicy
RemoteSigned
Note in the above code how calling Get-ExecutionPolicy
tells you the current mode. Also note how this mode is specified in the call to PowerShell itself, which can be combined with a script filename:
test.ps1 contents:
echo ('The current policy is ' + (Get-ExecutionPolicy)).ToString()
Calling test.ps1 with Unrestricted
policy on a system having scripts disabled:
C:\Users\xyz>PowerShell -ExecutionPolicy Unrestricted -file test.ps1
The current policy is Unrestricted
Also note that the above call does not require admin rights, so it can be called in Visual Studio's Pre-Build Step or similar.