Can my PowerGUI compiled PowerShell script be viewed by users? - powershell

I use PowerGUI to convert my PowerShell scripts to executables, and it is working fine.
I'd like to prevent my compiled script from being extracted or decompiled by users I distribute the generated executable to.
Is it possible to view my PowerShell script inside the generated executable? If so, how do I prevent this?

Unfortunately with PowerGUI it's not possible to prevent users of your executable from viewing your script, even if you use the "Protect script source code with a password" option.
The executable that PowerGUI generates is a self extracting ZIP file so it's possible to use 7-zip or WinRAR to open this file and view the original PowerShell script.
If you set a password using the "Protect script source code with a password" option all this does is password protect the files inside the ZIP container. However when launching the generated executable you'll be prompted for said password which can then be used to access the original script.

If you want to somewhat obfuscate the code, look at PS2EXE:
http://ps2exe.codeplex.com/
which points to:
https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1

download the ps2exe
http://ps2exe.codeplex.com/
run power shell
cd the folder of ps2exe
run:
.\yourfile.ps1 -inputFile C:\Users\franc\Desktop\temp\ps2exe\Install.ps1 C:\Users\franc\Desktop\temp\ps2exe\Install.exe
check the folder, the exe will be there

Related

Convert a PowerShell script to exe Including an Exe file into it

I have a few Powershell scripts that I use to copy files and run installations with the required install steps included in the sript, i know how to convert these scripts into exes. I would like to know, how to include the files I am installing with the script in the powershell converted exe?
This utility on github has proven very useful to me:
https://github.com/rzander/PS2EXE
It comes with a pretty straightforward GUI and command-line version, but only works with one PowerShell file.
I have modified a personal version to take multiple files and combine them into one executable. Let me know if you’re interested and I can upload it.
I would like to answer my own question for whom ever come across the same issue or need!
I had the way of doing this all along, but I didn't pay attention to details: For what I required which is to convert a .PS1 script that installs postgresql unattended, to an .exe file including(Postgres enbedded in the exe).
The best tool for this is the "PS1 to EXE" https://gallery.technet.microsoft.com/PS1-To-EXE-Generator-d39e8be2 software,
1. Load your ps1 file, make sure everything is working before hand.
2. Embed the the files you need for the ps1 to work (embed option is on the right column next to the "Options")
Then you can click the convert option
PS: Be sure to have the correct file path.

imported powershell module not visible when running via ssis package

I installed the ImportExcel module as described here:
https://www.powershellgallery.com/packages/ImportExcel/5.0.1
Then I wrote a simple ps1 file that loops though certain folder and converts each xlsx file in that folder to csv format (using ConvertFrom-ExcelSheet method from the above module).
Finally, I wrapped that ps1 script into a .bat file.
That batch script works fine when executed manually via windows command line.
However, when the same batch file is executed from an SSIS package (on the same computer, under my credentials, using the standard Execute Process Task ), when the powershell script reaches the line calling ConvertFrom-ExcelSheet method, I am getting the following error:
The term 'ConvertFrom-ExcelSheet' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The PC was restarted after module installation.
The module was installed with the AllUsers scope.
Elevated shell during module installation.
Any hints what I can do to get this module working in SSIS? Or at least, where to start looking?
Environment:
PowerShell 5.0.10586.117
Windows 7 Enterprise x64
Visual Studio 2012
Problem solved by moving the installed module to user's module folder i.e. %USERPROFILE%\Documents\WindowsPowerShell\Modules
There is still no clear explanation why the module was not working from shared modules folder but at least the issue is now gone.

power shell not releasing dll handle

I have a zip file which contains lot of dlls. Using power shell I unzip them and then import one dll to perform some operations. It works for the first time
Expand-ZipArchive "Myzip.zip" #this operation unzip all files
import-module "test.dll" #load the dll from zip file
perform some operations
But when I run it again Expand-ZipArchive fails to replace the dll files because the power shell is still holding it even after the execution. I am getting below error
"The process cannot access the file test.dll because it is being used by another process.\""
The only solution is close the powershell command prompt and then start a new window. But this will not help me in actual production enviornment
I tried to unload the module but still no help. I need to unzip every time because the zip will have new releases
I think I explained the issue in a very bad way. Any suggestions would help

TFS post build powershell script not seeing commands from imported module

We have a TFS build process using a custom template. The build works fine with the crm solution files successfully being created. We are then attempting to run a powershell script on the post build. This script imports a module. But the commands in the module are not being exposed. Whenever we attempt to invoke a command, we get an error. Along the lines of
The term 'Get-XrmSolution' is not recognized as the name of a cmdlet,
function, script file, or operable program.
It doesn't matter which command we use in that module, we get the same kind of error.
To troubleshoot, we have logged onto the build server under the identity of the build account and can successfully run the script we are attempting to run.
Putting some more output into the script to troubleshoot...
Get-Module lists Xrm.Framework.CI.PowerShell. - Good.
Get-Command -Module "Xrm.Framework.CI.PowerShell" returns nothing. From the console, a number of commands are listed.
Is there something we need to do with the running of powershell post build scripts to enable the contents of an imported module to be seen?
Watch out for the bitness of PowerShell invoked by MSBuild and likewise, the bitness of MSBuild launched by Visual Studio. Certain modules only run in either 32-bit or 64-bit PowerShell and not both. You want to make sure the correct version of PowerShell is getting launched.
IIRC you have to explicitly import the module with no assumption of being loaded on user profile, nor on the script path.
Suppose Module1.psm1 is in the same folder as your script, use something like
Import-Module (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Definition) 'Module1.psm1')
I had the same problem.
The module was loaded in the 'C:\Program Files\WindowsPowerShell\Modules' folder (64 bits).
It all seemed fine when I logged on as the user, but it failed during TFS build.
Solution: I had to uninstall the module in PowerShell 64 bit and re-install in PowerShell 32 bit.
The module was then installed in 'C:\Program Files (x86)\WindowsPowerShell\Modules' folder.

How do we include a batch file in a msi installer which should run every time the msi is installed?

Wow do we include a batch file in a msi installer which should run every time the msi is installed?
I'm not sure but it might work if you create a custom action to execute cmd.exe and have the path and name of the batch file as a parameter.
Otherwise you can create a custom action that contains a vbscript file that executes the .bat file (or possibly you can use a vbscript instead of a bat file).
Just be aware that I've heard that some antivirus programs can get a bit nervous if you include scripts in your msi and might make the install fail.