简体   繁体   English

使用C#和Powershell项目构建Visual Studio解决方案

[英]Build a Visual Studio Solution with C# AND Powershell projects

Visual Studio 2015 Visual Studio 2015

VS newb. VS newb。 I have many PS scripts that I want to migrate into a C# Solution. 我有很多PS脚本要迁移到C#解决方案中。 The solution will in theory do stuff and then with button presses or other triggers, run the Powrshell scripts. 从理论上讲,该解决方案将完成工作,然后通过按下按钮或其他触发器来运行Powrshell脚本。

The problem is that when I add the two projects, I don't know how to access the PS script from the C# side. 问题是,当我添加两个项目时,我不知道如何从C#端访问PS脚本。 I tried adding the PS Project to the Solution then adding a Reference to it. 我尝试将PS项目添加到解决方案中,然后添加对它的引用。 But when I try to build (even with a blank PS Script) I get error "CS0006 Metadata file 'MyPath\\PSTest\\bin\\Debug\\MyApplication.exe could not be found" 但是,当我尝试构建(即使使用空白PS脚本)时,也会收到错误消息“找不到CS0006元数据文件'MyPath \\ PSTest \\ bin \\ Debug \\ MyApplication.exe”

Is it possible to even do this? 有可能做到这一点吗? All I want is a Solution that has a main C# project and a Powershell project included that I can edit and use those scripts from the C# side. 我想要的是一个包含一个主要C#项目和一个Powershell项目的解决方案,我可以从C#端编辑和使用这些脚本。

EDIT: Do not really want to just invoke some scripts from within the C# environment. 编辑:真的不想只从C#环境中调用一些脚本。 I want to add a Powershell Project to the Solution and further develop the Powershell scripts from there as well as create the new app in C# that will use the Powershell Project. 我想将Powershell项目添加到解决方案中,然后从那里进一步开发Powershell脚本,并在C#中创建将使用Powershell项目的新应用程序。

I'll offer a couple of similar options. 我将提供几个类似的选项。

Option 1, similar to GibralterTop's answer, is to put the PS Scripts in your C# project. 选项1与GibralterTop的答案类似,是将PS脚本放入您的C#项目中。

  1. Right-click on the project and choose "Add Folder" to create a folder named "Scripts". 右键单击该项目,然后选择“添加文件夹”以创建一个名为“脚本”的文件夹。
  2. Add your scripts to this folder. 将脚本添加到此文件夹。
  3. Select all of your scripts in the solution explorer. 在解决方案资源管理器中选择所有脚本。 In the file properties, set the "Copy to output directory" option to "Copy if newer". 在文件属性中,将“复制到输出目录”选项设置为“如果更新则复制”。

When your program runs, it will be able to find the scripts in 当程序运行时,它将能够在其中找到脚本

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts")

Option 2 is similar, but you can keep your PowerShell Script project. 选项2与之类似,但是您可以保留PowerShell脚本项目。

  1. Put your scripts in your PowerShell Script project. 将脚本放入PowerShell脚本项目中。
  2. Create a folder named "Scripts" in your C# project. 在C#项目中创建一个名为“脚本”的文件夹。
  3. Choose Add Existing Item on the Scripts folder. 选择“脚本”文件夹上的“添加现有项”。
  4. Browse over to your PowerShell project and select your scripts. 浏览到您的PowerShell项目并选择脚本。 Rather than clicking the Add button, click the down-arrow to the right of it and choose "Add as link". 与其单击“添加”按钮,不单击其右侧的向下箭头,然后选择“添加为链接”。
  5. Select the links in the C# project and set the "Copy to output directory" option "Copy if newer". 选择C#项目中的链接,并将“复制到输出目录”选项设置为“如果更新则复制”。

You will need to repeat the steps to add links if you add any new PS scripts. 如果添加任何新的PS脚本,则将需要重复这些步骤来添加链接。

Eventually, you will probably build an installer. 最终,您可能会构建一个安装程序。 You will need to have it put the scripts in the same relative location; 您将需要将脚本放置在相同的相对位置。 a "Scripts" folder under the installation folder. 安装文件夹下的“脚本”文件夹。

If you right click on your C# Project from within VS, in the context menu you will see "Add". 如果从VS中右键单击C#项目,则在上下文菜单中将看到“添加”。 Hover over "Add", you will see "Existing Item" and click it. 将鼠标悬停在“添加”上,您将看到“现有项目”并单击它。

Shift + Alt + A is the shortcut for the above steps. Shift + Alt + A是上述步骤的快捷键。

Navigate to where your .ps file is and open it. 导航到.ps文件所在的位置,然后将其打开。 The .ps file will now be in your C# Project. .ps文件现在将在您的C#项目中。

Then, you need to look up how to execute Powershell files and reference the relative path. 然后,您需要查看如何执行Powershell文件并引用相对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有许多项目的Visual Studio C#解决方案(依赖项) - Visual Studio C# Solution with many Projects (Dependencies) 为解决方案中的多个 Visual Studio 2012 C# 项目启动调试器时出现 Visual Studio 2017 警告错误 - Visual Studio 2017 warning errors when starting a debugger for multiple Visual Studio 2012 C# projects in a solution vs2003:C#无法建立Visual Studio解决方案 - vs2003 : c# can not build visual studio solution 使用C#代码中的msbuild构建Visual Studio解决方案 - Build visual studio solution using msbuild from c# code Visual Studio 2012 C#解决方案,并行构建竞争条件 - Visual Studio 2012 C# solution, parallel build race condition .Net解决方案与c#和可视化基础项目 - .Net Solution with c# and visual basic projects MS Visual Studio解决方案,结合了非托管C ++项目和C#项目 - MS Visual Studio solution combining unmanaged C++ project and C# projects 使用 CMake(针对 Visual Studio 的 CMake)在同一解决方案中创建 C# 和 C++/CLR 项目 - Creating C# and C++/CLR projects in the same solution using CMake (CMake targeting visual studio) 我可以使用 c# 在 Visual Studio 中的同一解决方案中使用两个项目吗 - Can I use two projects in same solution in Visual Studio using c# 使用Visual Studio中的NLog在多个项目中设置C#解决方案 - Setting up C# solution with multiple projects using NLog in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM