Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active March 14, 2026 12:55
Show Gist options
  • Select an option

  • Save scriptingstudio/2e7a6c1aa61406ca7dd24b47cb8eb1f7 to your computer and use it in GitHub Desktop.

Select an option

Save scriptingstudio/2e7a6c1aa61406ca7dd24b47cb8eb1f7 to your computer and use it in GitHub Desktop.
PS2EXE script refactored. Source compressor added to reduce output size. Implemented as dotsource module.
#Requires -Version 5
<#
.SYNOPSIS
Converts powershell scripts to standalone executables.
.DESCRIPTION
Converts powershell scripts to standalone executables. GUI output and input is activated with one switch, real windows executables are generated. You may use the graphical front end Win-PS2EXE for convenience.
Please see Remarks on project page for topics "GUI mode output formatting", "Config files", "Password security", "Script variables" and "Window in background in -noConsole mode".
A generated executable has the following reserved parameters:
-? [<MODIFIER>] Powershell help text of the script inside the executable. The optional parameter combination
"-? -detailed", "-? -examples" or "-? -full" can be used to get the appropriate help text.
-debug Forces the executable to be debugged. It calls "System.Diagnostics.Debugger.Launch()".
-extract:<FILENAME> Extracts the powerShell script inside the executable and saves it as FILENAME.
The script will not be executed.
-wait At the end of the script execution it writes "Hit any key to exit..." and waits for a
key to be pressed.
-end All following options will be passed to the script inside the executable.
All preceding options are used by the executable itself.
.PARAMETER inputFile
Powershell script to convert to executable (file has to be UTF8 or UTF16 encoded)
.PARAMETER outputFile
destination executable file name or folder, defaults to inputFile with extension '.exe'
.PARAMETER prepareDebug
create helpful information for debugging of generated executable. See parameter -debug there
.PARAMETER x86
compile for 32-bit runtime only
.PARAMETER x64
compile for 64-bit runtime only
.PARAMETER lcid
location ID for the compiled executable. Current user culture if not specified
.PARAMETER STA
Single Thread Apartment mode
.PARAMETER MTA
Multi Thread Apartment mode
.PARAMETER nested
internal use
.PARAMETER noConsole
the resulting executable will be a Windows Forms app without a console window.
You might want to pipe your output to Out-String to prevent a message box for every line of output
(example: dir C:\ | Out-String)
.PARAMETER conHost
force start with conhost as console instead of Windows Terminal. If necessary a new console window
will appear.
Important: Disables redirection of input, output or error channel!
.PARAMETER UNICODEEncoding
encode output as UNICODE in console mode, useful to display special encoded chars
.PARAMETER credentialGUI
use GUI for prompting credentials in console mode instead of console input
.PARAMETER iconFile
icon file name for the compiled executable
.PARAMETER embedFiles
paths to files to embed given as hash table, will be extracted at runtime to the keys of the hashes, source
file names must be unique, e.g. -embedFiles @{'Targetfilepath'='Sourcefilepath'}.
Absolute and relative paths are allowed. For target paths a relative path beginning with '.\' is interpreted
as relative to the executable, without the leading '.\' as relative to the current path at runtime.
Directories are created automaticly on startup if necessary.
In the target path environment variables in cmd.exe notation like %TEMP% or %APPDATA% are expanded at runtime.
.PARAMETER title
title information (displayed in details tab of Windows Explorer's properties dialog)
.PARAMETER description
description information (not displayed, but embedded in executable)
.PARAMETER company
company information (not displayed, but embedded in executable)
.PARAMETER product
product information (displayed in details tab of Windows Explorer's properties dialog)
.PARAMETER copyright
copyright information (displayed in details tab of Windows Explorer's properties dialog)
.PARAMETER trademark
trademark information (displayed in details tab of Windows Explorer's properties dialog)
.PARAMETER version
version information (displayed in details tab of Windows Explorer's properties dialog)
.PARAMETER configFile
write a config file (<outputfile>.exe.config)
.PARAMETER noConfigFile
compatibility parameter
.PARAMETER noOutput
the resulting executable will generate no standard output (includes verbose and information channel)
.PARAMETER noError
the resulting executable will generate no error output (includes warning and debug channel)
.PARAMETER noVisualStyles
disable visual styles for a generated windows GUI application. Only applicable with parameter -noConsole
.PARAMETER exitOnCancel
exits program when Cancel or "X" is selected in a Read-Host input box. Only applicable with parameter -noConsole
.PARAMETER DPIAware
if display scaling is activated, GUI controls will be scaled if possible.
.PARAMETER winFormsDPIAware
creates an entry in the config file for WinForms to use DPI scaling. Forces -configFile and -supportOS
.PARAMETER requireAdmin
if UAC is enabled, compiled executable will run only in elevated context (UAC dialog appears if required)
.PARAMETER supportOS
use functions of newest Windows versions (execute [Environment]::OSVersion to see the difference)
.PARAMETER virtualize
application virtualization is activated (forcing x86 runtime)
.PARAMETER longPaths
enable long paths ( > 260 characters) if enabled on OS (works only with Windows 10 or up)
.EXAMPLE
Invoke-ps2exe C:\Data\MyScript.ps1
Compiles C:\Data\MyScript.ps1 to C:\Data\MyScript.exe as console executable
.EXAMPLE
ps2exe -inputFile C:\Data\MyScript.ps1 -outputFile C:\Data\MyScriptGUI.exe -iconFile C:\Data\Icon.ico -noConsole -title "MyScript" -version 0.0.0.1
Compiles C:\Data\MyScript.ps1 to C:\Data\MyScriptGUI.exe as graphical executable, icon and meta data
.EXAMPLE
Win-PS2EXE
Start graphical front end to Invoke-ps2exe
.NOTES
Version: 0.5.0.33
Date: 2025-08-19
Author: Ingo Karstein, Markus Scholtes
.LINK
https://github.com/MScholtes/PS2EXE
#>
function Invoke-Ps2exe {
[CmdletBinding()]
param (
[Parameter(Position=0)]
[string]$inputFile,
[string]$outputFile,
# Application options
[string]$iconFile,
[string]$title, [string]$description, [string]$company, [string]$product,
[string]$copyright, [string]$trademark, [string]$version,
[switch]$x86, [switch]$x64,
[switch]$noConsole, [switch]$conHost,
[switch]$noOutput, [switch]$noError,
[int]$lcid,
[switch]$UNICODEEncoding,
[switch]$configFile,
[switch]$credentialGUI,
[hashtable]$embedFiles,
[switch]$STA, [switch]$MTA,
[switch]$prepareDebug,
[switch]$noVisualStyles, [switch]$exitOnCancel,
[switch]$DPIAware,
[switch]$requireAdmin,
[switch]$virtualize, [switch]$supportOS,
[switch]$longPaths,
# Internal runtime options
[switch]$noCompress,
[switch]$noConfigFile,
[switch]$winFormsDPIAware,
[switch]$nested
)
if (-not $nested) {
Write-Host "PS2EXE-GUI v0.5.0.33 by Ingo Karstein, reworked and GUI support by Markus Scholtes`n"
}
if ($isMacos -or $islinux) {
Write-Warning 'Operation is not supported on this platform.'
return
}
if ([string]::IsNullOrEmpty($inputFile)) {
Show-Help
return
}
# PS7 detection, start compiler in Windows PowerShell
if ($PSVersionTable.PSEdition -eq "Core") {
if ($nested) {
Write-Warning 'This mode is not supported.'
return
}
$CallParam = [System.Text.StringBuilder]::new()
$null = $PSBoundParameters.GetEnumerator().foreach{
$key,$val = $_.Key,$_.Value
if ($val -is [System.Management.Automation.SwitchParameter]) {
if ($val.IsPresent) {$CallParam.Append(" -$key")}
}
elseif ($val -is [string]) {
if ($val -match " " -or [string]::IsNullOrEmpty($val)) {$val = "'$val'"}
$CallParam.Append(" -$key $val")
}
elseif ($val -is [hashtable]) {
$ht = $val.GetEnumerator().foreach{"'{0}'='{1}';" -f $_.Key,$_.Value}
$CallParam.Append(" -$key @{$ht}")
}
else {
$CallParam.Append(" -$key $val")
}
} # foreach
powershell.exe -Command "if (-not (Get-Command -Name 'Invoke-ps2exe' -ErrorAction 0)) {. $PSScriptRoot\ps2exe.ps1}; &'$($MyInvocation.MyCommand.Name)' $($CallParam.ToString()) -nested"
return
} # PS7
# retrieve absolute paths independent of path is given relative or absolute
$inputFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($inputFile)
if ($inputFile -match 'RevShell|Update-KB4524147|Rek4m2ell|UpdatxK1q24147') {
Write-Error "PS2EXE did not compile this because PS2EXE does not like malware." -Category ParserError -ErrorId RuntimeException
return
}
$outputFile = if ([string]::IsNullOrEmpty($outputFile)) {
[System.IO.Path]::ChangeExtension($inputFile,'exe')
}
else {
$fn = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($outputFile)
if (Test-Path -Literalpath $fn -PathType Container) {
Join-Path $outputFile ([System.IO.Path]::GetFileNameWithoutExtension($inputFile) + '.exe')
}
else {$fn}
}
if (-not (Test-Path -LiteralPath $inputFile -PathType Leaf)) {
Write-Error "Input file '$inputfile' not found!"
return
}
if ($inputFile -eq $outputFile) {
Write-Error "Input file is identical to output file!"
return
}
if ($outputFile -notmatch '\.(exe|com)$') {
Write-Error "Output file must have extension '.exe' or '.com'!"
return
}
if (-not [string]::IsNullOrEmpty($iconFile)) {
# retrieve absolute paths independent of path is given relative or absolute
$iconFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($iconFile)
if (-not (Test-Path -LiteralPath $iconFile -PathType Leaf)) {
Write-Error "Icon file '$iconFile' not found!"
return
}
}
# Test input conflicts
if ($winFormsDPIAware) {
$supportOS = $true
}
if ($noConsole -and $conHost) {
Write-Error "-noConsole cannot be combined with -conHost."
return
}
if ($requireAdmin -and $virtualize) {
Write-Error "-requireAdmin cannot be combined with -virtualize."
return
}
if ($supportOS -and $virtualize) {
Write-Error "-supportOS cannot be combined with -virtualize."
return
}
if ($longPaths -and $virtualize) {
Write-Error "-longPaths cannot be combined with -virtualize."
return
}
if ($noConfigFile -and $configFile) {
Write-Error "-configFile cannot be combined with -noConfigFile."
return
}
if (-not $configFile -and $longPaths) {
Write-Warning "Forcing generation of a config file, since the option -longPaths requires this."
$configFile = $true
}
if (-not $configFile -and $winFormsDPIAware) {
Write-Warning "Forcing generation of a config file, since the option -winFormsDPIAware requires this."
$configFile = $true
}
if ($STA -and $MTA) {
Write-Error "You cannot use switches -STA and -MTA at the same time!"
return
}
if (-not $MTA -and -not $STA) {
# Set default apartment mode for powershell version if not set by parameter
$STA = $true
}
# Normalize app info
$title = $title -replace '\\', '\\'
$product = $product -replace '\\', '\\'
$copyright = $copyright -replace '\\', '\\'
$trademark = $trademark -replace '\\', '\\'
$description = $description -replace '\\', '\\'
$company = $company -replace '\\', '\\'
$noVersion = if (-not [string]::IsNullOrEmpty($version)) {
# check for correct version number information
if (-not ($version -as [version]) -or $version -match '[^\d\\.]') {
Write-Error 'Version number has to be supplied in the form n.n.n.n, n.n.n, n.n or n (with n as number)!'
return
}
$false
} else {$true}
Write-Host
# Generate compiler options
$type = 'System.Collections.Generic.Dictionary`2' -as 'Type'
$type = $type.MakeGenericType(@(('System.String' -as 'Type'), ('System.String' -as 'Type')))
$obj = [Activator]::CreateInstance($type)
$obj.Add('CompilerVersion', 'v4.0')
$referenceAssemblies = [System.Collections.Generic.List[object]]::new()
$referenceAssemblies.Add("System.dll")
if (-not $noConsole) {
if ([System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object {$_.ManifestModule.Name -eq "Microsoft.PowerShell.ConsoleHost.dll"}) {
$referenceAssemblies.Add(([System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object {$_.ManifestModule.Name -eq "Microsoft.PowerShell.ConsoleHost.dll"} |
Select-Object -First 1).Location)
}
}
$referenceAssemblies.Add(([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.ManifestModule.Name -eq "System.Management.Automation.dll"} | Select-Object -First 1).Location)
$n = [System.Reflection.AssemblyName]::new("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$null = [System.AppDomain]::CurrentDomain.Load($n)
$referenceAssemblies.Add(([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.ManifestModule.Name -eq "System.Core.dll"} | Select-Object -First 1).Location)
if ($noConsole) {
$n = [System.Reflection.AssemblyName]::new("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$null = [System.AppDomain]::CurrentDomain.Load($n)
$n = [System.Reflection.AssemblyName]::new("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$null = [System.AppDomain]::CurrentDomain.Load($n)
$referenceAssemblies.Add(([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.ManifestModule.Name -eq "System.Windows.Forms.dll"} | Select-Object -First 1).Location)
$referenceAssemblies.Add(([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.ManifestModule.Name -eq "System.Drawing.dll"} | Select-Object -First 1).Location)
}
$cop = [Microsoft.CSharp.CSharpCodeProvider]::new($obj)
$compilerparam = [System.CodeDom.Compiler.CompilerParameters]::new($referenceAssemblies, $outputFile)
$compilerparam.GenerateInMemory = $false
$compilerparam.GenerateExecutable = $true
$iconFileParam = if (-not [string]::IsNullOrEmpty($iconFile)) {
'"/win32icon:{0}"' -f $iconFile
}
$newline = "`r`n"
if ($requireAdmin -or $DPIAware -or $supportOS -or $longPaths) {
$win32manifest = [System.Collections.Generic.List[object]]::new()
$win32manifest.Add("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>${newline}<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">${newline}")
if ($DPIAware -or $longPaths) {
$win32manifest.Add("<application xmlns=""urn:schemas-microsoft-com:asm.v3"">${newline}<windowsSettings>${newline}")
if ($DPIAware) {
$win32manifest.Add("<dpiAware xmlns=""http://schemas.microsoft.com/SMI/2005/WindowsSettings"">true</dpiAware>${newline}<dpiAwareness xmlns=""http://schemas.microsoft.com/SMI/2016/WindowsSettings"">PerMonitorV2</dpiAwareness>${newline}")
}
if ($longPaths) {
$win32manifest.Add("<longPathAware xmlns=""http://schemas.microsoft.com/SMI/2016/WindowsSettings"">true</longPathAware>${newline}")
}
$win32manifest.Add("</windowsSettings>${newline}</application>${newline}")
}
if ($requireAdmin) {
$win32manifest.Add("<trustInfo xmlns=""urn:schemas-microsoft-com:asm.v2"">${newline}<security>${newline}<requestedPrivileges xmlns=""urn:schemas-microsoft-com:asm.v3"">${newline}<requestedExecutionLevel level=""requireAdministrator"" uiAccess=""false""/>${newline}</requestedPrivileges>${newline}</security>${newline}</trustInfo>${newline}")
}
if ($supportOS) {
$win32manifest.Add("<compatibility xmlns=""urn:schemas-microsoft-com:compatibility.v1"">${newline}<application>${newline}<supportedOS Id=""{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}""/>${newline}<supportedOS Id=""{1f676c76-80e1-4239-95bb-83d0f6d0da78}""/>${newline}<supportedOS Id=""{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}""/>${newline}<supportedOS Id=""{35138b9a-5d96-4fbd-8e2d-a2440225f93a}""/>${newline}<supportedOS Id=""{e2011457-1546-43c5-a5fe-008deee3d3f0}""/>${newline}</application>${newline}</compatibility>${newline}")
}
$win32manifest.Add("</assembly>")
$win32manifest | Out-File -FilePath "$outputFile.win32manifest" -Encoding UTF8
$manifestParam = '"/win32manifest:{0}.win32manifest"' -f $outputFile
} else {$manifestParam = ''}
$platform = if ($x64 -and -not $x86) {'x64'}
elseif ($x86 -and -not $x64) {'x86'} else {'anycpu'}
$target = if ($noConsole -or $conHost) {'winexe'} else {'exe'}
if ($virtualize) {
Write-Host 'Application virtualization is activated, forcing x86 platform.'
$platform = 'x86'
$manifestParam = '/nowin32manifest'
}
$compilerparam.CompilerOptions = "/platform:$platform /target:$target $iconFileParam $manifestParam".TrimEnd()
$compilerparam.IncludeDebugInformation = $prepareDebug
if ($prepareDebug) {
$compilerparam.TempFiles.KeepFiles = $true
}
Write-Host "Reading input file '$inputFile'..."
if (-not $noCompress) {
Write-Host 'Compressing input file...'
$source = Compress-Source -Path $inputFile -NoTest
if (-not $source) {
Write-Error "Could not compress the source file." -ErrorAction Continue
return
}
$inputFile = Join-Path $env:TEMP ([System.IO.Path]::GetFileName($inputFile))
$source | Out-File -FilePath $inputFile -Force -ErrorAction 0
}
$null = $compilerparam.EmbeddedResources.Add($inputFile)
$EMBEDSECTION =
if ($null -ne $embedFiles -and $embedFiles.Count) {
Write-Host "Embedding $($embedFiles.Count) file(s)"
$sb = [System.Text.StringBuilder]::new()
$null = $sb.Append("string tgtFile = string.Empty, tgtDir = string.Empty;${newline}")
$null = $embedFiles.GetEnumerator() | ForEach-Object {
$compilerparam.EmbeddedResources.Add($_.Value)
$sb.Append("tgtFile = Environment.ExpandEnvironmentVariables(@`"$($_.name)`");${newline}if (string.Compare(`".\\`", 0, tgtFile, 0, 2) == 0) { tgtFile = System.AppDomain.CurrentDomain.BaseDirectory + tgtFile.Substring(2); }${newline}try { tgtDir = System.IO.Path.GetDirectoryName(tgtFile);${newline}if (tgtDir != string.Empty) { System.IO.Directory.CreateDirectory(tgtDir); }${newline}using (System.IO.Stream tgtStream = new System.IO.FileStream(tgtFile, System.IO.FileMode.Create)) { executingAssembly.GetManifestResourceStream(`"$([System.IO.Path]::GetFileName($_.Value))`").CopyTo(tgtStream); }${newline}}${newline}catch { throw new System.IO.IOException(`"Error creating '`" + tgtFile + `"'\r\n`"); }${newline}")
}
$sb.ToString()
}
#if ($null -ne $embedFiles -and -not $embedFiles.Count) {
# Write-Warning 'Embed files definition is empty.'
#}
$culture = if ($lcid) {
@"
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo($lcid);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo($lcid);
"@
}
$configFileForEXE3 = [System.Collections.Generic.List[object]]::new()
$x = if ($winFormsDPIAware) {'4.7'} else {'4.0'}
$configFileForEXE3.Add("<?xml version=""1.0"" encoding=""utf-8"" ?>${newline}<configuration><startup><supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v$x"" /></startup>")
if ($longPaths) {
$configFileForEXE3.Add("<runtime><AppContextSwitchOverrides value=""Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"" /></runtime>")
}
if ($winFormsDPIAware) {
$configFileForEXE3.Add("<System.Windows.Forms.ApplicationConfigurationSection><add key=""DpiAwareness"" value=""PerMonitorV2"" /></System.Windows.Forms.ApplicationConfigurationSection>")
}
$configFileForEXE3.Add("</configuration>")
Write-Host "Compiling the source..."
$cr = $cop.CompileAssemblyFromSource($compilerparam, (Get-MainProgram))
if ($cr.Errors.Count -gt 0) {
if (Test-Path -LiteralPath $outputFile) {
Write-Host 'Cleaning temp files...'
Remove-Item -LiteralPath $outputFile #-Verbose:$false
}
Write-Error "Could not create the '.exe' file because of compilation errors. Use -verbose parameter to see details." -ErrorAction Continue
$cr.Errors | ForEach-Object {Write-Verbose $_}
}
else {
if (Test-Path -LiteralPath $outputFile) {
Write-Host "Output file '$outputFile' written"
if ($prepareDebug) {
$cr.TempFiles | Where-Object {$_ -like "*.cs"} | Select-Object -First 1 | ForEach-Object {
$dstSrc = [System.IO.Path]::ChangeExtension($outputFile,'cs')
Write-Host "Source file name for debug copied: $dstSrc"
Copy-Item -Path $_ -Destination $dstSrc -Force
}
$cr.TempFiles | Remove-Item -Force -ErrorAction SilentlyContinue #-Verbose:$false
}
if ($configFile) {
$configFileForEXE3 | Out-File -FilePath "$outputFile.config" -Encoding UTF8
Write-Host "Config file for EXE created"
}
}
else {
Write-Error "Output file '$outputFile' not written." -ErrorAction Continue
}
}
if ($requireAdmin -or $DPIAware -or $supportOS -or $longPaths) {
if (Test-Path -LiteralPath "$outputFile.win32manifest") {
Remove-Item -LiteralPath "$outputFile.win32manifest" #-Verbose:$false
}
}
if ($inputFile -like "$env:TEMP*") { # remove a temp file
Write-Host 'Cleaning temp files...'
Remove-Item -LiteralPath $inputFile -Force -ErrorAction 0 #-Verbose:$false
}
} # END Invoke-Ps2exe
function Compress-Source {
# https://elhacker.info/Cursos/CEHv13/CEHv13%20Module%2006%20System%20Hacking/GitHub%20Tools/PowerSploit/ScriptModification/Remove-Comment.ps1
[CmdletBinding(DefaultParameterSetName = 'File')]
param (
[Parameter(Position = 0, Mandatory, ParameterSetName = 'File')]
[ValidateNotNullOrEmpty()]
[alias('FilePath','FileName')][String] $Path,
[Parameter(Position = 0, Mandatory, ParameterSetName = 'ScriptBlock')]
[ValidateNotNullOrEmpty()]
[ScriptBlock] $ScriptBlock,
[Switch] $NoTest
)
if ($Path) {
if (-not $NoTest) {if (-not (Test-Path $Path)) {return}}
$ScriptBlockString = [IO.File]::ReadAllText((Resolve-Path $Path).Path)
$ScriptBlock = [ScriptBlock]::Create($ScriptBlockString)
} else {
# Convert the scriptblock to a string so that it can be referenced with array notation
$ScriptBlockString = $ScriptBlock.ToString()
}
# Tokenize the scriptblock and return all tokens except for comments
$Tokens = @([System.Management.Automation.PSParser]::Tokenize($ScriptBlock, [Ref] $Null)).Where{$_.Content -match '^#Requires' -or $_.Type -ne 'Comment'}
$StringBuilder = [System.Text.StringBuilder]::new()
# The majority of the remaining code comes from Lee Holmes' Show-ColorizedContent script.
$CurrentColumn = 1
$NewlineCount = 0
$null = foreach ($CurrentToken in $Tokens) {
if ($CurrentToken.Type -match '^NewLine|^LineContinuation') {
$CurrentColumn = 1
# Only insert a single newline. Sequential newlines are ignored in order to save space.
if ($NewlineCount -eq 0) {
$StringBuilder.AppendLine()
}
$NewlineCount++
} else {
$NewlineCount = 0
# Do any indenting
if ($CurrentColumn -lt $CurrentToken.StartColumn) {
# Insert a single space in between tokens on the same line.
# Extraneous whiltespace is ignored.
if ($CurrentColumn -ne 1) {
$StringBuilder.Append(' ')
}
}
# See where the token ends
$CurrentTokenEnd = $CurrentToken.Start + $CurrentToken.Length - 1
$ScriptLines = -join $ScriptBlockString[$CurrentToken.Start..$CurrentTokenEnd]
# Handle multi-line strings
if ($CurrentToken.Type -eq 'String' -and $CurrentToken.EndLine -gt $CurrentToken.StartLine) {
$StringBuilder.Append($ScriptLines.replace("`r`n",""))
} else { # Write out a regular token
$StringBuilder.Append($ScriptLines)
}
# Update our position in the column
$CurrentColumn = $CurrentToken.EndColumn
} # token type
} # tokens
$StringBuilder.ToString()
} # END Compress-Source
function Show-Help {
Write-Host "USAGE:" -ForegroundColor DarkYellow
Write-Host "Invoke-ps2exe [-inputFile] <filename> [[-outputFile] <filename>]"
Write-Host " [-prepareDebug] [-x86|-x64] [-lcid <id>] [-STA|-MTA] [-noConsole] [-conHost] [-UNICODEEncoding]"
Write-Host " [-credentialGUI] [-iconFile <filename>] [-title <title>] [-description '<description>']"
Write-Host " [-company <company>] [-product <product>] [-copyright <copyright>] [-trademark <trademark>]"
Write-Host " [-version <version>] [-configFile] [-noOutput] [-noError] [-noVisualStyles] [-exitOnCancel]"
Write-Host " [-DPIAware] [-winFormsDPIAware] [-requireAdmin] [-supportOS] [-virtualize] [-longPaths]"
Write-Host
Write-Host 'PARAMETERS:' -ForegroundColor DarkYellow
Write-Host " inputFile = Powershell script that you want to convert to executable (file has to be UTF8 or UTF16 encoded)"
Write-Host " outputFile = destination executable file name or folder, defaults to inputFile with extension '.exe'"
Write-Host " prepareDebug = create helpful information for debugging"
Write-Host " x86 or x64 = compile for 32-bit or 64-bit runtime only"
Write-Host " lcid = location ID for the compiled executable. Current user culture if not specified"
Write-Host " STA or MTA = 'Single Thread Apartment' or 'Multi Thread Apartment' mode"
Write-Host " noConsole = the resulting executable will be a Windows Forms app without a console window"
Write-Host " conHost = force start with conhost as console instead of Windows Terminal (disables redirections)"
Write-Host " UNICODEEncoding = encode output as UNICODE in console mode"
Write-Host " credentialGUI = use GUI for prompting credentials in console mode"
Write-Host " iconFile = icon file name for the compiled executable"
Write-Host " embedFiles = files to embed given as hash, will be extracted to key of hash, source file names must be unique"
Write-Host " (e.g. -embedFiles @{'Targetfilepath'='Sourcefilepath'} )"
Write-Host " title = title information (displayed in details tab of Windows Explorer's properties dialog)"
Write-Host " description = description information (not displayed, but embedded in executable)"
Write-Host " company = company information (not displayed, but embedded in executable)"
Write-Host " product = product information (displayed in details tab of Windows Explorer's properties dialog)"
Write-Host " copyright = copyright information (displayed in details tab of Windows Explorer's properties dialog)"
Write-Host " trademark = trademark information (displayed in details tab of Windows Explorer's properties dialog)"
Write-Host " version = version information (displayed in details tab of Windows Explorer's properties dialog)"
Write-Host " configFile = write a config file (<outputfile>.exe.config)"
Write-Host " noOutput = the resulting executable will generate no standard output (includes verbose and information channel)"
Write-Host " noError = the resulting executable will generate no error output (includes warning and debug channel)"
Write-Host " noVisualStyles = disable visual styles for a generated windows GUI application (only with -noConsole)"
Write-Host " exitOnCancel = exits program when Cancel or ""X"" is selected in a Read-Host input box (only with -noConsole)"
Write-Host " DPIAware = if display scaling is activated, GUI controls will be scaled if possible"
Write-Host "winFormsDPIAware = if display scaling is activated, WinForms use DPI scaling (requires Windows 10 and .Net 4.7 or up)"
Write-Host " requireAdmin = if UAC is enabled, compiled executable run only in elevated context (UAC dialog appears if required)"
Write-Host " supportOS = use functions of newest Windows versions (execute [Environment]::OSVersion to see the difference)"
Write-Host " virtualize = application virtualization is activated (forcing x86 runtime)"
Write-Host " longPaths = enable long paths ( > 260 characters) if enabled on OS (works only with Windows 10 or up)"
Write-Host
Write-Warning "Input file not specified!"
} # END Show-Help
function Get-MainProgram {
@"
// Simple PowerShell host created by Ingo Karstein (http://blog.karstein-consulting.com)
// Reworked and GUI support by Markus Scholtes
// REFACTOR: Use preprocessor instead of string replacement
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Globalization;
using System.Management.Automation.Host;
using System.Security;
using System.Reflection;
using System.Runtime.InteropServices;
#if noConsole
using System.Windows.Forms;
using System.Drawing;
#endif
#if winFormsDpiAware
using System.Runtime.Versioning;
[assembly:TargetFrameworkAttribute(".NETFramework,Version=v4.7,Profile=Client",FrameworkDisplayName=".NET Framework 4.7")]
#endif
[assembly:AssemblyTitle("$title")]
[assembly:AssemblyProduct("$product")]
[assembly:AssemblyCopyright("$copyright")]
[assembly:AssemblyTrademark("$trademark")]
#if !noVersion
[assembly:AssemblyVersion("$version")]
[assembly:AssemblyFileVersion("$version")]
#endif
// not displayed in details tab of properties dialog, but embedded to file
[assembly:AssemblyDescription("$description")]
[assembly:AssemblyCompany("$company")]
namespace ModuleNameSpace
{
#if (noConsole || credentialGUI)
internal class Credential_Form
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct CREDUI_INFO
{
public int cbSize;
public IntPtr hwndParent;
public string pszMessageText;
public string pszCaptionText;
public IntPtr hbmBanner;
}
[Flags]
enum CREDUI_FLAGS
{
INCORRECT_PASSWORD = 0x1,
DO_NOT_PERSIST = 0x2,
REQUEST_ADMINISTRATOR = 0x4,
EXCLUDE_CERTIFICATES = 0x8,
REQUIRE_CERTIFICATE = 0x10,
SHOW_SAVE_CHECK_BOX = 0x40,
ALWAYS_SHOW_UI = 0x80,
REQUIRE_SMARTCARD = 0x100,
PASSWORD_ONLY_OK = 0x200,
VALIDATE_USERNAME = 0x400,
COMPLETE_USERNAME = 0x800,
PERSIST = 0x1000,
SERVER_CREDENTIAL = 0x4000,
EXPECT_CONFIRMATION = 0x20000,
GENERIC_CREDENTIALS = 0x40000,
USERNAME_TARGET_CREDENTIALS = 0x80000,
KEEP_USERNAME = 0x100000,
}
public enum CredUI_ReturnCodes
{
NO_ERROR = 0,
ERROR_CANCELLED = 1223,
ERROR_NO_SUCH_LOGON_SESSION = 1312,
ERROR_NOT_FOUND = 1168,
ERROR_INVALID_ACCOUNT_NAME = 1315,
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_INVALID_PARAMETER = 87,
ERROR_INVALID_FLAGS = 1004,
}
[DllImport("credui", CharSet = CharSet.Unicode)]
private static extern CredUI_ReturnCodes CredUIPromptForCredentials(ref CREDUI_INFO credinfo,
string targetName,
IntPtr reserved1,
int iError,
StringBuilder userName,
int maxUserName,
StringBuilder password,
int maxPassword,
[MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
CREDUI_FLAGS flags);
public class User_Pwd
{
public string User = string.Empty;
public string Password = string.Empty;
public string Domain = string.Empty;
}
internal static User_Pwd PromptForPassword(string caption, string message, string target, string user, PSCredentialTypes credTypes, PSCredentialUIOptions options)
{
// Flags und Variablen initialisieren
StringBuilder userPassword = new StringBuilder("", 128), userID = new StringBuilder(user, 128);
CREDUI_INFO credUI = new CREDUI_INFO();
if (!string.IsNullOrEmpty(message)) credUI.pszMessageText = message;
if (!string.IsNullOrEmpty(caption)) credUI.pszCaptionText = caption;
credUI.cbSize = Marshal.SizeOf(credUI);
bool save = false;
CREDUI_FLAGS flags = CREDUI_FLAGS.DO_NOT_PERSIST;
if ((credTypes & PSCredentialTypes.Generic) == PSCredentialTypes.Generic)
{
flags |= CREDUI_FLAGS.GENERIC_CREDENTIALS;
if ((options & PSCredentialUIOptions.AlwaysPrompt) == PSCredentialUIOptions.AlwaysPrompt)
{
flags |= CREDUI_FLAGS.ALWAYS_SHOW_UI;
}
}
// den Benutzer nach Kennwort fragen, grafischer Prompt
CredUI_ReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, target, IntPtr.Zero, 0, userID, 128, userPassword, 128, ref save, flags);
if (returnCode == CredUI_ReturnCodes.NO_ERROR)
{
User_Pwd ret = new User_Pwd();
ret.User = userID.ToString();
ret.Password = userPassword.ToString();
ret.Domain = "";
return ret;
}
return null;
}
}
#endif
internal class MainModuleRawUI : PSHostRawUserInterface
{
#if noConsole
// Speicher für Konsolenfarben bei GUI-Output werden gelesen und gesetzt, aber im Moment nicht genutzt (for future use)
private ConsoleColor GUIBackgroundColor = ConsoleColor.White;
private ConsoleColor GUIForegroundColor = ConsoleColor.Black;
#if !title
private string GUITitle = System.AppDomain.CurrentDomain.FriendlyName;
#else
private string GUITitle = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;
#endif
#else
const int STD_OUTPUT_HANDLE = -11;
//CHAR_INFO struct, which was a union in the old days
// so we want to use LayoutKind.Explicit to mimic it as closely
// as we can
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
[FieldOffset(0)]
internal char UnicodeChar;
[FieldOffset(0)]
internal char AsciiChar;
[FieldOffset(2)] //2 bytes seems to work properly
internal UInt16 Attributes;
}
//COORD struct
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
//SMALL_RECT struct
[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
/* Reads character and color attribute data from a rectangular block of character cells in a console screen buffer,
and the function writes the data to a rectangular block at a specified location in the destination buffer. */
[DllImport("kernel32.dll", EntryPoint = "ReadConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool ReadConsoleOutput(
IntPtr hConsoleOutput,
/* This pointer is treated as the origin of a two-dimensional array of CHAR_INFO structures
whose size is specified by the dwBufferSize parameter.*/
[MarshalAs(UnmanagedType.LPArray), Out] CHAR_INFO[,] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpReadRegion);
/* Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer.
The data to be written is taken from a correspondingly sized rectangular block at a specified location in the source buffer */
[DllImport("kernel32.dll", EntryPoint = "WriteConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool WriteConsoleOutput(
IntPtr hConsoleOutput,
/* This pointer is treated as the origin of a two-dimensional array of CHAR_INFO structures
whose size is specified by the dwBufferSize parameter.*/
[MarshalAs(UnmanagedType.LPArray), In] CHAR_INFO[,] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpWriteRegion);
/* Moves a block of data in a screen buffer. The effects of the move can be limited by specifying a clipping rectangle, so
the contents of the console screen buffer outside the clipping rectangle are unchanged. */
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ScrollConsoleScreenBuffer(
IntPtr hConsoleOutput,
[In] ref SMALL_RECT lpScrollRectangle,
[In] ref SMALL_RECT lpClipRectangle,
COORD dwDestinationOrigin,
[In] ref CHAR_INFO lpFill);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
#endif
public override ConsoleColor BackgroundColor
{
#if !noConsole
get
{
return Console.BackgroundColor;
}
set
{
Console.BackgroundColor = value;
}
#else
get
{
return GUIBackgroundColor;
}
set
{
GUIBackgroundColor = value;
}
#endif
}
public override System.Management.Automation.Host.Size BufferSize
{
get
{
#if !noConsole
if (Console.IsOutputRedirected)
// return default value for redirection. If no valid value is returned WriteLine will not be called
return new System.Management.Automation.Host.Size(120, 50);
else
return new System.Management.Automation.Host.Size(Console.BufferWidth, Console.BufferHeight);
#else
// return default value for Winforms. If no valid value is returned WriteLine will not be called
return new System.Management.Automation.Host.Size(120, 50);
#endif
}
set
{
#if !noConsole
Console.BufferWidth = value.Width;
Console.BufferHeight = value.Height;
#endif
}
}
public override Coordinates CursorPosition
{
get
{
#if !noConsole
return new Coordinates(Console.CursorLeft, Console.CursorTop);
#else
// Dummywert für Winforms zurückgeben.
return new Coordinates(0, 0);
#endif
}
set
{
#if !noConsole
Console.CursorTop = value.Y;
Console.CursorLeft = value.X;
#endif
}
}
public override int CursorSize
{
get
{
#if !noConsole
return Console.CursorSize;
#else
// Dummywert für Winforms zurückgeben.
return 25;
#endif
}
set
{
#if !noConsole
Console.CursorSize = value;
#endif
}
}
#if noConsole
private Form Invisible_Form = null;
#endif
public override void FlushInputBuffer()
{
#if !noConsole
if (!Console.IsInputRedirected)
{ while (Console.KeyAvailable)
Console.ReadKey(true);
}
#else
if (Invisible_Form != null)
{
Invisible_Form.Close();
Invisible_Form = null;
}
else
{
Invisible_Form = new Form();
Invisible_Form.Opacity = 0;
Invisible_Form.ShowInTaskbar = false;
Invisible_Form.Visible = true;
}
#endif
}
public override ConsoleColor ForegroundColor
{
#if !noConsole
get
{
return Console.ForegroundColor;
}
set
{
Console.ForegroundColor = value;
}
#else
get
{
return GUIForegroundColor;
}
set
{
GUIForegroundColor = value;
}
#endif
}
public override BufferCell[,] GetBufferContents(System.Management.Automation.Host.Rectangle rectangle)
{
#if !noConsole
IntPtr hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR_INFO[,] buffer = new CHAR_INFO[rectangle.Bottom - rectangle.Top + 1, rectangle.Right - rectangle.Left + 1];
COORD buffer_size = new COORD() {X = (short)(rectangle.Right - rectangle.Left + 1), Y = (short)(rectangle.Bottom - rectangle.Top + 1)};
COORD buffer_index = new COORD() {X = 0, Y = 0};
SMALL_RECT screen_rect = new SMALL_RECT() {Left = (short)rectangle.Left, Top = (short)rectangle.Top, Right = (short)rectangle.Right, Bottom = (short)rectangle.Bottom};
ReadConsoleOutput(hStdOut, buffer, buffer_size, buffer_index, ref screen_rect);
System.Management.Automation.Host.BufferCell[,] ScreenBuffer = new System.Management.Automation.Host.BufferCell[rectangle.Bottom - rectangle.Top + 1, rectangle.Right - rectangle.Left + 1];
for (int y = 0; y <= rectangle.Bottom - rectangle.Top; y++)
for (int x = 0; x <= rectangle.Right - rectangle.Left; x++)
{
ScreenBuffer[y,x] = new System.Management.Automation.Host.BufferCell(buffer[y,x].AsciiChar, (System.ConsoleColor)(buffer[y,x].Attributes & 0xF), (System.ConsoleColor)((buffer[y,x].Attributes & 0xF0) / 0x10), System.Management.Automation.Host.BufferCellType.Complete);
}
return ScreenBuffer;
#else
System.Management.Automation.Host.BufferCell[,] ScreenBuffer = new System.Management.Automation.Host.BufferCell[rectangle.Bottom - rectangle.Top + 1, rectangle.Right - rectangle.Left + 1];
for (int y = 0; y <= rectangle.Bottom - rectangle.Top; y++)
for (int x = 0; x <= rectangle.Right - rectangle.Left; x++)
{
ScreenBuffer[y,x] = new System.Management.Automation.Host.BufferCell(' ', GUIForegroundColor, GUIBackgroundColor, System.Management.Automation.Host.BufferCellType.Complete);
}
return ScreenBuffer;
#endif
}
public override bool KeyAvailable
{
get
{
#if !noConsole
return Console.KeyAvailable;
#else
return true;
#endif
}
}
public override System.Management.Automation.Host.Size MaxPhysicalWindowSize
{
get
{
#if !noConsole
return new System.Management.Automation.Host.Size(Console.LargestWindowWidth, Console.LargestWindowHeight);
#else
// Dummy-Wert für Winforms
return new System.Management.Automation.Host.Size(240, 84);
#endif
}
}
public override System.Management.Automation.Host.Size MaxWindowSize
{
get
{
#if !noConsole
return new System.Management.Automation.Host.Size(Console.BufferWidth, Console.BufferWidth);
#else
// Dummy-Wert für Winforms
return new System.Management.Automation.Host.Size(120, 84);
#endif
}
}
public override KeyInfo ReadKey(ReadKeyOptions options)
{
#if !noConsole
ConsoleKeyInfo cki = Console.ReadKey((options & ReadKeyOptions.NoEcho)!=0);
ControlKeyStates cks = 0;
if ((cki.Modifiers & ConsoleModifiers.Alt) != 0)
cks |= ControlKeyStates.LeftAltPressed | ControlKeyStates.RightAltPressed;
if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
cks |= ControlKeyStates.LeftCtrlPressed | ControlKeyStates.RightCtrlPressed;
if ((cki.Modifiers & ConsoleModifiers.Shift) != 0)
cks |= ControlKeyStates.ShiftPressed;
if (Console.CapsLock)
cks |= ControlKeyStates.CapsLockOn;
if (Console.NumberLock)
cks |= ControlKeyStates.NumLockOn;
return new KeyInfo((int)cki.Key, cki.KeyChar, cks, (options & ReadKeyOptions.IncludeKeyDown)!=0);
#else
if ((options & ReadKeyOptions.IncludeKeyDown)!=0)
return ReadKey_Box.Show(WindowTitle, "", true);
else
return ReadKey_Box.Show(WindowTitle, "", false);
#endif
}
public override void ScrollBufferContents(System.Management.Automation.Host.Rectangle source, Coordinates destination, System.Management.Automation.Host.Rectangle clip, BufferCell fill)
{ // no destination block clipping implemented
#if !noConsole
// clip area out of source range?
if ((source.Left > clip.Right) || (source.Right < clip.Left) || (source.Top > clip.Bottom) || (source.Bottom < clip.Top))
{ // clipping out of range -> nothing to do
return;
}
IntPtr hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT lpScrollRectangle = new SMALL_RECT() {Left = (short)source.Left, Top = (short)source.Top, Right = (short)(source.Right), Bottom = (short)(source.Bottom)};
SMALL_RECT lpClipRectangle;
if (clip != null)
{ lpClipRectangle = new SMALL_RECT() {Left = (short)clip.Left, Top = (short)clip.Top, Right = (short)(clip.Right), Bottom = (short)(clip.Bottom)}; }
else
{ lpClipRectangle = new SMALL_RECT() {Left = (short)0, Top = (short)0, Right = (short)(Console.WindowWidth - 1), Bottom = (short)(Console.WindowHeight - 1)}; }
COORD dwDestinationOrigin = new COORD() {X = (short)(destination.X), Y = (short)(destination.Y)};
CHAR_INFO lpFill = new CHAR_INFO() { AsciiChar = fill.Character, Attributes = (ushort)((int)(fill.ForegroundColor) + (int)(fill.BackgroundColor)*16) };
ScrollConsoleScreenBuffer(hStdOut, ref lpScrollRectangle, ref lpClipRectangle, dwDestinationOrigin, ref lpFill);
#endif
}
public override void SetBufferContents(System.Management.Automation.Host.Rectangle rectangle, BufferCell fill)
{
#if !noConsole
// using a trick: move the buffer out of the screen, the source area gets filled with the char fill.Character
if (rectangle.Left >= 0)
Console.MoveBufferArea(rectangle.Left, rectangle.Top, rectangle.Right-rectangle.Left+1, rectangle.Bottom-rectangle.Top+1, BufferSize.Width, BufferSize.Height, fill.Character, fill.ForegroundColor, fill.BackgroundColor);
else
{ // Clear-Host: move all content off the screen
Console.MoveBufferArea(0, 0, BufferSize.Width, BufferSize.Height, BufferSize.Width, BufferSize.Height, fill.Character, fill.ForegroundColor, fill.BackgroundColor);
}
#endif
}
public override void SetBufferContents(Coordinates origin, BufferCell[,] contents)
{
#if !noConsole
IntPtr hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR_INFO[,] buffer = new CHAR_INFO[contents.GetLength(0), contents.GetLength(1)];
COORD buffer_size = new COORD() {X = (short)(contents.GetLength(1)), Y = (short)(contents.GetLength(0))};
COORD buffer_index = new COORD() {X = 0, Y = 0};
SMALL_RECT screen_rect = new SMALL_RECT() {Left = (short)origin.X, Top = (short)origin.Y, Right = (short)(origin.X + contents.GetLength(1) - 1), Bottom = (short)(origin.Y + contents.GetLength(0) - 1)};
for (int y = 0; y < contents.GetLength(0); y++)
for (int x = 0; x < contents.GetLength(1); x++)
{
buffer[y,x] = new CHAR_INFO() { AsciiChar = contents[y,x].Character, Attributes = (ushort)((int)(contents[y,x].ForegroundColor) + (int)(contents[y,x].BackgroundColor)*16) };
}
WriteConsoleOutput(hStdOut, buffer, buffer_size, buffer_index, ref screen_rect);
#endif
}
public override Coordinates WindowPosition
{
get
{
Coordinates s = new Coordinates();
#if !noConsole
s.X = Console.WindowLeft;
s.Y = Console.WindowTop;
#else
// Dummy-Wert für Winforms
s.X = 0;
s.Y = 0;
#endif
return s;
}
set
{
#if !noConsole
Console.WindowLeft = value.X;
Console.WindowTop = value.Y;
#endif
}
}
public override System.Management.Automation.Host.Size WindowSize
{
get
{
System.Management.Automation.Host.Size s = new System.Management.Automation.Host.Size();
#if !noConsole
s.Height = Console.WindowHeight;
s.Width = Console.WindowWidth;
#else
// Dummy-Wert für Winforms
s.Height = 50;
s.Width = 120;
#endif
return s;
}
set
{
#if !noConsole
Console.WindowWidth = value.Width;
Console.WindowHeight = value.Height;
#endif
}
}
public override string WindowTitle
{
get
{
#if !noConsole
return Console.Title;
#else
return GUITitle;
#endif
}
set
{
#if !noConsole
Console.Title = value;
#else
GUITitle = value;
#endif
}
}
}
#if noConsole
public class Input_Box
{
[DllImport("user32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr MB_GetString(uint strId);
public static DialogResult Show(string strTitle, string strPrompt, ref string strVal, bool blSecure)
{
// Generate controls
Form form = new Form();
form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
// Sizes and positions are defined according to the label
// This control has to be finished first
if (string.IsNullOrEmpty(strPrompt))
{
if (blSecure)
label.Text = "Secure input: ";
else
label.Text = "Input: ";
}
else
label.Text = strPrompt;
label.Location = new Point(9, 19);
label.MaximumSize = new System.Drawing.Size(System.Windows.Forms.Screen.FromControl(form).Bounds.Width*5/8 - 18, 0);
label.AutoSize = true;
// Size of the label is defined not before Add()
form.Controls.Add(label);
// Generate textbox
if (blSecure) textBox.UseSystemPasswordChar = true;
textBox.Text = strVal;
textBox.SetBounds(12, label.Bottom, label.Right - 12, 20);
// Generate buttons
// get localized "OK"-string
string sTextOK = Marshal.PtrToStringUni(MB_GetString(0));
if (string.IsNullOrEmpty(sTextOK))
buttonOk.Text = "OK";
else
buttonOk.Text = sTextOK;
// get localized "Cancel"-string
string sTextCancel = Marshal.PtrToStringUni(MB_GetString(1));
if (string.IsNullOrEmpty(sTextCancel))
buttonCancel.Text = "Cancel";
else
buttonCancel.Text = sTextCancel;
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
buttonOk.SetBounds(System.Math.Max(12, label.Right - 158), label.Bottom + 36, 75, 23);
buttonCancel.SetBounds(System.Math.Max(93, label.Right - 77), label.Bottom + 36, 75, 23);
// Configure form
form.Text = strTitle;
form.ClientSize = new System.Drawing.Size(System.Math.Max(178, label.Right + 10), label.Bottom + 71);
form.Controls.AddRange(new Control[] { textBox, buttonOk, buttonCancel });
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
try {
form.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch
{ }
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
// Show form and compute results
DialogResult dialogResult = form.ShowDialog();
strVal = textBox.Text;
return dialogResult;
}
public static DialogResult Show(string strTitle, string strPrompt, ref string strVal)
{
return Show(strTitle, strPrompt, ref strVal, false);
}
}
public class Choice_Box
{
public static int Show(System.Collections.ObjectModel.Collection<ChoiceDescription> arrChoice, int intDefault, string strTitle, string strPrompt)
{
// cancel if array is empty
if (arrChoice == null) return -1;
if (arrChoice.Count < 1) return -1;
// Generate controls
Form form = new Form();
form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
RadioButton[] aradioButton = new RadioButton[arrChoice.Count];
ToolTip toolTip = new ToolTip();
Button buttonOk = new Button();
// Sizes and positions are defined according to the label
// This control has to be finished first when a prompt is available
int iPosY = 19, iMaxX = 0;
if (!string.IsNullOrEmpty(strPrompt))
{
Label label = new Label();
label.Text = strPrompt;
label.Location = new Point(9, 19);
label.MaximumSize = new System.Drawing.Size(System.Windows.Forms.Screen.FromControl(form).Bounds.Width*5/8 - 18, 0);
label.AutoSize = true;
// erst durch Add() wird die Größe des Labels ermittelt
form.Controls.Add(label);
iPosY = label.Bottom;
iMaxX = label.Right;
}
// An den Radiobuttons orientieren sich die weiteren Größen und Positionen
// Diese Controls also jetzt fertigstellen
int Counter = 0;
int tempWidth = System.Windows.Forms.Screen.FromControl(form).Bounds.Width*5/8 - 18;
foreach (ChoiceDescription sAuswahl in arrChoice)
{
aradioButton[Counter] = new RadioButton();
aradioButton[Counter].Text = sAuswahl.Label;
if (Counter == intDefault)
aradioButton[Counter].Checked = true;
aradioButton[Counter].Location = new Point(9, iPosY);
aradioButton[Counter].AutoSize = true;
// erst durch Add() wird die Größe des Labels ermittelt
form.Controls.Add(aradioButton[Counter]);
if (aradioButton[Counter].Width > tempWidth)
{ // radio field to wide for screen -> make two lines
int tempHeight = aradioButton[Counter].Height;
aradioButton[Counter].Height = tempHeight*(1 + (aradioButton[Counter].Width-1)/tempWidth);
aradioButton[Counter].Width = tempWidth;
aradioButton[Counter].AutoSize = false;
}
iPosY = aradioButton[Counter].Bottom;
if (aradioButton[Counter].Right > iMaxX) { iMaxX = aradioButton[Counter].Right; }
if (!string.IsNullOrEmpty(sAuswahl.HelpMessage))
toolTip.SetToolTip(aradioButton[Counter], sAuswahl.HelpMessage);
Counter++;
}
// Tooltip auch anzeigen, wenn Parent-Fenster inaktiv ist
toolTip.ShowAlways = true;
// Button erzeugen
buttonOk.Text = "OK";
buttonOk.DialogResult = DialogResult.OK;
buttonOk.SetBounds(System.Math.Max(12, iMaxX - 77), iPosY + 36, 75, 23);
// configure form
if (string.IsNullOrEmpty(strTitle))
form.Text = System.AppDomain.CurrentDomain.FriendlyName;
else
form.Text = strTitle;
form.ClientSize = new System.Drawing.Size(System.Math.Max(178, iMaxX + 10), iPosY + 71);
form.Controls.Add(buttonOk);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
try {
form.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch
{ }
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
// show and compute form
if (form.ShowDialog() == DialogResult.OK)
{ int iRueck = -1;
for (Counter = 0; Counter < arrChoice.Count; Counter++)
{
if (aradioButton[Counter].Checked == true)
{ iRueck = Counter; }
}
return iRueck;
}
else
return -1;
}
}
public class ReadKey_Box
{
[DllImport("user32.dll")]
public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpKeyState,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)] System.Text.StringBuilder pwszBuff,
int cchBuff, uint wFlags);
static string GetCharFromKeys(Keys keys, bool blShift, bool blAltGr)
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder(64);
byte[] keyboardState = new byte[256];
if (blShift)
{ keyboardState[(int) Keys.ShiftKey] = 0xff; }
if (blAltGr)
{ keyboardState[(int) Keys.ControlKey] = 0xff;
keyboardState[(int) Keys.Menu] = 0xff;
}
if (ToUnicode((uint) keys, 0, keyboardState, buffer, 64, 0) >= 1)
return buffer.ToString();
else
return "\0";
}
class Keyboard_Form : Form
{
public Keyboard_Form()
{
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.KeyDown += new KeyEventHandler(Keyboard_Form_KeyDown);
this.KeyUp += new KeyEventHandler(Keyboard_Form_KeyUp);
}
// check for KeyDown or KeyUp?
public bool checkKeyDown = true;
// key code for pressed key
public KeyInfo keyinfo;
void Keyboard_Form_KeyDown(object sender, KeyEventArgs e)
{
if (checkKeyDown)
{ // store key info
keyinfo.VirtualKeyCode = e.KeyValue;
keyinfo.Character = GetCharFromKeys(e.KeyCode, e.Shift, e.Alt & e.Control)[0];
keyinfo.KeyDown = false;
keyinfo.ControlKeyState = 0;
if (e.Alt) { keyinfo.ControlKeyState = ControlKeyStates.LeftAltPressed | ControlKeyStates.RightAltPressed; }
if (e.Control)
{ keyinfo.ControlKeyState |= ControlKeyStates.LeftCtrlPressed | ControlKeyStates.RightCtrlPressed;
if (!e.Alt)
{ if (e.KeyValue > 64 && e.KeyValue < 96) keyinfo.Character = (char)(e.KeyValue - 64); }
}
if (e.Shift) { keyinfo.ControlKeyState |= ControlKeyStates.ShiftPressed; }
if ((e.Modifiers & System.Windows.Forms.Keys.CapsLock) > 0) { keyinfo.ControlKeyState |= ControlKeyStates.CapsLockOn; }
if ((e.Modifiers & System.Windows.Forms.Keys.NumLock) > 0) { keyinfo.ControlKeyState |= ControlKeyStates.NumLockOn; }
// and close the form
this.Close();
}
}
void Keyboard_Form_KeyUp(object sender, KeyEventArgs e)
{
if (!checkKeyDown)
{ // store key info
keyinfo.VirtualKeyCode = e.KeyValue;
keyinfo.Character = GetCharFromKeys(e.KeyCode, e.Shift, e.Alt & e.Control)[0];
keyinfo.KeyDown = true;
keyinfo.ControlKeyState = 0;
if (e.Alt) { keyinfo.ControlKeyState = ControlKeyStates.LeftAltPressed | ControlKeyStates.RightAltPressed; }
if (e.Control)
{ keyinfo.ControlKeyState |= ControlKeyStates.LeftCtrlPressed | ControlKeyStates.RightCtrlPressed;
if (!e.Alt)
{ if (e.KeyValue > 64 && e.KeyValue < 96) keyinfo.Character = (char)(e.KeyValue - 64); }
}
if (e.Shift) { keyinfo.ControlKeyState |= ControlKeyStates.ShiftPressed; }
if ((e.Modifiers & System.Windows.Forms.Keys.CapsLock) > 0) { keyinfo.ControlKeyState |= ControlKeyStates.CapsLockOn; }
if ((e.Modifiers & System.Windows.Forms.Keys.NumLock) > 0) { keyinfo.ControlKeyState |= ControlKeyStates.NumLockOn; }
// and close the form
this.Close();
}
}
}
public static KeyInfo Show(string strTitle, string strPrompt, bool blIncludeKeyDown)
{
// Controls erzeugen
Keyboard_Form form = new Keyboard_Form();
Label label = new Label();
// Am Label orientieren sich die Größen und Positionen
// Dieses Control also zuerst fertigstellen
if (string.IsNullOrEmpty(strPrompt))
{
label.Text = "Press a key";
}
else
label.Text = strPrompt;
label.Location = new Point(9, 19);
label.MaximumSize = new System.Drawing.Size(System.Windows.Forms.Screen.FromControl(form).Bounds.Width*5/8 - 18, 0);
label.AutoSize = true;
// erst durch Add() wird die Größe des Labels ermittelt
form.Controls.Add(label);
// configure form
form.Text = strTitle;
form.ClientSize = new System.Drawing.Size(System.Math.Max(178, label.Right + 10), label.Bottom + 55);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
try {
form.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch
{ }
form.MinimizeBox = false;
form.MaximizeBox = false;
// show and compute form
form.checkKeyDown = blIncludeKeyDown;
form.ShowDialog();
return form.keyinfo;
}
}
public class Progress_Form : Form
{
private ConsoleColor ProgressBarColor = ConsoleColor.DarkCyan;
private string WindowTitle = "";
#if !noVisualStyles
private System.Timers.Timer timer = new System.Timers.Timer();
private int barNumber = -1;
private int barValue = -1;
private bool inTick = false;
#endif
struct Progress_Data
{
internal Label lbActivity;
internal Label lbStatus;
internal ProgressBar objProgressBar;
internal Label lbRemainingTime;
internal Label lbOperation;
internal int ActivityId;
internal int ParentActivityId;
internal int Depth;
};
private List<Progress_Data> progressDataList = new List<Progress_Data>();
private Color DrawingColor(ConsoleColor color)
{ // convert ConsoleColor to System.Drawing.Color
switch (color)
{
case ConsoleColor.Black: return Color.Black;
case ConsoleColor.Blue: return Color.Blue;
case ConsoleColor.Cyan: return Color.Cyan;
case ConsoleColor.DarkBlue: return ColorTranslator.FromHtml("#000080");
case ConsoleColor.DarkGray: return ColorTranslator.FromHtml("#808080");
case ConsoleColor.DarkGreen: return ColorTranslator.FromHtml("#008000");
case ConsoleColor.DarkCyan: return ColorTranslator.FromHtml("#008080");
case ConsoleColor.DarkMagenta: return ColorTranslator.FromHtml("#800080");
case ConsoleColor.DarkRed: return ColorTranslator.FromHtml("#800000");
case ConsoleColor.DarkYellow: return ColorTranslator.FromHtml("#808000");
case ConsoleColor.Gray: return ColorTranslator.FromHtml("#C0C0C0");
case ConsoleColor.Green: return ColorTranslator.FromHtml("#00FF00");
case ConsoleColor.Magenta: return Color.Magenta;
case ConsoleColor.Red: return Color.Red;
case ConsoleColor.White: return Color.White;
default: return Color.Yellow;
}
}
public Progress_Form()
{
InitializeComponent();
}
public Progress_Form(ConsoleColor BarColor)
{
ProgressBarColor = BarColor;
InitializeComponent();
}
public Progress_Form(string Title, ConsoleColor BarColor)
{
WindowTitle = Title;
ProgressBarColor = BarColor;
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.Text = WindowTitle;
this.Height = 147;
this.Width = 800;
this.BackColor = Color.White;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MinimizeBox = false;
this.MaximizeBox = false;
this.ControlBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout();
#if !noVisualStyles
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimeTick);
timer.Interval = 50; // milliseconds
timer.AutoReset = true;
timer.Start();
#endif
}
#if !noVisualStyles
private void TimeTick(object source, System.Timers.ElapsedEventArgs e)
{ // worker function that is called by timer event
if (inTick) return;
inTick = true;
if (barNumber >= 0)
{
if (barValue >= 0)
{
progressDataList[barNumber].objProgressBar.Value = barValue;
barValue = -1;
}
progressDataList[barNumber].objProgressBar.Refresh();
}
inTick = false;
}
#endif
private void AddBar(ref Progress_Data pd, int position)
{
// Create Label
pd.lbActivity = new Label();
pd.lbActivity.Left = 5;
pd.lbActivity.Top = 104*position + 10;
pd.lbActivity.Width = 800 - 20;
pd.lbActivity.Height = 16;
pd.lbActivity.Font = new Font(pd.lbActivity.Font, FontStyle.Bold);
pd.lbActivity.Text = "";
// Add Label to Form
this.Controls.Add(pd.lbActivity);
// Create Label
pd.lbStatus = new Label();
pd.lbStatus.Left = 25;
pd.lbStatus.Top = 104*position + 26;
pd.lbStatus.Width = 800 - 40;
pd.lbStatus.Height = 16;
pd.lbStatus.Text = "";
// Add Label to Form
this.Controls.Add(pd.lbStatus);
// Create ProgressBar
pd.objProgressBar = new ProgressBar();
pd.objProgressBar.Value = 0;
#if noVisualStyles
pd.objProgressBar.Style = ProgressBarStyle.Continuous;
#else
pd.objProgressBar.Style = ProgressBarStyle.Blocks;
#endif
pd.objProgressBar.ForeColor = DrawingColor(ProgressBarColor);
if (pd.Depth < 15)
{
pd.objProgressBar.Size = new System.Drawing.Size(800 - 60 - 30*pd.Depth, 20);
pd.objProgressBar.Left = 25 + 30*pd.Depth;
}
else
{
pd.objProgressBar.Size = new System.Drawing.Size(800 - 60 - 450, 20);
pd.objProgressBar.Left = 25 + 450;
}
pd.objProgressBar.Top = 104*position + 47;
// Add ProgressBar to Form
this.Controls.Add(pd.objProgressBar);
// Create Label
pd.lbRemainingTime = new Label();
pd.lbRemainingTime.Left = 5;
pd.lbRemainingTime.Top = 104*position + 72;
pd.lbRemainingTime.Width = 800 - 20;
pd.lbRemainingTime.Height = 16;
pd.lbRemainingTime.Text = "";
// Add Label to Form
this.Controls.Add(pd.lbRemainingTime);
// Create Label
pd.lbOperation = new Label();
pd.lbOperation.Left = 25;
pd.lbOperation.Top = 104*position + 88;
pd.lbOperation.Width = 800 - 40;
pd.lbOperation.Height = 16;
pd.lbOperation.Text = "";
// Add Label to Form
this.Controls.Add(pd.lbOperation);
}
public int GetCount()
{
return progressDataList.Count;
}
public void Update(ProgressRecord objRecord)
{
if (objRecord == null)
return;
int currentProgress = -1;
for (int i = 0; i < progressDataList.Count; i++)
{
if (progressDataList[i].ActivityId == objRecord.ActivityId)
{ currentProgress = i;
break;
}
}
if (objRecord.RecordType == ProgressRecordType.Completed)
{
if (currentProgress >= 0)
{
#if !noVisualStyles
if (barNumber == currentProgress) barNumber = -1;
#endif
this.Controls.Remove(progressDataList[currentProgress].lbActivity);
this.Controls.Remove(progressDataList[currentProgress].lbStatus);
this.Controls.Remove(progressDataList[currentProgress].objProgressBar);
this.Controls.Remove(progressDataList[currentProgress].lbRemainingTime);
this.Controls.Remove(progressDataList[currentProgress].lbOperation);
progressDataList[currentProgress].lbActivity.Dispose();
progressDataList[currentProgress].lbStatus.Dispose();
progressDataList[currentProgress].objProgressBar.Dispose();
progressDataList[currentProgress].lbRemainingTime.Dispose();
progressDataList[currentProgress].lbOperation.Dispose();
progressDataList.RemoveAt(currentProgress);
}
if (progressDataList.Count == 0)
{
#if !noVisualStyles
timer.Stop();
timer.Dispose();
#endif
this.Close();
return;
}
if (currentProgress < 0) return;
for (int i = currentProgress; i < progressDataList.Count; i++)
{
progressDataList[i].lbActivity.Top = 104*i + 10;
progressDataList[i].lbStatus.Top = 104*i + 26;
progressDataList[i].objProgressBar.Top = 104*i + 47;
progressDataList[i].lbRemainingTime.Top = 104*i + 72;
progressDataList[i].lbOperation.Top = 104*i + 88;
}
if (104*progressDataList.Count + 43 <= System.Windows.Forms.Screen.FromControl(this).Bounds.Height)
{
this.Height = 104*progressDataList.Count + 43;
this.Location = new Point((System.Windows.Forms.Screen.FromControl(this).Bounds.Width - this.Width)/2, (System.Windows.Forms.Screen.FromControl(this).Bounds.Height - this.Height)/2);
}
else
{
this.Height = System.Windows.Forms.Screen.FromControl(this).Bounds.Height;
this.Location = new Point((System.Windows.Forms.Screen.FromControl(this).Bounds.Width - this.Width)/2, 0);
}
return;
}
if (currentProgress < 0)
{
Progress_Data pd = new Progress_Data();
pd.ActivityId = objRecord.ActivityId;
pd.ParentActivityId = objRecord.ParentActivityId;
pd.Depth = 0;
int nextid = -1;
int parentid = -1;
if (pd.ParentActivityId >= 0)
{
for (int i = 0; i < progressDataList.Count; i++)
{
if (progressDataList[i].ActivityId == pd.ParentActivityId)
{ parentid = i;
break;
}
}
}
if (parentid >= 0)
{
pd.Depth = progressDataList[parentid].Depth + 1;
for (int i = parentid + 1; i < progressDataList.Count; i++)
{
if ((progressDataList[i].Depth < pd.Depth) || ((progressDataList[i].Depth == pd.Depth) && (progressDataList[i].ParentActivityId != pd.ParentActivityId)))
{ nextid = i;
break;
}
}
}
if (nextid == -1)
{
AddBar(ref pd, progressDataList.Count);
currentProgress = progressDataList.Count;
progressDataList.Add(pd);
}
else
{
AddBar(ref pd, nextid);
currentProgress = nextid;
progressDataList.Insert(nextid, pd);
for (int i = currentProgress+1; i < progressDataList.Count; i++)
{
progressDataList[i].lbActivity.Top = 104*i + 10;
progressDataList[i].lbStatus.Top = 104*i + 26;
progressDataList[i].objProgressBar.Top = 104*i + 47;
progressDataList[i].lbRemainingTime.Top = 104*i + 72;
progressDataList[i].lbOperation.Top = 104*i + 88;
}
}
if (104*progressDataList.Count + 43 <= System.Windows.Forms.Screen.FromControl(this).Bounds.Height)
{
this.Height = 104*progressDataList.Count + 43;
this.Location = new Point((System.Windows.Forms.Screen.FromControl(this).Bounds.Width - this.Width)/2, (System.Windows.Forms.Screen.FromControl(this).Bounds.Height - this.Height)/2);
}
else
{
this.Height = System.Windows.Forms.Screen.FromControl(this).Bounds.Height;
this.Location = new Point((System.Windows.Forms.Screen.FromControl(this).Bounds.Width - this.Width)/2, 0);
}
}
if (!string.IsNullOrEmpty(objRecord.Activity))
progressDataList[currentProgress].lbActivity.Text = objRecord.Activity;
else
progressDataList[currentProgress].lbActivity.Text = "";
if (!string.IsNullOrEmpty(objRecord.StatusDescription))
progressDataList[currentProgress].lbStatus.Text = objRecord.StatusDescription;
else
progressDataList[currentProgress].lbStatus.Text = "";
if ((objRecord.PercentComplete >= 0) && (objRecord.PercentComplete <= 100))
{
#if !noVisualStyles
if (objRecord.PercentComplete < 100)
progressDataList[currentProgress].objProgressBar.Value = objRecord.PercentComplete + 1;
else
progressDataList[currentProgress].objProgressBar.Value = 99;
progressDataList[currentProgress].objProgressBar.Visible = true;
barNumber = currentProgress;
barValue = objRecord.PercentComplete;
#else
progressDataList[currentProgress].objProgressBar.Value = objRecord.PercentComplete;
progressDataList[currentProgress].objProgressBar.Visible = true;
#endif
}
else
{ if (objRecord.PercentComplete > 100)
{
progressDataList[currentProgress].objProgressBar.Value = 0;
progressDataList[currentProgress].objProgressBar.Visible = true;
#if !noVisualStyles
barNumber = currentProgress;
barValue = 0;
#endif
}
else
{
progressDataList[currentProgress].objProgressBar.Visible = false;
#if !noVisualStyles
if (barNumber == currentProgress) barNumber = -1;
#endif
}
}
if (objRecord.SecondsRemaining >= 0)
{
System.TimeSpan objTimeSpan = new System.TimeSpan(0, 0, objRecord.SecondsRemaining);
progressDataList[currentProgress].lbRemainingTime.Text = "Remaining time: " + string.Format("{0:00}:{1:00}:{2:00}", (int)objTimeSpan.TotalHours, objTimeSpan.Minutes, objTimeSpan.Seconds);
}
else
progressDataList[currentProgress].lbRemainingTime.Text = "";
if (!string.IsNullOrEmpty(objRecord.CurrentOperation))
progressDataList[currentProgress].lbOperation.Text = objRecord.CurrentOperation;
else
progressDataList[currentProgress].lbOperation.Text = "";
Application.DoEvents();
}
}
#endif
internal class MainModuleUI : PSHostUserInterface
{
private MainModuleRawUI rawUI = null;
public ConsoleColor ErrorForegroundColor = ConsoleColor.Red;
public ConsoleColor ErrorBackgroundColor = ConsoleColor.Black;
public ConsoleColor WarningForegroundColor = ConsoleColor.Yellow;
public ConsoleColor WarningBackgroundColor = ConsoleColor.Black;
public ConsoleColor DebugForegroundColor = ConsoleColor.Yellow;
public ConsoleColor DebugBackgroundColor = ConsoleColor.Black;
public ConsoleColor VerboseForegroundColor = ConsoleColor.Yellow;
public ConsoleColor VerboseBackgroundColor = ConsoleColor.Black;
#if !noConsole
public ConsoleColor ProgressForegroundColor = ConsoleColor.Yellow;
#else
public ConsoleColor ProgressForegroundColor = ConsoleColor.DarkCyan;
#endif
public ConsoleColor ProgressBackgroundColor = ConsoleColor.DarkCyan;
public MainModuleUI() : base()
{
rawUI = new MainModuleRawUI();
#if !noConsole
rawUI.ForegroundColor = Console.ForegroundColor;
rawUI.BackgroundColor = Console.BackgroundColor;
#endif
}
public override Dictionary<string, PSObject> Prompt(string caption, string message, System.Collections.ObjectModel.Collection<FieldDescription> descriptions)
{
#if !noConsole
if (!string.IsNullOrEmpty(caption)) WriteLine(caption);
if (!string.IsNullOrEmpty(message)) WriteLine(message);
#else
if ((!string.IsNullOrEmpty(caption)) || (!string.IsNullOrEmpty(message)))
{ string sTitel = System.AppDomain.CurrentDomain.FriendlyName, sMeldung = "";
if (!string.IsNullOrEmpty(caption)) sTitel = caption;
if (!string.IsNullOrEmpty(message)) sMeldung = message;
MessageBox.Show(sMeldung, sTitel);
}
// Labeltext für Input_Box zurücksetzen
ib_message = "";
#endif
Dictionary<string, PSObject> ret = new Dictionary<string, PSObject>();
foreach (FieldDescription cd in descriptions)
{
Type t = null;
if (string.IsNullOrEmpty(cd.ParameterAssemblyFullName))
t = typeof(string);
else
t = Type.GetType(cd.ParameterAssemblyFullName);
if (t.IsArray)
{
Type elementType = t.GetElementType();
Type genericListType = Type.GetType("System.Collections.Generic.List"+((char)0x60).ToString()+"1");
genericListType = genericListType.MakeGenericType(new Type[] { elementType });
ConstructorInfo constructor = genericListType.GetConstructor(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);
object resultList = constructor.Invoke(null);
int index = 0;
string data = "";
do
{
try
{
#if !noConsole
if (!string.IsNullOrEmpty(cd.Name)) Write(string.Format("{0}[{1}]: ", cd.Name, index));
#else
if (!string.IsNullOrEmpty(cd.Name)) ib_message = string.Format("{0}[{1}]: ", cd.Name, index);
#endif
data = ReadLine();
if (string.IsNullOrEmpty(data))
break;
object o = System.Convert.ChangeType(data, elementType);
genericListType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, resultList, new object[] { o });
}
catch (Exception e)
{
throw e;
}
index++;
} while (true);
System.Array retArray = (System.Array )genericListType.InvokeMember("ToArray", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, resultList, null);
ret.Add(cd.Name, new PSObject(retArray));
}
else
{
object o = null;
string l = null;
try
{
if (t != typeof(System.Security.SecureString))
{
if (t != typeof(System.Management.Automation.PSCredential))
{
#if !noConsole
if (!string.IsNullOrEmpty(cd.Name)) Write(cd.Name);
if (!string.IsNullOrEmpty(cd.HelpMessage)) Write(" (Type !? for help.)");
if ((!string.IsNullOrEmpty(cd.Name)) || (!string.IsNullOrEmpty(cd.HelpMessage))) Write(": ");
#else
if (!string.IsNullOrEmpty(cd.Name)) ib_message = string.Format("{0}: ", cd.Name);
if (!string.IsNullOrEmpty(cd.HelpMessage)) ib_message += "\n(Type !? for help.)";
#endif
do {
l = ReadLine();
if (l == "!?")
WriteLine(cd.HelpMessage);
else
{
if (string.IsNullOrEmpty(l)) o = cd.DefaultValue;
if (o == null)
{
try {
o = System.Convert.ChangeType(l, t);
}
catch {
Write("Wrong format, please repeat input: ");
l = "!?";
}
}
}
} while (l == "!?");
}
else
{
PSCredential pscred = PromptForCredential("", "", "", "");
o = pscred;
}
}
else
{
#if !noConsole
if (!string.IsNullOrEmpty(cd.Name)) Write(string.Format("{0}: ", cd.Name));
#else
if (!string.IsNullOrEmpty(cd.Name)) ib_message = string.Format("{0}: ", cd.Name);
#endif
SecureString pwd = null;
pwd = ReadLineAsSecureString();
o = pwd;
}
ret.Add(cd.Name, new PSObject(o));
}
catch (Exception e)
{
throw e;
}
}
}
#if noConsole
// Labeltext für Input_Box zurücksetzen
ib_message = "";
#endif
return ret;
}
public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice)
{
#if noConsole
int iReturn = Choice_Box.Show(choices, defaultChoice, caption, message);
if (iReturn == -1) { iReturn = defaultChoice; }
return iReturn;
#else
if (!string.IsNullOrEmpty(caption)) WriteLine(caption);
WriteLine(message);
do {
int idx = 0;
SortedList<string, int> res = new SortedList<string, int>();
string defkey = "";
foreach (ChoiceDescription cd in choices)
{
string lkey = cd.Label.Substring(0, 1), ltext = cd.Label;
int pos = cd.Label.IndexOf('&');
if (pos > -1)
{
lkey = cd.Label.Substring(pos + 1, 1).ToUpper();
if (pos > 0)
ltext = cd.Label.Substring(0, pos) + cd.Label.Substring(pos + 1);
else
ltext = cd.Label.Substring(1);
}
res.Add(lkey.ToLower(), idx);
if (idx > 0) Write(" ");
if (idx == defaultChoice)
{
Write(VerboseForegroundColor, rawUI.BackgroundColor, string.Format("[{0}] {1}", lkey, ltext));
defkey = lkey;
}
else
Write(rawUI.ForegroundColor, rawUI.BackgroundColor, string.Format("[{0}] {1}", lkey, ltext));
idx++;
}
Write(rawUI.ForegroundColor, rawUI.BackgroundColor, string.Format(" [?] Help (default is \"{0}\"): ", defkey));
string inpkey = "";
try
{
inpkey = Console.ReadLine().ToLower();
if (res.ContainsKey(inpkey)) return res[inpkey];
if (string.IsNullOrEmpty(inpkey)) return defaultChoice;
}
catch { }
if (inpkey == "?")
{
foreach (ChoiceDescription cd in choices)
{
string lkey = cd.Label.Substring(0, 1);
int pos = cd.Label.IndexOf('&');
if (pos > -1) lkey = cd.Label.Substring(pos + 1, 1).ToUpper();
if (!string.IsNullOrEmpty(cd.HelpMessage))
WriteLine(rawUI.ForegroundColor, rawUI.BackgroundColor, string.Format("{0} - {1}", lkey, cd.HelpMessage));
else
WriteLine(rawUI.ForegroundColor, rawUI.BackgroundColor, string.Format("{0} -", lkey));
}
}
} while (true);
#endif
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
#if (!noConsole && !credentialGUI)
if (!string.IsNullOrEmpty(caption)) WriteLine(caption);
WriteLine(message);
string un;
if ((string.IsNullOrEmpty(userName)) || ((options & PSCredentialUIOptions.ReadOnlyUserName) == 0))
{
Write("User name: ");
un = ReadLine();
}
else
{
Write("User name: ");
if (!string.IsNullOrEmpty(targetName)) Write(targetName + "\\");
WriteLine(userName);
un = userName;
}
SecureString pwd = null;
Write("Password: ");
pwd = ReadLineAsSecureString();
if (string.IsNullOrEmpty(un)) un = "<NOUSER>";
if (!string.IsNullOrEmpty(targetName))
{
if (un.IndexOf('\\') < 0)
un = targetName + "\\" + un;
}
PSCredential c2 = new PSCredential(un, pwd);
return c2;
#else
Credential_Form.User_Pwd cred = Credential_Form.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;
#endif
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
#if (!noConsole && !credentialGUI)
if (!string.IsNullOrEmpty(caption)) WriteLine(caption);
WriteLine(message);
string un;
if (string.IsNullOrEmpty(userName))
{
Write("User name: ");
un = ReadLine();
}
else
{
Write("User name: ");
if (!string.IsNullOrEmpty(targetName)) Write(targetName + "\\");
WriteLine(userName);
un = userName;
}
SecureString pwd = null;
Write("Password: ");
pwd = ReadLineAsSecureString();
if (string.IsNullOrEmpty(un)) un = "<NOUSER>";
if (!string.IsNullOrEmpty(targetName))
{
if (un.IndexOf('\\') < 0)
un = targetName + "\\" + un;
}
PSCredential c2 = new PSCredential(un, pwd);
return c2;
#else
Credential_Form.User_Pwd cred = Credential_Form.PromptForPassword(caption, message, targetName, userName, PSCredentialTypes.Default, PSCredentialUIOptions.Default);
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;
#endif
}
public override PSHostRawUserInterface RawUI
{
get
{
return rawUI;
}
}
#if noConsole
private string ib_message;
#endif
public override string ReadLine()
{
#if !noConsole
return Console.ReadLine();
#else
string sWert = "";
if (Input_Box.Show(rawUI.WindowTitle, ib_message, ref sWert) == DialogResult.OK)
return sWert;
else
#endif
#if (noConsole && exitOnCancel)
Environment.Exit(1);
return "";
#else
return "";
#endif
}
private System.Security.SecureString getPassword()
{
System.Security.SecureString pwd = new System.Security.SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.RemoveAt(pwd.Length - 1);
Console.Write("\b \b");
}
}
else if (i.KeyChar != '\u0000')
{
pwd.AppendChar(i.KeyChar);
Console.Write("*");
}
}
return pwd;
}
public override System.Security.SecureString ReadLineAsSecureString()
{
System.Security.SecureString secstr = new System.Security.SecureString();
#if !noConsole
secstr = getPassword();
#else
string sWert = "";
if (Input_Box.Show(rawUI.WindowTitle, ib_message, ref sWert, true) == DialogResult.OK)
{
foreach (char ch in sWert)
secstr.AppendChar(ch);
}
#endif
#if (noConsole && exitOnCancel)
else
Environment.Exit(1);
#endif
return secstr;
}
// called by Write-Host
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
#if (!noOutput && !noConsole)
ConsoleColor fgc = Console.ForegroundColor, bgc = Console.BackgroundColor;
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
Console.Write(value);
Console.ForegroundColor = fgc;
Console.BackgroundColor = bgc;
#else
if ((!string.IsNullOrEmpty(value)) && (value != "\n"))
MessageBox.Show(value, rawUI.WindowTitle);
#endif
}
public override void Write(string value)
{
#if (!noOutput && !noConsole)
Console.Write(value);
#else
if ((!string.IsNullOrEmpty(value)) && (value != "\n"))
MessageBox.Show(value, rawUI.WindowTitle);
#endif
}
// called by Write-Debug
public override void WriteDebugLine(string message)
{
#if (!noError && !noConsole)
WriteLineInternal(DebugForegroundColor, DebugBackgroundColor, string.Format("DEBUG: {0}", message));
#else
MessageBox.Show(message, rawUI.WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
}
// called by Write-Error
public override void WriteErrorLine(string value)
{
#if (!noError && !noConsole)
if (Console.IsErrorRedirected)
Console.Error.WriteLine(string.Format("ERROR: {0}", value));
else
WriteLineInternal(ErrorForegroundColor, ErrorBackgroundColor, string.Format("ERROR: {0}", value));
#else
MessageBox.Show(value, rawUI.WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
}
public override void WriteLine()
{
#if (!noOutput && !noConsole)
Console.WriteLine();
#else
MessageBox.Show("", rawUI.WindowTitle);
#endif
}
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
#if (!noOutput && !noConsole)
ConsoleColor fgc = Console.ForegroundColor, bgc = Console.BackgroundColor;
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
Console.WriteLine(value);
Console.ForegroundColor = fgc;
Console.BackgroundColor = bgc;
#else
if ((!string.IsNullOrEmpty(value)) && (value != "\n"))
MessageBox.Show(value, rawUI.WindowTitle);
#endif
}
#if (!noError && !noConsole)
private void WriteLineInternal(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
ConsoleColor fgc = Console.ForegroundColor, bgc = Console.BackgroundColor;
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
Console.WriteLine(value);
Console.ForegroundColor = fgc;
Console.BackgroundColor = bgc;
}
#endif
// called by Write-Output
public override void WriteLine(string value)
{
#if (!noOutput && !noConsole)
Console.WriteLine(value);
#else
if ((!string.IsNullOrEmpty(value)) && (value != "\n"))
MessageBox.Show(value, rawUI.WindowTitle);
#endif
}
#if noConsole
public Progress_Form pf = null;
#endif
public override void WriteProgress(long sourceId, ProgressRecord record)
{
#if noConsole
if (pf == null)
{
if (record.RecordType == ProgressRecordType.Completed) return;
pf = new Progress_Form(rawUI.WindowTitle, ProgressForegroundColor);
pf.Show();
}
pf.Update(record);
if (record.RecordType == ProgressRecordType.Completed)
{
if (pf.GetCount() == 0) pf = null;
}
#endif
}
// called by Write-Verbose
public override void WriteVerboseLine(string message)
{
#if (!noOutput && !noConsole)
WriteLine(VerboseForegroundColor, VerboseBackgroundColor, string.Format("VERBOSE: {0}", message));
#else
MessageBox.Show(message, rawUI.WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
}
// called by Write-Warning
public override void WriteWarningLine(string message)
{
#if (!noError && !noConsole)
WriteLineInternal(WarningForegroundColor, WarningBackgroundColor, string.Format("WARNING: {0}", message));
#else
MessageBox.Show(message, rawUI.WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif
}
}
internal class MainModule : PSHost
{
private MainAppInterface parent;
private MainModuleUI ui = null;
private CultureInfo originalCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
private CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
private Guid myId = Guid.NewGuid();
public MainModule(MainAppInterface app, MainModuleUI ui)
{
this.parent = app;
this.ui = ui;
}
public class ConsoleColorProxy
{
private MainModuleUI _ui;
public ConsoleColorProxy(MainModuleUI ui)
{
if (ui == null) throw new ArgumentNullException("ui");
_ui = ui;
}
public ConsoleColor ErrorForegroundColor
{
get
{ return _ui.ErrorForegroundColor; }
set
{ _ui.ErrorForegroundColor = value; }
}
public ConsoleColor ErrorBackgroundColor
{
get
{ return _ui.ErrorBackgroundColor; }
set
{ _ui.ErrorBackgroundColor = value; }
}
public ConsoleColor WarningForegroundColor
{
get
{ return _ui.WarningForegroundColor; }
set
{ _ui.WarningForegroundColor = value; }
}
public ConsoleColor WarningBackgroundColor
{
get
{ return _ui.WarningBackgroundColor; }
set
{ _ui.WarningBackgroundColor = value; }
}
public ConsoleColor DebugForegroundColor
{
get
{ return _ui.DebugForegroundColor; }
set
{ _ui.DebugForegroundColor = value; }
}
public ConsoleColor DebugBackgroundColor
{
get
{ return _ui.DebugBackgroundColor; }
set
{ _ui.DebugBackgroundColor = value; }
}
public ConsoleColor VerboseForegroundColor
{
get
{ return _ui.VerboseForegroundColor; }
set
{ _ui.VerboseForegroundColor = value; }
}
public ConsoleColor VerboseBackgroundColor
{
get
{ return _ui.VerboseBackgroundColor; }
set
{ _ui.VerboseBackgroundColor = value; }
}
public ConsoleColor ProgressForegroundColor
{
get
{ return _ui.ProgressForegroundColor; }
set
{ _ui.ProgressForegroundColor = value; }
}
public ConsoleColor ProgressBackgroundColor
{
get
{ return _ui.ProgressBackgroundColor; }
set
{ _ui.ProgressBackgroundColor = value; }
}
}
public override PSObject PrivateData
{
get
{
if (ui == null) return null;
return _consoleColorProxy ?? (_consoleColorProxy = PSObject.AsPSObject(new ConsoleColorProxy(ui)));
}
}
private PSObject _consoleColorProxy;
public override System.Globalization.CultureInfo CurrentCulture
{
get
{
return this.originalCultureInfo;
}
}
public override System.Globalization.CultureInfo CurrentUICulture
{
get
{
return this.originalUICultureInfo;
}
}
public override Guid InstanceId
{
get
{
return this.myId;
}
}
public override string Name
{
get
{
return "PSRunspace-Host";
}
}
public override PSHostUserInterface UI
{
get
{
return ui;
}
}
public override Version Version
{
get
{
return new Version(0, 5, 0, 33);
}
}
public override void EnterNestedPrompt()
{
}
public override void ExitNestedPrompt()
{
}
public override void NotifyBeginApplication()
{
return;
}
public override void NotifyEndApplication()
{
return;
}
public override void SetShouldExit(int exitCode)
{
this.parent.ShouldExit = true;
this.parent.ExitCode = exitCode;
}
}
internal interface MainAppInterface
{
bool ShouldExit { get; set; }
int ExitCode { get; set; }
}
internal class MainApp : MainAppInterface
{
private bool shouldExit;
private int exitCode;
public bool ShouldExit
{
get { return this.shouldExit; }
set { this.shouldExit = value; }
}
public int ExitCode
{
get { return this.exitCode; }
set { this.exitCode = value; }
}
#if conHost
[DllImport("kernel32.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AllocConsole();
#endif
#if STA
[STAThread]
#else
[MTAThread]
#endif
private static int Main(string[] args)
{
#if conHost
// before this command no console should be attached or allocation fails
if (!AllocConsole()) { Console.Error.WriteLine("Creation of console failed!"); }
// connect STDIN
Console.SetIn(new System.IO.StreamReader(Console.OpenStandardInput()));
// connect STDOUT
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(Console.OpenStandardOutput());
streamWriter.AutoFlush = true;
Console.SetOut(streamWriter);
// connect STDERR
System.IO.StreamWriter errorWriter = new System.IO.StreamWriter(Console.OpenStandardOutput());
errorWriter.AutoFlush = true;
Console.SetError(errorWriter);
#endif
#if (!noConsole && UNICODEEncoding)
Console.OutputEncoding = new System.Text.UnicodeEncoding();
#endif
$culture
#if (!noVisualStyles && noConsole)
Application.EnableVisualStyles();
#endif
MainApp me = new MainApp();
bool paramWait = false;
string extractFN = string.Empty;
MainModuleUI ui = new MainModuleUI();
MainModule host = new MainModule(me, ui);
System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
try
{
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(host))
{
#if STA
myRunSpace.ApartmentState = System.Threading.ApartmentState.STA;
#else
myRunSpace.ApartmentState = System.Threading.ApartmentState.MTA;
#endif
myRunSpace.Open();
using (PowerShell posh = PowerShell.Create())
{
#if !noConsole
Console.CancelKeyPress += new ConsoleCancelEventHandler(delegate(object sender, ConsoleCancelEventArgs e)
{
try
{
posh.BeginStop(new AsyncCallback(delegate(IAsyncResult r)
{
mre.Set();
e.Cancel = true;
}), null);
}
catch
{
};
});
#endif
posh.Runspace = myRunSpace;
posh.Streams.Error.DataAdded += new EventHandler<DataAddedEventArgs>(delegate(object sender, DataAddedEventArgs e)
{
ui.WriteErrorLine(((PSDataCollection<ErrorRecord>)sender)[e.Index].Exception.Message);
});
PSDataCollection<string> colInput = new PSDataCollection<string>();
if (Console.IsInputRedirected)
{ // read standard input
string sItem = "";
while ((sItem = Console.ReadLine()) != null)
{ // add to powershell pipeline
colInput.Add(sItem);
}
}
colInput.Complete();
PSDataCollection<PSObject> colOutput = new PSDataCollection<PSObject>();
colOutput.DataAdded += new EventHandler<DataAddedEventArgs>(delegate(object sender, DataAddedEventArgs e)
{
ui.WriteLine(((PSDataCollection<PSObject>)sender)[e.Index].ToString());
});
int separator = 0;
int idx = 0;
bool bHelp = false;
string sHelp = "";
foreach (string s in args)
{
if (string.Compare(s, "-wait", true) == 0)
paramWait = true;
else if (s.StartsWith("-extract", StringComparison.InvariantCultureIgnoreCase))
{
string[] s1 = s.Split(new string[] { ":" }, 2, StringSplitOptions.RemoveEmptyEntries);
if (s1.Length != 2)
{
#if !noConsole
Console.WriteLine("If you specify the -extract option you need to add a file for extraction in this way\r\n -extract:\"<filename>\"");
#else
MessageBox.Show("If you specify the -extract option you need to add a file for extraction in this way\r\n -extract:\"<filename>\"", System.AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
return 1;
}
extractFN = s1[1].Trim(new char[] { '\"' });
}
else if (string.Compare(s, "-end", true) == 0)
{
separator = idx + 1;
break;
}
else if (string.Compare(s, "-?", true) == 0)
{
bHelp = true;
}
else if (bHelp)
{
if ((string.Compare(s, "-detailed", true) == 0) || (string.Compare(s, "-examples", true) == 0) || (string.Compare(s, "-full", true) == 0))
{
sHelp = s;
}
}
else if (string.Compare(s, "-debug", true) == 0)
{
System.Diagnostics.Debugger.Launch();
break;
}
idx++;
}
Assembly executingAssembly = Assembly.GetExecutingAssembly();
$EMBEDSECTION
using (System.IO.Stream scriptstream = executingAssembly.GetManifestResourceStream("$([System.IO.Path]::GetFileName($inputFile))"))
{
using (System.IO.StreamReader scriptreader = new System.IO.StreamReader(scriptstream, System.Text.Encoding.UTF8))
{
string script = scriptreader.ReadToEnd();
if (!string.IsNullOrEmpty(extractFN))
{
System.IO.File.WriteAllText(extractFN, script);
return 0;
}
if (bHelp)
{ // help selected
posh.AddScript("function " + System.AppDomain.CurrentDomain.FriendlyName + "{" + script + "}; Get-Help " + System.AppDomain.CurrentDomain.FriendlyName + " " + sHelp + " | Out-String");
} else { // execution selected
posh.AddScript(script);
}
}
}
if (!bHelp)
{ // only if no help selected
// parse parameters
string argbuffer = null;
// regex for named parameters
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^-([^: ]+)[ :]?([^:]*)$");
for (int i = separator; i < args.Length; i++)
{
System.Text.RegularExpressions.Match match = regex.Match(args[i]);
double dummy;
if ((match.Success && match.Groups.Count == 3) && (!Double.TryParse(args[i], out dummy)))
{ // parameter in powershell style, means named parameter found
if (argbuffer != null) // already a named parameter in buffer, then flush it
posh.AddParameter(argbuffer);
if (match.Groups[2].Value.Trim() == "")
{ // store named parameter in buffer
argbuffer = match.Groups[1].Value;
}
else
// caution: when called in powershell $TRUE gets converted, when called in cmd.exe not
if ((match.Groups[2].Value == "$TRUE") || (match.Groups[2].Value.ToUpper() == "\x24TRUE"))
{ // switch found
posh.AddParameter(match.Groups[1].Value, true);
argbuffer = null;
}
else
// caution: when called in powershell $FALSE gets converted, when called in cmd.exe not
if ((match.Groups[2].Value == "$FALSE") || (match.Groups[2].Value.ToUpper() == "\x24"+"FALSE"))
{ // switch found
posh.AddParameter(match.Groups[1].Value, false);
argbuffer = null;
}
else
{ // named parameter with value found
posh.AddParameter(match.Groups[1].Value, match.Groups[2].Value);
argbuffer = null;
}
}
else
{ // unnamed parameter found
if (argbuffer != null)
{ // already a named parameter in buffer, so this is the value
posh.AddParameter(argbuffer, args[i]);
argbuffer = null;
}
else
{ // position parameter found
posh.AddArgument(args[i]);
}
}
}
if (argbuffer != null) posh.AddParameter(argbuffer); // flush parameter buffer...
// convert output to strings
posh.AddCommand("Out-String");
// with a single string per line
posh.AddParameter("Stream");
}
posh.BeginInvoke<string, PSObject>(colInput, colOutput, null, new AsyncCallback(delegate(IAsyncResult ar)
{
if (ar.IsCompleted)
mre.Set();
}), null);
while (!me.ShouldExit && !mre.WaitOne(100))
{ };
posh.Stop();
if (posh.InvocationStateInfo.State == PSInvocationState.Failed)
ui.WriteErrorLine(posh.InvocationStateInfo.Reason.Message);
}
myRunSpace.Close();
}
}
catch (Exception ex)
{
#if (!noError && !noConsole)
Console.Write("An exception occured: ");
Console.WriteLine(ex.Message);
#else
MessageBox.Show("An exception occured: " + ex.Message, System.AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
}
if (paramWait)
{
#if !noConsole
Console.WriteLine("Hit any key to exit...");
Console.ReadKey();
#else
MessageBox.Show("Click OK to exit...", System.AppDomain.CurrentDomain.FriendlyName);
#endif
}
return me.ExitCode;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
throw new Exception("Unhandled exception in " + System.AppDomain.CurrentDomain.FriendlyName);
}
}
}
"@
} # END Get-MainProgram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment