简体   繁体   中英

Build a Visual Studio Solution with C# AND Powershell projects

Visual Studio 2015

VS newb. I have many PS scripts that I want to migrate into a C# Solution. The solution will in theory do stuff and then with button presses or other triggers, run the Powrshell scripts.

The problem is that when I add the two projects, I don't know how to access the PS script from the C# side. I tried adding the PS Project to the Solution then adding a Reference to it. 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"

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.

EDIT: Do not really want to just invoke some scripts from within the C# environment. 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.

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. 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.

  1. Put your scripts in your PowerShell Script project.
  2. Create a folder named "Scripts" in your C# project.
  3. Choose Add Existing Item on the Scripts folder.
  4. Browse over to your PowerShell project and select your scripts. 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".

You will need to repeat the steps to add links if you add any new PS scripts.

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". Hover over "Add", you will see "Existing Item" and click it.

Shift + Alt + A is the shortcut for the above steps.

Navigate to where your .ps file is and open it. The .ps file will now be in your C# Project.

Then, you need to look up how to execute Powershell files and reference the relative path.

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