后期构建事件执行powershell

ama*_*eur 76 .net c# msbuild powershell

是否可以使用post build事件设置.net项目来执行powershell脚本?我正在使用此脚本生成一些文件.我也可以通过它是脚本的调试版本还是发布版本.这方面的一个例子很棒.

JPB*_*anc 114

这是一个例子:

首先:您必须意识到PowerShell必须配置为执行脚本.以下行允许PowerShell执行脚本:

Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)

特别值得一提的是:如果您运行的是64位系统,则需要注意"devenv.exe "Visual Studio 2010可执行文件是32Bits exe这一事实,因此您需要允许PowerShell 32执行脚本.

到这里后,您可以进入项目属性并配置post build,如下所示(抱歉用法语):

在VS 2010中发布

例如 :

使用PowerShell进行postbuild的示例

这是文件' psbuild.ps1',它test.txt在目标路径中创建一个' ',其中包含配置名称.我提出了不同的方法来调试你的postbuild脚本(消息框,声音,输出上的消息)

param ([string]$config, [string]$target)

#[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[void][System.Windows.Forms.MessageBox]::Show("It works.")
#[Console]::Beep(600, 800)
#Write-Host 'coucou'
set-content $target -Value $config -Force
Run Code Online (Sandbox Code Playgroud)

  • 哇,这真是一个答案!:) (9认同)
  • 好答案.我只想补充一点,你不应该将执行策略设置为不受限制,而是设置为remotesigned.Unrestricted允许任何脚本执行,而remotesigned需要使用可信密钥签名下载的脚本. (8认同)
  • 如果您使用:c:\ windows\sysnative\windowspowershell\v1.0\powershell.exe作为路径,将调用64位版本的powershell.%systemroot%\ sysnative是一个特殊的别名,用于告知重定向器停止重定向,并允许实际访问%systemroot%\ system32. (5认同)
  • 您是否测试在Fat32驱动器上下载.PS1文件或使用基本cmdline FTP下载带有"remotesigned"的.PS1文件?这不是一种"烟雾缭绕的安全"吗? (2认同)
  • +1是针对64位系统的特别说明-我一直在发疯,直到读到我还需要允许在32位PowerShell中执行。谢谢! (2认同)

Ari*_*awa 13

命令Set-ExecutePolicy,当前会话下的临时设置执行策略.如果你在powershell中设置它并在vs中运行post build命令,你仍然会被允许.所以先设置,然后像下面那样运行你的ps1脚本

powershell -ExecutionPolicy Unrestricted $(ProjectDir)Deploy.ps1 -ProjectDir $(ProjectDir) -TargetPath $(TargetPath)
Run Code Online (Sandbox Code Playgroud)


Nar*_*yal 9

在从visual studio调用power-shell脚本之前,将ExecutionPolicy设置为不受限制的power-shell窗口,如下所示......

Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: RemoteSigned;
Run Code Online (Sandbox Code Playgroud)

然后以下列方式调用power-shell脚本......

(无需传递完整的"powershell.exe"文件路径)

powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后在脚本中,你总是可以读取这样的参数......

param([string]$SolutionDir,
     [string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 7

而不是搞乱系统范围的设置并且必须区分32位和64位环境,更容易和更可靠的方法是ExecutionPolicy在PowerShell的调用中指定,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

请注意以上代码中的调用如何Get-ExecutionPolicy告诉您当前模式.另请注意在PowerShell本身的调用中如何指定此模式,该模式可以与脚本文件名结合使用:

test.ps1内容:

echo ('The current policy is ' + (Get-ExecutionPolicy)).ToString()
Run Code Online (Sandbox Code Playgroud)

Unrestricted在禁用了脚本的系统上调用带有策略的test.ps1 :

C:\Users\xyz>PowerShell -ExecutionPolicy Unrestricted -file test.ps1
The current policy is Unrestricted
Run Code Online (Sandbox Code Playgroud)

还要注意的是,上述呼叫并没有需要管理员权限,因此它可以在Visual Studio的预生成步骤或类似的调用.


小智 5

我在构建后的even命令中使用以下命令进行了操作:

PowerShell -NoProfile -ExecutionPolicy unrestricted -file $(SolutionDir)AutomationScript\DBAutomationScript.ps1 -target $(SolutionDir)MUFG.SECMOD.Data\SqlScripts -generatedFileName $(SolutionDir)MUFG.SECMOD.Data\SqlScripts\DeploymentDBScript.sql
Run Code Online (Sandbox Code Playgroud)

DBAutomationScript.ps1的内容:

param ([string]$target, [string]$generatedFileName)
Run Code Online (Sandbox Code Playgroud)