PS2exe is not working on Version 4.0.
Regards
Taqui
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
It’s BETA in the current version 0.5.0.0.
New in v0.4.0.0 (14/11/2013):
New in v0.4.0.0 (03/09/2013):
New in v0.3.0.0 (03/08/2013):
Original description of v0.1.0.0
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any. -x86 (switch) compile EXE to run as 32 bit application -x64 (switch) compile EXE to run as 64 bit application -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0 -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0 -lcid specify language ID for threads -sta run PowerShell environment in Single Thread Apartment mode -mta run PowerShell environment in Multithread Apartment mode -noconsole compile PS script as Windows application
Sample:
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Have fun!
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
It’s BETA in the current version 0.5.0.0.
New in v0.4.0.0 (14/11/2013):
New in v0.4.0.0 (03/09/2013):
New in v0.3.0.0 (03/08/2013):
Original description of v0.1.0.0
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any. -x86 (switch) compile EXE to run as 32 bit application -x64 (switch) compile EXE to run as 64 bit application -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0 -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0 -lcid specify language ID for threads -sta run PowerShell environment in Single Thread Apartment mode -mta run PowerShell environment in Multithread Apartment mode -noconsole compile PS script as Windows application
Sample:
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Have fun!
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
It’s BETA in the current version 0.5.0.0.
New in v0.4.0.0 (14/11/2013):
New in v0.4.0.0 (03/09/2013):
New in v0.3.0.0 (03/08/2013):
Original description of v0.1.0.0
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any. -x86 (switch) compile EXE to run as 32 bit application -x64 (switch) compile EXE to run as 64 bit application -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0 -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0 -lcid specify language ID for threads -sta run PowerShell environment in Single Thread Apartment mode -mta run PowerShell environment in Multithread Apartment mode -noconsole compile PS script as Windows application
Sample:
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Have fun!
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
if (!CONSOLE)
{
ik.PowerShell.CredentialForm.UserPwd cred = CredentialForm.PromptForPassword(caption, message, targetName, userName, allowedCredentialTypes, options);
if (cred != null )
{
System.Security.SecureString x = new System.Security.SecureString();
foreach (char c in cred.Password.ToCharArray())
x.AppendChar(c);
return new PSCredential(cred.User, x);
}
return null;
}
if (!string.IsNullOrEmpty(caption))
WriteLine(caption);
WriteLine(message);
Write("User name: ");
string un = ReadLine();
SecureString pwd = null;
Write("Password: ");
pwd = ReadLineAsSecureString();
PSCredential c2 = new PSCredential(un, pwd);
return c2;
}
Once the above was fixed, I noticed that the password was being displayed in plain text, something that would not be acceptable for the .exe I was creating. To fix this, I updated the function ReadLineAsSecureString(), see below for code update.public override System.Security.SecureString ReadLineAsSecureString()
{
System.Security.SecureString x = new System.Security.SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (x.Length > 0)
{
x.RemoveAt(x.Length - 1);
Console.Write("\b \b");
}
}
else
{
x.AppendChar(i.KeyChar);
Console.Write("*");
}
}
return x;
}
This works for the console application, but when using -noconsole, we are unable to use Get-Credential at all, as there is no prompt for credentials. Can I recommend the above code be added to the repository as an enhancement? Is there any troubleshooting or code updates to allow Get-Credential to work when -noconsole is used? Thank you.PS2EXE moved to Microsoft Technet Galery: https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
It’s BETA in the current version 0.5.0.0.
New in v0.4.0.0 (14/11/2013):
New in v0.4.0.0 (03/09/2013):
New in v0.3.0.0 (03/08/2013):
Original description of v0.1.0.0
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any. -x86 (switch) compile EXE to run as 32 bit application -x64 (switch) compile EXE to run as 64 bit application -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0 -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0 -lcid specify language ID for threads -sta run PowerShell environment in Single Thread Apartment mode -mta run PowerShell environment in Multithread Apartment mode -noconsole compile PS script as Windows application
Sample:
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Have fun!
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
It’s BETA in the current version 0.5.0.0.
New in v0.4.0.0 (14/11/2013):
New in v0.4.0.0 (03/09/2013):
New in v0.3.0.0 (03/08/2013):
Original description of v0.1.0.0
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any. -x86 (switch) compile EXE to run as 32 bit application -x64 (switch) compile EXE to run as 64 bit application -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0 -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0 -lcid specify language ID for threads -sta run PowerShell environment in Single Thread Apartment mode -mta run PowerShell environment in Multithread Apartment mode -noconsole compile PS script as Windows application
Sample:
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Have fun!
I have same issu when i add some picture ressource in my code. I thing some antivirus are more or less permissive with encapsulation code. For exemple "Avira" alert me but Sophos do not. I thing it's just a "false positive" for me.