Program Tip

빌드 후 이벤트 실행 PowerShell

programtip 2020. 10. 16. 07:59
반응형

빌드 후 이벤트 실행 PowerShell


빌드 후 이벤트로 .NET 프로젝트를 설정하여 powershell 스크립트를 실행할 수 있습니까? 이 스크립트를 사용하여 일부 파일을 생성하고 있습니다.

또한 디버그 또는 릴리스 빌드인지 여부를 스크립트로 전달할 수 있습니다. 이것의 예가 좋을 것입니다.


다음은 예입니다.

우선 : 스크립트를 실행하려면 PowerShell을 구성해야한다는 사실을 알고 있어야합니다. 다음 줄에서는 PowerShell이 ​​스크립트를 실행할 수 있습니다.

Set-ExecutionPolicy RemoteSigned

여기에 특별한 언급이 있습니다 . 64 비트 시스템을 실행하는 경우 'devenv.exe '가 Visual Studio 2010 실행 파일이 32Bits exe 라는 사실에주의 해야하므로 PowerShell 32에서 스크립트를 실행하도록 허용해야합니다.

여기에서 프로젝트 속성으로 이동하여 아래에 표시된대로 빌드 후 구성 할 수 있습니다 (프랑스어로 죄송합니다).

VS 2010에서 빌드 후

예 :

powershell을 사용한 포스트 빌드의 예

다음은 ' psbuild.ps1' 파일 test.txt입니다. 대상 경로에 구성 이름 이 포함 된 ' '를 만듭니다 . 포스트 빌드 스크립트 (메시지 상자, 사운드, 출력 메시지)를 디버깅하는 다른 방법에 주석을 달았습니다.

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

명령 Set-ExecutePolicy는 현재 세션에서 실행 정책을 일시적으로 설정합니다. powershell에서 이것을 설정하고 빌드 후 명령을 실행하면 여전히 허용되지 않습니다. 그래서 먼저 설정하고 다음과 같이 ps1 스크립트를 실행하십시오.

powershell -ExecutionPolicy Unrestricted $(ProjectDir)Deploy.ps1 -ProjectDir $(ProjectDir) -TargetPath $(TargetPath)

Visual Studio에서 power-shell 스크립트를 호출하기 전에 ExecutionPolicy를 다음 RemoteSigned과 같이 power-shell 창에서로 설정합니다 .

Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: RemoteSigned;

그런 다음 다음과 같은 방식으로 powershell 스크립트를 호출하십시오.

(전체 "powershell.exe"파일 경로를 전달할 필요 없음)

powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)

여기에 이미지 설명 입력

then in the script, you can always read the parameter like this...

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

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.


I made it with below command in post-build even command:

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

DBAutomationScript.ps1 내용 :

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

참고 URL : https://stackoverflow.com/questions/6500320/post-build-event-execute-powershell

반응형