1

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.

JoeBlow
  • 61
  • 1
  • 2
  • 7
  • 1
    Possible duplicate of [Execute PowerShell Script from C# with Commandline Arguments](http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments) – Lews Therin Apr 28 '17 at 17:55
  • Yea right click your c# project > add existing item > find your .ps file and add it – interesting-name-here Apr 28 '17 at 18:14
  • What is a "PowerShell project"? What project template did you use to create this project? I don't see a PowerShell project template either in the installed templates or the online templates. (I'm leading up to suggesting that you just put your PS scripts in a folder within your C# project. I'm also assuming your question is how to find the scripts from your C# program, not how to execute them.) – OldFart Apr 28 '17 at 18:46
  • @OldFart - Right click Solution, select Add->New Project. Select PowerShell from the left margin list. Select PowerShell Script Project. – JoeBlow Apr 28 '17 at 18:52
  • @JoeBlow - Found it now. I still wonder if a PowerShell project is the right choice in this circumstance. If the PS Scripts are put under the C# project, there is an option to copy them to the output folder. I don't see any such option for a PowerShell (Script) project. – OldFart Apr 28 '17 at 19:09
  • @OldFart - That's a good point. I guess I'll pursue that path then. – JoeBlow Apr 28 '17 at 19:29

2 Answers2

3

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.

OldFart
  • 2,411
  • 15
  • 20
0

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.

Community
  • 1
  • 1
interesting-name-here
  • 1,851
  • 1
  • 20
  • 33