Post build event execute PowerShell

后端 未结 5 1086
南旧
南旧 2020-11-27 12:04

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

5条回答
  •  难免孤独
    2020-11-27 12:57

    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.

提交回复
热议问题