简体   繁体   中英

Easiest/shortest command to run a PowerShell script as a build event in Visual Studio

Just getting started with PowerShell. I was running DOS .bat files in my post build events in VS and wanted to graduate up to PowerShell. Bat files were easy... CALL something.bat . I tried to do that with a PowerShell ps1 file and my trial-and-erroring got me to the following (first thing that worked...tried all the simple things first, obviously):

powershell -command "& {(powershell '$(ProjectDir)test.ps1')}"

Is there a shorthand version of this? I think the only thing that really bothers me is the redundant calls to the powershell executable, but that's probably only required because .ps1 files open in notepad by default on my machine (and I should keep the redundancy for deployment on other systems so I'm not reliant on the default program for a file type). Anyway, if there's unnecessary redundancy here, I'd love to know.

I'm very new to PowerShell, so any related insight is always appreciated.

I've used PowerShell as a post-build event in the past; now I lean towards using psake (super simple build system) or just running a raw PowerShell script. Post-build events get messy, are inflexible, and have few advantages over doing the same thing in a build script.

EDIT: If you are still interested in using a post-build script, I've answered the question before here

According to the MSN , this should work nice:

powershell.exe "$(ProjectDir)test.ps1"

Edit: Found this

powershell.exe "& ""$(ProjectDir)test.ps1"""

I didn't check any MSDN, it simply works:

powershell $(ProjectDir)test.ps1

Btw. don't forget to set run privileges for BOTH versions of PowerShell -- 32-bit and 64-bit.

I was sort of happy with the elegant powershell $(ProjectDir) solution detailed here, but I ran into problems when starting/stopping IIS App Pools via WebAdministration's Start-WebAppPool and Stop-WebAppPool

I had better success with:

%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe 

ie

%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe -Command "$(ProjectDir)Powershell\Stop-AppPool.ps1"

The reason turned out to be mixing of architectures, as detailed here (in the section "Using Windows PowerShell Scripts to Automate Tasks in Visual Studio"):

It's important that you use the virtual path of %WINDIR%\\SysNative and not the actual path of C:\\Windows\\System32. The reason for this is that Visual Studio 2010 is a 32-bit application that needs to call the 64-bit version of powershell.exe to successfully load the Microsoft.SharePoint.Powershell snap-in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM