PowerShell to executable - go the extra mile or leave it? - powershell

It seems like it is not all that easy to create an executable from a PowerShell script - do you know if it was ever meant to be an option?
I have found tools like PS2EXE, but still it does not seem like it was meant to be.
I am asking, if it is worth it to go the extra mile or leave it.
Background reason: I have some less technical users that need a smoother workflow.

As #Bill_Stewart kindly noted:
PowerShell is a shell that contains a powerful scripting language.
Which is perfectly in line with Microsoft's definition :
PowerShell is a task-based command-line shell and scripting language built on .NET.
Basically, wrapping it in an executable would go beyond its purpose. The nice thing about scripting is, that it's lightweight and task based. And in this case, that you can easily run it on multiple OS-ses.
So, I wouldn't go through the effort creating an exe.
If you want to run it easily, just create a .bat or .cmd file (if using Windows). On windows, I believe, you can also create a shortcut with command arguments and a little icon.
For Linux you clould use a .sh.

Related

Obfuscating a PowerShell script

I have written a PowerShell script in many hours and days and would like to obfuscate it, but so that the code is no longer readable or decompilable, but the script can still be executed. Is there something like that?
With regards to actually obfuscating an entire powershell script, this site seems to do just that.
I tested a script a little over a year ago and here are some of my findings:
Windows10: The obfuscated script worked on Windows 10. I dont
remember the Powershell version I had back then.
macOS: Confirmed it also works on Macs (if you have powershell installed), but I remember there were some errors spat out.
I just tested another script a few minutes ago on a Windows Server 2016 (PSVersion:5.1./Desktop) system. No issues so far.
Now, as was already mentioned by previous posters, it is important to note that any obfuscation can be hacked into. It's just a matter of incentive for the hacker.
I say "any" specifically because you dont control the hosts on which your powershell script will be used. And as such, those who do, if inclined, can alter the binary of the powershell program to get it to spit out everything that it does. How easy that is, I do not know. But a quick google search suggests there are settings available that, if turned on, can log the entire execution of your powershell script, obfuscated or not.
A couple of links that touches on Powershell logging:
PowerShell Logging: Recording and Auditing all Things
About Logging Windows - PowerShell - Microsoft Docs
There is a thing named Powershell Constrained language.
Quoting from here: https://devblogs.microsoft.com/powershell/powershell-constrained-language-mode/
Constrained language mode is a language mode of PowerShell designed to support day-to-day administrative tasks, yet restrict access to sensitive language elements that can be used to invoke arbitrary Windows APIs.
In Constrained mode, these are not supported:
COM objects
Unapproved .NET types
XAML based workflows
PowerShell classes
It's best for running administrative tasks, still it's not better for daily uses. To start it use:
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
Read more here: https://devblogs.microsoft.com/powershell/powershell-constrained-language-mode/
The Invoke-Obsufcation wrote by Daniel Bohmann is much better for this purpose. Link: https://github.com/danielbohannon/Invoke-Obfuscation
Usage Guide: https://blog.vonhewitt.com/2017/08/obfuscating-powershell-commands-using/
If you want something to obfuscate your powershell code to make it unreadable but keep it working you should check a project called Invoke-Obfuscationation done by Daniel Bohannan. You should check his talk about powershell obfuscation where he presented the tool.

How do I convert a PowerShell script with custom modules into a single executable?

I've written a simple script that has multiple custom functions stored as modules. I have done it this way because I was always been told that if your function can be reused by other things then it should be a module and not a .\ source include. I'm starting to think that mantra isn't right in my current scenario. I am trying to convert the script to an single .exe so that I can install it as a windows service.
Probably should acknowledge that I understand why you wouldn't want to include system modules like Active Directory or IIS management for the obvious issue that could lead to but I'm only trying to include custom functions in a single disputable non editable way.
I have used PowerGUI in the past but can't find any valid exe's for that since DELL have removed it and from memory, I don't think I've ever used it with a module.
I've tried PS2EXE-GUI and PS2EXE. Both of these make the exe and everything works fine while the modules exist. However, as soon as I put the exe on a server that hasn't got the Modules deployed to it, it fails to run. I thought the compile followed all the dependencies and included them as part of the build into the single exe? That appears to not be the case.
I've also tried the PowerShell Studio 2018 by Sapien, but based on their forums you can't include modules into the complied exe. Which again feels wrong if they are actually just custom functions, but it's the way they've written it.
I see https://poshtools.com/docs/posh-pro-tools/merge-script/ would possibly do what I need but that's chargeable and it looks like it actually merges all the content back into a single file. Given the time pressure I'm starting to think I'll have to pay if there are really no other better options. I just don't have time to join everything together manually and I can't help thinking there is a better way I'm missing!
Can anybody please suggest other options?
Could I also get clarification around my original mantra (functions go in modules...)?
"No, never!" or "Yes, always!" or "It's just wrong in this scenario."

Does everything done through GUI (in Windows) have a command line equivalent?

Does everything you do in the GUI in windows (i.e. through pointing, clicking and typing) have a command line equivalent, or are there some things (or maybe a lot of things) the command prompt can’t do?
FYI I'm very new to using the command line and just curious
Microsoft does not guarantee that, but it does gradually allow every changes on UI to have a related PowerShell cmdlet.
Thus, you'd better check out PowerShell, which is more related to server management, than programming.

How can I intercept and correct keypresses at a low level?

