Running PowerShell from Post-Build event ignoring ExecutionPolicy

A

Ann Brady

I have my ExecutionPolicy set to Unrestricted. I can verify this in the
registry at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell . I can verify this by opening PowerShell and asking Get-ExecutionPolicy.

However, when I run a powershell command from a Post-Build event, either
from MSBuild or Visual Studio 2008 I get the error "cannot be loaded because
the execution of scripts is disabled on this system. Please see "get-help
about_signing" for more details."

If I run the same command directly from PowerShell there is no error.

I had a WindowsPowerShell profile, I removed it. I had the same error from
MSbuild and Visual Studio on loading my profile as running the post-build
command. Again, PowerShell (and my profile) load correctly with the correct
ExecutionPolicy level from outside MSBuild\Visual Studio.

I have uninstalled and reinstalled .NET, Visual Studio, SP1 for .NET and
Visual Studio and PowerShell 1.0. I am on windows server 2003 R2 x64 sp2.

Other developers do not have a problem, there is clearly something wrong
with my environment but I don't know where to look next.

The command is:
set ProjectSource=$(ProjectDir)
set ScriptOutput=$(TargetDir)CreateScript.sql
powershell.exe -command "$(projectDir)CompileCreateDatabase.ps1"
$env:ProjectSource $env:ScriptOutput

Any ideas are appreciated.
 

My Computer

S

Shay Levy [MVP]

Hello Ann,

VS is probably launching the x64 version of PowerShell. x64 machines have
two PowerShell shortcuts, launch the x64 console and set the execution policy.
If you examine the property page for each shortcut you'll see the difference:

Windows PowerShell(32).lnk (x32) - %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Windows PowerShell.lnk (x64) - %SystemRoot%\sysWOW64\WindowsPowerShell\v1.0\powershell.exe

They also read/write to different sections in the registry.


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


AB> I have my ExecutionPolicy set to Unrestricted. I can verify this in
AB> the registry at
AB> HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsof
AB> t.PowerShell . I can verify this by opening PowerShell and asking
AB> Get-ExecutionPolicy.
AB>
AB> However, when I run a powershell command from a Post-Build event,
AB> either from MSBuild or Visual Studio 2008 I get the error "cannot be
AB> loaded because the execution of scripts is disabled on this system.
AB> Please see "get-help about_signing" for more details."
AB>
AB> If I run the same command directly from PowerShell there is no
AB> error.
AB>
AB> I had a WindowsPowerShell profile, I removed it. I had the same
AB> error from MSbuild and Visual Studio on loading my profile as
AB> running the post-build command. Again, PowerShell (and my profile)
AB> load correctly with the correct ExecutionPolicy level from outside
AB> MSBuild\Visual Studio.
AB>
AB> I have uninstalled and reinstalled .NET, Visual Studio, SP1 for .NET
AB> and Visual Studio and PowerShell 1.0. I am on windows server 2003
AB> R2 x64 sp2.
AB>
AB> Other developers do not have a problem, there is clearly something
AB> wrong with my environment but I don't know where to look next.
AB>
AB> The command is:
AB> set ProjectSource=$(ProjectDir)
AB> set ScriptOutput=$(TargetDir)CreateScript.sql
AB> powershell.exe -command "$(projectDir)CompileCreateDatabase.ps1"
AB> $env:ProjectSource $env:ScriptOutput
AB> Any ideas are appreciated.
AB>
 

My Computer

A

Ann Brady

Re: Running PowerShell from Post-Build event ignoring ExecutionPol

Shay,

That was exactly correct. I didn't realize I had to set the ExecutionPolicy
level for each.

Thanks.



"Shay Levy [MVP]" wrote:

> Hello Ann,
>
> VS is probably launching the x64 version of PowerShell. x64 machines have
> two PowerShell shortcuts, launch the x64 console and set the execution policy.
> If you examine the property page for each shortcut you'll see the difference:
>
> Windows PowerShell(32).lnk (x32) - %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
> Windows PowerShell.lnk (x64) - %SystemRoot%\sysWOW64\WindowsPowerShell\v1.0\powershell.exe
>
> They also read/write to different sections in the registry.
>
 

My Computer

Top