1

I have some custom powershell cmdlets in several scripts, one of which i've loaded an executed as such in C#

string Script2 = File.ReadAllText(@"C:\blabla\blabla\ScriptPowershell\TestCheck.ps1");

shell.Commands.Clear();
shell.AddScript(Script2, false);
shell.Invoke();
shell.Commands.Clear();

shell.AddCommand("TestCheck").AddParameter("ServerName", $"{serverId}").AddParameter("ServerPort", $"{serverPort}");

Collection<PSObject> psObjects = shell.Invoke();

The main issue with this is that if I publish and install this app on another computer that surely might not have this script at the same exact directory, the cmdlet will not work.

QUESTION : Is there a way to publish the app with the scripts joined to it, so that it doesn't need to search in the users computer for the scripts ? Or at least be present in the same directory as the .exe

Any suggestion to solve this problem is welcome.

Thank you !

Edit 1 : In response to @despacito this is the error message after appyling his solution

enter image description here

  • 1
    Does this answer your question? [How to publish additional files into bin folder](https://stackoverflow.com/questions/52409575/how-to-publish-additional-files-into-bin-folder) – gunr2171 Jan 07 '22 at 21:57
  • This seems to be for another reason than the solution I posted. I'd suggest you to make a new question about this error since it's bad practice to have multiple questions on a single post. I'm no expert in this criteria so I unfortunately can't help you here. When you ask this question separately, I would also recommend to open up the "details" tab since it might have valuable information. I wish you good luck! – Despacito 2 Jan 07 '22 at 23:54
  • Thanks ! I will post another question – Shorty Beast Jan 07 '22 at 23:57

1 Answers1

1

You can create a folder within your project to contain your scripts:

enter image description here

The path must be adjusted accordingly.

Despacito 2
  • 444
  • 2
  • 10
  • Thank you, I did that and changed the path to the script when I set the script2 string in the code. When I build the code in VS the script works just fine, but when I publish it and click a button to execute the script it sends an error – Shorty Beast Jan 07 '22 at 23:12
  • Could you state what the error says? Btw, to help you with terminology: **Compiler error**: The thing that checks if the code makes sense before it's run. **Run time exception**: A mistake that happened during the program was running. – Despacito 2 Jan 07 '22 at 23:25
  • I updated my question to add the error message that I get @Despacito 2 – Shorty Beast Jan 07 '22 at 23:38