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

3
  • 1
    Does this answer your question? How to publish additional files into bin folder Commented Jan 7, 2022 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! Commented Jan 7, 2022 at 23:54
  • Thanks ! I will post another question Commented Jan 7, 2022 at 23:57

1 Answer 1

1

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

enter image description here

The path must be adjusted accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
I updated my question to add the error message that I get @Despacito 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.