I keep typing "t eh" instead of " the" which is, of course, annoying in the amount of time it takes me to correct myself.
The obvious answer is "Learn to type, noob!" or at least to type more slowly and/or more correctly. This error is frighteningly consistent so it appears I've trained my muscle memory for that pattern already.
But I'm wondering if it's possible to write a small, windows portable script or application that, when it detects the incorrect sequence, backspaces and corrects it automatically at a layer where it would apply to any keyboard input.
Does C# have access to that layer of the OS that intercepts keypresses systemwide?
Will I run into UAC issues with Vista?
Am I re-inventing the wheel (ie, are there open source tools I can modify or use out of the box)?
In DOS this sort of thing was quite easy and one could make TSRs (Terminate and Stay Resident) programs that would, for instance, give you a calculator onscreen with a special keypress. Not to mention the many, many practical joke programs based on this concept (dial "M" for monster!)...
I would, of course, never suggest such a utility could be used that way for co-workers...
-Adam
On windows you could use AutoHotKey. That allows you to create little scripts or macros to automate and correct things like mistypes.
One use was posted on lifehacker which took the common mistyped words and corrected them. It is at http://lifehacker.com/192506/download-of-the-day-universal-autocorrect
UPDATE Per Comment: This is Free software and windows only as far as I know.
The above script is just an example of what it can do. There are a slew of scripts available at AutoHotkeys Site
I suggest AutoHotKey. If you've never used it before, have a quick read of the tutorial: http://www.autohotkey.com/docs/Tutorial.htm
The feature you are looking for is called "hotstrings." http://www.autohotkey.com/docs/Hotstrings.htm
In your case, your script would look something like:
::teh::the
That's it! Add other things you want corrected on additional lines. AutoHotkey scripts can be compiled so you don't have to install AutoHotKey on all of your machines.
It's a very cool program. It's primary use (for making custom hotkeys) rocks! These scripts are system wide so you'll also probably want to make a hotkey to be able to turn them off too!
EDIT: In a comment, it was mentioned that he actually types "t eh" (with a space in it) and I wondered if something additional would be needed for it to work. I just tested it and it works fine. Just install autohotkey, and create a file with the .AHK extension. In that file put in the following line
::t eh::the
and save the file. Then double-click on the AHK file to load AutoHotKey with your script (you'll see a green square in your system tray to let you know it is running). It should work fine!
Yes, you can use pinvoke commands from C# to intercept the low-level os commands. I recommend you take a look at http://www.pinvoke.net. The coding isn't easy but it does work.
I suggest learning to type more slowly. I also suffer from "teh" and "ahve" in part due to autocorrect giving me the leniency. If you forced yourself to retrain then you would not be at a disadvantage when using someone else's machine.
Not to mention the unfortunate event when you need to write "t eh" and are being prevented by an overzealous 'corrector'.

CMD.exe replacement [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that does not really meet my needs at work because it's too heavy. I'd like a function-for-function identical wrapper on cmd.exe, but with highlighting, intellisense, and (critically) a tabbed interface. Powershell is okay, but the interface is still lacking.
Edited: I've been using ConEmu (http://conemu.github.io/) for quite some time now. This one is a wrapper too, since it is not really possible to replace the Windows console without rewriting the whole command interpreter. Below the line is my original answer for an earlier alternative.
Not exactly a replacement (actually, it's a prettifying wrapper) but you might try Console (http://sourceforge.net/projects/console/)
I've been using JPSoft's products a long time (starting back with 4OS2 and 4DOS), and currently use Take Command 9. It works with existing batch files, has it's own improvements on top, a tabbed interface, and filename coloring options.
Looks like their site is having some problems right now, but you can find them at: http://jpsoft.com/
I use Take Command 9.0. I have used JPSoft's products for years. It has a tabbed interface. I have Take Command start with Take Command, Powershell, and CMD.exe each in their own tab. It doesn't do syntax highlighting. Take Command is syntactically compatible with CMD.exe and enhances each command quite a bit and adds many more.
PowerShell isn't a complete replacement for CMD.exe or Take Command. I find myself using both. You might ask why I would still use CMD.exe and it is because I will use Take Command to test a batch file that is limited to commands that work in CMD.exe and I then need to deploy the batch file on a workstation/server that doesn't have Take Command on it. I can create/test in Take Command and then verify it works in CMD.exe before deploying it.
I don't know of any IDE's that provide Intellisense for batch files specifically. If they did it would only be for a few keywords anyway. Most of the time in batch files you are running commands that are external to the batch language and wouldn't be included in the Intellisense.
I use Textpad to edit my batch files. Take Command has a debugger and it has logging capabilities which makes it very easy to test your batch files.
PowerCmd is a trial-ware wrapper for cmd.exe and costs 30$
It offers:
tabs
a "normal" selection mode
copy'n'paste
highlighting
auto complete
buttons to start Python, Powershell and others
If you want a more feature-rich UI for Powershell, try PowerGUI.
http://powergui.org/index.jspa
NYAOS
"NYAOS" is the tcsh-like enhanced commandline shell for Windows and OS/2 !
http://www.nyaos.org/
For decent completion and command history, try the PyCmd wrapper at https://sourceforge.net/projects/pycmd/
I use 4NT from the above mentioned JPSoft. It works great and has great added functionality. It is being replaced by TakeCommand but I do not need that much extra functionality.
Update:
It's no longer known as 4NT. Now its name is TCC/LE.
Nick, I know you asked this a long while ago but I've just found it while searching for something related. I have been using PromptPal and its been great. I got it about a year ago, early 2008, and it was through this discount software thing called BitsDujour. I just went there and noticed they had a deal for 51% off that product only a few days ago. Keep your eyes on that site and maybe the discount will come up again soon. Its well worth the $30 but I got 2 licenses for 1/2 off, one for each of my PCs...
If you want not to use cmd.exe totally then go for ZOC.exe terminal.ZOC is enterprise application.
Else just add some feature to your Command prompt by installing GOW.EXE.GOW is opensource application.
But always you can go for Git which is giving mostly commands in bash. Just add the bin folder to your environment path. Your command prompt will work as unix terminal.