我试图让 Powershell 在构建后运行我的 PS 脚本 - 但不知怎的,它并没有像预期的那样工作:
构建后的以下命令:
C:\WINDOWS\system32\windowspowershell\1.0\powershell.exe
-Command "& $(MSBuildProjectDirectory)\CreateSite.ps1 'auto'"
(插入换行符以便更好地阅读)
该命令成功执行了 powershell 脚本,但它无法运行(Build 的输出)中的命令: 运行构建后命令:
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 2
At C:\path\CreateSite.ps1:4 char:
38
+ Add-PsSnapin <<<< Microsoft.SharePoint.PowerShell}
+ CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShell:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
接下来会出现许多错误,因为所有后续命令都需要 Sharepoint 管理单元。
- 从 cmd 运行 powershell C:\path\CreateSite.ps1 auto 时 - 一切正常。
- 打开 powershell.exe 并运行 C:\path\CreateSite.ps1 auto 时 - 一切正常。
- 右键单击 CreateSite.ps1 --> 使用 powershell 运行 - 一切正常。
脚本中的相关行就是 Add-PsSnapin Microsoft.SharePoint.PowerShell
。
如何运行该脚本(并让它包含 PSSnapIn)并在 Visual Studio 构建后向其传递参数?
最佳答案
(这个帖子并不新鲜,但我是从 Google 来到这里的,所以我认为分享我发现的解决方案对其他人来说会很有趣)
我尝试将 powershell.exe 的路径更改为“%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe”,效果非常好。 64 位版本是从“构建后”事件中调用的,并且成功添加了 SharePoint 管理单元。
本文来源:http://msdn.microsoft.com/en-us/library/ff798298.aspx ,“在 Visual Studio 中使用 Windows PowerShell 脚本自动执行任务”。
关于visual-studio-2010 - 使用参数在构建后调用 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5006619/