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:
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.
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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With