使用参数后置调用Powershell脚本 [英] Call powershell script in post-built with parameters

查看:114
本文介绍了使用参数后置调用Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Powershell在后期构建中运行我的PS脚本-但由于某种原因它无法正常运行

I'm trying to get Powershell to run my PS script in post built - but somehow it doesn't work like it's supposed to:

在Post中遵循命令-构建:

Following command in Post-Build:

C:\WINDOWS\system32\windowspowershell\1.0\powershell.exe
  -Command "& $(MSBuildProjectDirectory)\CreateSite.ps1 'auto'"

(插入换行符以便于阅读)

该命令成功执行了powershell脚本,但是不能执行以下命令(在生成):
运行后的生成命令:

The command executes the powershell script sucessfully, but what it can't do is run the commands within (Output from Build): Rund Post-Build Command:

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管理单元。

And following that are many errors because all subsequent commands need the Sharepoint Snap-In.


  • 从cmd运行powershell C:dpath\CreateSite.ps1 auto-一切正常。

  • 打开powershell时。 exe并运行C:\path\CreateSite.ps1自动-一切正常。

  • 右键单击CreateSite.ps1->用powershell运行-一切正常。

脚本中的相关行就是 Add-PsSnapin Microsoft.SharePoint.PowerShell

我如何才能运行darn脚本(并使其包含PSSnapIn)在Visual Studio生成后的过程中向其传递参数?

解决方案

由于文件系统虚拟化,您不能真正从32位进程(即Visual Studio-托管msbuild引擎)指定到64位版本的PowerShell的路径。 )。解决该问题的一种可行方法是创建一个以64位运行的64位启动器,并将启动PowerShell的64位版本。这是一个简单的C#程序,可以执行此操作:

Because of file system virtualization, you can't really specify the path to the 64-bit version of PowerShell from a 32-bit process (ie Visual Studio - which hosts the msbuild engine). One hack-ish way to work around this is to create a 64-bit launcher that runs as 64-bit and will launch the 64-bit version of PowerShell. Here's a simple C# program that will do this:

using System;
using System.Diagnostics;

class App
{
  static int Main(string[] args)
  {
    Process process = Process.Start("PowerShell.exe", String.Join(" ", args));
    process.WaitForExit();
    return process.ExitCode;
  }
}

请务必将其编译为64位:

Be sure to compile this as 64-bit like so:

csc .\PowerShell64.cs /platform:x64

然后,从构建后事件中执行此启动器exe,并向其传递您要用来调用64位PowerShell的参数。另外,在PowerShell 2.0中,我建议使用 File 参数执行脚本,例如:

Then, from your post-build event execute this launcher exe passing it the parameters you want to invoke 64-bit PowerShell with. Also, with PowerShell 2.0 I would recommend using the File parameter to execute a script e.g.:

c:\path\PowerShell64.exe -File "$(MSBuildProjectDirectory)\CreateSite.ps1" auto

也就是说,肯定有其他方法(实用程序)可以从64位进程中启动exe。

That said, surely there has to be some other way (utility) that launches exes from a 64-bit process.

这篇关于使用参数后置调用Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