Created
October 4, 2016 14:36
-
-
Save MSAdministrator/d4b809b7bb65e881d94e5eecd98f2e8e to your computer and use it in GitHub Desktop.
Startup Function for PowerShell Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Main { | |
<# | |
.SYNOPSIS | |
The Main function starts the project application. | |
.PARAMETER Commandline | |
$Commandline contains the complete argument string passed to the script packager executable. | |
.NOTES | |
Use this function to initialize your script and to call GUI forms. | |
.NOTES | |
To get the console output in the Packager (Forms Engine) use: | |
$ConsoleOutput (Type: System.Collections.ArrayList) | |
#> | |
Param ([String]$Commandline) | |
if (-not $Commandline) | |
{ | |
#$Commandline = '"-NoRestart" "True" "-Force" "False" "-Quiet" "True" "-Help" "True"' | |
} | |
$Dictionary = New-Object System.Collections.Specialized.StringDictionary | |
$ParsedInput = Convert-CommandLineToDictionary -Dictionary $Dictionary -CommandLine $Commandline -ParamIndicator '-' | |
$NoRestart = '' | |
$Force = '' | |
$Help = '' | |
foreach ($item in $ParsedInput) | |
{ | |
switch ($item.Key) { | |
NoRestart { | |
$NoRestart = $item.Value | |
} | |
Force { | |
$Force = $item.Value | |
} | |
help { | |
$Help = @" | |
#ADD YOUR HELP INFO HERE FOR THE EXE | |
"@ | |
#exit | |
} | |
} | |
} | |
if (-not $Commandline) | |
{ | |
Call-MainForm_psf | |
} | |
else | |
{ | |
if ($null -ne $help) | |
{ | |
return $Help | |
} | |
if ($Quiet) | |
{ | |
#Call Main Function Uninstall-Reporter.ps1 | |
} | |
else | |
{ | |
Call-MainForm_psf -NoRestart $NoRestart -Force $Force | |
} | |
} | |
$global:ExitCode = 0 #Set the exit code for the Packager | |
} | |
function Parse-Commandline | |
{ | |
<# | |
.SYNOPSIS | |
Parses the Commandline of a package executable | |
.DESCRIPTION | |
Parses the Commandline of a package executable | |
.PARAMETER Commandline | |
The Commandline of the package executable | |
.EXAMPLE | |
$arguments = Parse-Commandline -Commandline $Commandline | |
.INPUTS | |
System.String | |
.OUTPUTS | |
System.Collections.Specialized.StringCollection | |
#> | |
[OutputType([System.Collections.Specialized.StringCollection])] | |
Param([string]$CommandLine) | |
$Arguments = New-Object System.Collections.Specialized.StringCollection | |
if($CommandLine) | |
{ | |
#Find First Quote | |
$index = $CommandLine.IndexOf('"') | |
while ( $index -ne -1) | |
{#Continue as along as we find a quote | |
#Find Closing Quote | |
$closeIndex = $CommandLine.IndexOf('"',$index + 1) | |
if($closeIndex -eq -1) | |
{ | |
break #Can’t find a match | |
} | |
$value = $CommandLine.Substring($index + 1,$closeIndex – ($index + 1)) | |
[void]$Arguments.Add($value) | |
$index = $closeIndex | |
#Find First Quote | |
$index = $CommandLine.IndexOf('"',$index + 1) | |
} | |
} | |
return ,$Arguments | |
} | |
function Convert-CommandLineToDictionary | |
{ | |
<# | |
.SYNOPSIS | |
Parses and converts the commandline of a packaged executable into a Dictionary | |
.DESCRIPTION | |
Parses and converts the commandline of a packaged executable into a Dictionary | |
.PARAMETER Dictionary | |
The Dictionary to load the value pairs into. | |
.PARAMETER CommandLine | |
The commandline of the package executable | |
.PARAMETER ParamIndicator | |
The character used to indicate what is a parameter. | |
.EXAMPLE | |
$Dictionary = New-Object System.Collections.Specialized.StringDictionary | |
Convert-CommandLineToDictionary -Dictionary $Dictionary -CommandLine $Commandline -ParamIndicator '-' | |
#> | |
Param( [ValidateNotNull()] | |
[System.Collections.Specialized.StringDictionary]$Dictionary, | |
[string]$CommandLine, | |
[char] $ParamIndicator) | |
$Params = Parse-Commandline $CommandLine | |
for($index = 0; $index -lt $Params.Count; $index++) | |
{ | |
[string]$param = $Params[$index] | |
#Clear the values | |
$key = "" | |
$value = "" | |
if($param.StartsWith($ParamIndicator)) | |
{ | |
#Remove the indicator | |
$key = $param.Remove(0,1) | |
if($index + 1 -lt $Params.Count) | |
{ | |
#Check if the next Argument is a parameter | |
[string]$param = $Params[$index + 1] | |
if($param.StartsWith($ParamIndicator) -ne $true ) | |
{ | |
#If it isn’t a parameter then set it as the value | |
$value = $param | |
$index++ | |
} | |
} | |
$Dictionary[$key] = $value | |
}#else skip | |
} | |
return $Dictionary | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment