Visual studio 从自定义项目模板执行PowerShell脚本

Visual studio 从自定义项目模板执行PowerShell脚本,visual-studio,powershell,visual-studio-2012,nuget,project-template,Visual Studio,Powershell,Visual Studio 2012,Nuget,Project Template,我目前正在创建一个项目模板,我想知道是否可以在创建项目后执行PowerShell脚本(类似于NuGet软件包中的install.ps1) 有没有机会在不实现我自己的情况下执行此操作?好的。。。我尝试了很多东西,但最终我还是选择了定制IWizard public class InitScriptWizard : IWizard { public void RunStarted( object automationObject, Dictionary<s

我目前正在创建一个项目模板,我想知道是否可以在创建项目后执行PowerShell脚本(类似于NuGet软件包中的install.ps1


有没有机会在不实现我自己的情况下执行此操作?

好的。。。我尝试了很多东西,但最终我还是选择了定制IWizard

public class InitScriptWizard : IWizard
{
    public void RunStarted(
        object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind,
        object[] customParams)
    { }

    public void ProjectFinishedGenerating(Project project)
    {
        var script =
            project.ProjectItems.FindProjectItem(
                item => item.Name.Equals("init.ps1"));

        if (script == null)
        {
            return;
        }

        var process =
            System.Diagnostics.Process.Start(
                "powershell",
                string.Concat(
                    "-NoProfile -ExecutionPolicy Unrestricted -File \"",
                    script.FileNames[0],
                    "\""));

        //if (process != null)
        //{
        //    process.WaitForExit();
        //}

        //script.Delete();
    }

    public void ProjectItemFinishedGenerating(ProjectItem projectItem)
    { }

    public bool ShouldAddProjectItem(string filePath)
    {
        return true;
    }

    public void BeforeOpeningFile(ProjectItem projectItem)
    { }

    public void RunFinished()
    { }
}
公共类初始化脚本向导:IWizard
{
已启动公共无效运行(
对象自动化对象,
词典替换词典,
巫师朗金德朗金德,
对象[]自定义参数)
{ }
公共无效项目完成生成(项目)
{
变量脚本=
project.ProjectItems.FindProjectItem(
item=>item.Name.Equals(“init.ps1”);
如果(脚本==null)
{
返回;
}
var过程=
系统。诊断。过程。启动(
“能量地狱”,
字符串。Concat(
“-NoProfile-ExecutionPolicy Unrestricted-File\”,
script.filename[0],
"\""));
//if(进程!=null)
//{
//process.WaitForExit();
//}
//script.Delete();
}
公共无效项目完成生成(项目项目)
{ }
公共bool ShouldAddProjectItem(字符串文件路径)
{
返回true;
}
打开文件之前的公共无效(ProjectItem ProjectItem)
{ }
public void RunFinished()
{ }
}

这很有效。不要相信带有向导的.dll只能部署到GAC。程序集可以取消签名并使用VSIX部署,请检查(对我来说非常有用)。我在var script=project.ProjectItems.FindProjectItem(item=>item.Name.Equals(“init.ps1”)时遇到问题;错误是ProjectItems不包含FindProjectItem的定义,并且没有扩展方法“FindProjectItem”接受类型为“ProjectItems”等的第一个参数(是否缺少用户指令或程序集引用)。非常感谢您的帮助。这只是一个简单的扩展方法。你可以在这里看到:超级..发现。。。由于某些原因,现在在ProjectFinishedGenerating行public void ProjectFinishedGenerating(项目项目)中出现项目空值异常,您知道如何在使用process.start时保持powershell/cmd窗口打开吗?