简体   繁体   中英

(Visual Studio) How to import powershell scripts into c# project so that I can publish my .NET application together with the scripts

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 !

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

在此处输入图像描述

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