[c#] 빌드 후 이벤트 실행 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)


답변

시스템 전체 설정을 엉망으로 만들고 32 비트와 64 비트 환경을 구분해야하는 대신 다음과 같이 PowerShell에 대한 호출에서 를 지정하는 것이 훨씬 쉽고 안정적인 방법입니다 ExecutionPolicy.

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

위 코드에서 호출 Get-ExecutionPolicy이 현재 모드를 알려주는 방법에 유의 하십시오. 또한 스크립트 파일 이름과 결합 할 수있는 PowerShell 자체에 대한 호출에서이 모드가 지정되는 방식에 유의하십시오.

test.ps1 내용 :

echo ('The current policy is ' + (Get-ExecutionPolicy)).ToString()

Unrestricted스크립트가 비활성화 된 시스템 에서 정책을 사용하여 test.ps1 호출 :

C:\Users\xyz>PowerShell -ExecutionPolicy Unrestricted -file test.ps1
The current policy is Unrestricted

또한 위의 호출 에는 관리자 권한이 필요 하지 않으므로 Visual Studio의 사전 빌드 단계 또는 이와 유사한 단계에서 호출 할 수 있습니다.


답변

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)

여기에 이미지 설명 입력

그런 다음 스크립트에서 항상 다음과 같은 매개 변수를 읽을 수 있습니다.

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


답변

빌드 후 명령에서 아래 명령으로 만들었습니다.

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)


답변