Run exe with parameter -NOGUI?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 7 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Run exe with parameter -NOGUI?

Post by stevens »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

Yes but you would have to code it to work that way.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Run exe with parameter -NOGUI?

Post by stevens »

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} ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

Param(
[switch]$NoGui
}
if($NoGUI({
....
}else{
....
}
User avatar
CermakPOI
Posts: 41
Last visit: Wed Jan 31, 2024 2:57 am

Re: Run exe with parameter -NOGUI?

Post by CermakPOI »

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:

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
}

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

Parameters are passed to EXE correctly. Why do you think they aren't?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

Place the following code at the top of startup.pss:
  1. #Define a Param block to use custom parameters in the project
  2. Param(
  3.     $MyParam1,
  4.     $MyParam2
  5. )
  6.  
  7. [System.Windows.Forms.MessageBox]::Show("P1 = $MyParam1`nP2 = $MyParam2")
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"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

If you want to check a bunch of parameters then we can do this:
  1. #Define a Param block to use custom parameters in the project
  2. Param(
  3.     $MyParam1,
  4.     $MyParam2
  5. )
  6. [System.Windows.Forms.MessageBox]::Show(($PSBoundParameters|ft|Out-String))
  7.  
  8. function Main {
Notice, too, that they also bind by default position:

Example.exe hello2 hello1
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Run exe with parameter -NOGUI?

Post by jvierra »

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')
User avatar
CermakPOI
Posts: 41
Last visit: Wed Jan 31, 2024 2:57 am

Re: Run exe with parameter -NOGUI?

Post by CermakPOI »

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/
This topic is 7 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked