Hi,
I have this exe (healthcheck of a computer) which I would like to run as a scheduled job. So if a user runs it, it get the healthcheck report, if it is ran on the computer as a scheduled job, it should not show the gui.
Can that be done?
So that would be myexe.exe -NOGUI in a scheduled job.
S.
Run exe with parameter -NOGUI?
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Run exe with parameter -NOGUI?
Ok, so I probably have to detect the -nogui switch somehow, then I can just launch the command else the form.
So how would I do this if ($NOGUISwitchUsed) {execute script} else { show-form} ?
So how would I do this if ($NOGUISwitchUsed) {execute script} else { show-form} ?
Re: Run exe with parameter -NOGUI?
Compiled Exe do not work with standard powershell parameter handling!
@Sapien: Would be good to add Parameter parsing code for exe-Projects by default.
I use this construction:
@Sapien: Would be good to add Parameter parsing code for exe-Projects by default.
I use this construction:
Code: Select all
function Convert-CommandLineToDictionary {
<#
.SYNOPSIS
Parses and converts the command line of a packaged executable into a Dictionary
.DESCRIPTION
Parses and converts the command line of a packaged executable into a Dictionary
.PARAMETER Dictionary
The Dictionary to load the value pairs into.
.PARAMETER CommandLine
The command line 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
}
} # < END Function Convert-CommandLineToDictionary
function Parse-Commandline {
<#
.SYNOPSIS
Parses the command line of a package executable
.DESCRIPTION
Parses the command line of a package executable
.PARAMETER Commandline
The command line 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
} # < END Function Parse-Commandline
#######################################################################
function Main {
Param ([String]$Commandline)
$P = Convert-CommandLineToDictionary -Dictionary $Dictionary -CommandLine $Commandline -ParamIndicator "-"
# Parameters are in the Dictionary!
# code
}
Re: Run exe with parameter -NOGUI?
Place the following code at the top of startup.pss:
Call the exe like this:
Example.exe -Myparam2 hello2 -MyParam1 hello1
You will see that the parameters are passed correctly. The Sapien host parse them for you and also the project hands you the raw command line in "Main"
- #Define a Param block to use custom parameters in the project
- Param(
- $MyParam1,
- $MyParam2
- )
- [System.Windows.Forms.MessageBox]::Show("P1 = $MyParam1`nP2 = $MyParam2")
Example.exe -Myparam2 hello2 -MyParam1 hello1
You will see that the parameters are passed correctly. The Sapien host parse them for you and also the project hands you the raw command line in "Main"
Re: Run exe with parameter -NOGUI?
If you want to check a bunch of parameters then we can do this:
Notice, too, that they also bind by default position:
Example.exe hello2 hello1
- #Define a Param block to use custom parameters in the project
- Param(
- $MyParam1,
- $MyParam2
- )
- [System.Windows.Forms.MessageBox]::Show(($PSBoundParameters|ft|Out-String))
- function Main {
Example.exe hello2 hello1
Re: Run exe with parameter -NOGUI?
An interesting side effect is that switch parameters can be declared but will not set the value correctly.
To detect a switch do this.
$PSBoundParameters.ContainsKey('NoGui')
To detect a switch do this.
$PSBoundParameters.ContainsKey('NoGui')
Re: Run exe with parameter -NOGUI?
Sorry!
I built this workaround 6 weeks before the Parameter Handling for exe file was updated in this Blog:
https://www.sapien.com/blog/2015/11/30/ ... able-file/
I built this workaround 6 weeks before the Parameter Handling for exe file was updated in this Blog:
https://www.sapien.com/blog/2015/11/30/ ... able-file/