Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding powershell scripts into c# program

1st time poster, so sorry if this is incorrect

Im an IT SysAdmin/general I.T Admin

Background: I have created some separate powershell scripts to do some basic repetitive tasks, this could be adding firewall rules, renaming computers, creating Local accounts on a PC. In order to bypass the powershell script execution policy on computers, I have a .bat and a ps1 file. I launch the relevant .bat, which in turn launches the ps1 file that does the work. These work fine for myself since I know how they work and I dont mind working with the powershell console etc but is a bit of a pain copying the required .bat/ps1 files to a computer, executing and then deleting the files before shipping to a customer

End Goal:

  • Eliminate the need for multiple .bat/.ps1's having to be copied to a PC and run each script individually
  • Eliminate powershell console view for user input etc
  • Try to get just 1 .exe (with all my scripts embedded within) that I can give to other members of the team that essentially does the same as the powershell scripts but just looks nicer

My thoughts are: Create a c# program (ive never used c# before) but if I can learn just enough to create a program with some buttons that you press that launches my specific powershell scripts.

  • Low level - A basic Form, buttons that launch a hardcoded powershell script (essentially copy.paste my script into the button code)
  • high level - A nice GUI with buttons that launch powershell scripts from the Internet or separate embedded resources within the .exe and if needed to prompt for user input, it prompts within the Nice GUI and then passes this back to the script?

What I know so far?

Ive seen some links such as: https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ I can hardcode the individual scripts code into each button I guess? but then if my scripts change as im testing/improving I would need to keep rebuilding the .exe and changing each button

Can you embed within the .exe a .ps1 resource or something? so I can hardcode ".\Script1.ps" on the button launch? rather than D:\Temp\Script.ps1..I understand I would still need to compile the .exe each time but at least it would be a bit tidier

Ive tried searching on here and googling but I havent exactly found what im looking for. Is this best practice? I seen on the internet people saying powershell can do what C# does and vice versa...the reason i would want to do it this way is I already have the powershell scripts created and the c# aspect of it is just for me to get a nice GUI keeping everything together hopefully

Thanks a lot for your help and if this question is too long/messy that I apologize

like image 569
manipulated Avatar asked Oct 31 '22 06:10

manipulated


1 Answers

This question is fairly generic and broad, so I'd argue that it's subject to a lot of personal decisions. Best practice would likely involve custom signed scripts you run after pushing an image that has already had the execution policy set up appropriately, but it sounds like that isn't an option for you. So, I'll work on assumptions.

Since you talk about shipping the computer to a customer at the end, I'll answer this with the assumption that you're using the scripts in a capacity outside of a Domain where best practice would be to use code signing (and the RemoteSigned or AllSigned execution policy). If this may still be an option, there are plenty of resources available for that already.

You also mention copying files to the computers and having to delete them afterwards, which drives your want for as few files as possible. I'll again assume that this is driven by a need to have people running specific scripts at specific times, for specific reasons (which is why you want a GUI).

You also also mention that you have a goal of removing the PoSh console from user view, but then you talk about having a GUI, so I'll assume that your people running the scripts don't love dealing with complexity.

The C# Route

So let's say this is a big enough deal to take the time and learn C#. When you invoke PowerShell from C# you are still subject to the execution policy of the machine since you are still spinning up a PowerShell Host within C#. So you would to handle the changing of the Execution Policy (and then changing it back when you're done). Depending on the length of your scripts, you could use embedded resources, or you could simply save them as strings.

Like you said, you'd have to account for updating the GUI and buttons to launch the scripts any time things change, so that could get messy, and quick.

GUIs In PoSh

If you really want to have a GUI and nobody can convince you otherwise, there are plenty of tools out there that will help you with that, if you don't want to do the coding yourself (protip: don't). Look for the ones that build it directly in to your PoSh code so it stays one file, but be aware that some can make the code pretty messy.

My Suggestion

Arguably, the best solution may not be the best practice depending on your needs. To keep it within relative scope of your question, I'd do the following:

  1. Combine all your PoSh scripts in to a single script, and separate the functionality with properly written functions.
  2. Create a main function for your script that presents the user with a list of the options representing all of the functions you created for step 1. Have the user enter a number or letter to choose which function to proceed with.
  3. Run the function, gather any input needed (if renaming a computer, have it prompt for computer name, etc) and once completed provide results to the user.
  4. Prompt the user if they want to run again, or exit.
  5. Upon exiting, tell the script to delete first the bat file and then delete itself. (Careful not to do this when testing your source script!)

You'll still have a .bat file and a PoSh script, but it will be easier for your users to use, it will clean itself up when finished and most importantly, you won't have the overhead of learning C# and having to do all the extra work maintaining and recompiling the .exe file.

like image 118
squid808 Avatar answered Nov 15 '22 08:11

squid808