Calling .exe

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 7 months 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
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Calling .exe

Post by obrienc »

Is it possible to call this from a form? Its located in c:\windows\system32

Code: Select all

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

Re: Calling .exe

Post by jvierra »

Just run it.

$result = qwinsta /server:$ServerName
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Calling .exe

Post by obrienc »

Code: Select all

$queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+", ",")) } | ConvertFrom-Csv)
ERROR: qwinsta : The term 'qwinsta' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
ERROR: path was included, verify that the path is correct and try again.
MainForm.psf (2, 19): ERROR: At Line: 2 char: 19
ERROR: + $queryResults = (qwinsta /server:$ServerName | foreach { (($_ ...
ERROR: + ~~~~~~~
ERROR: + CategoryInfo : ObjectNotFound: (qwinsta:String) [], CommandNotFoundException
ERROR: + FullyQualifiedErrorId : CommandNotFoundException
ERROR:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling .exe

Post by jvierra »

If it is not installed then you cannot run it.

Go to a PowerShell prompt and type qwinsta. It must be installed on the local system.
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Calling .exe

Post by obrienc »

It's there. It returns the desired result in the console.

I wrapped this

Code: Select all

$queryResults = (".\qwinsta.exe /server:$ServerName" | foreach { (($_.trim() -replace "\s+", ",")) } | ConvertFrom-Csv)
in quotes and it doesn't give an error but no output.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling .exe

Post by jvierra »

This returns qwinsta as an object collection:

Code: Select all

qwinsta |
	Select-Object -Skip 1 |
	ForEach-Object{ 
		[pscustomobject]@{
			Session = $_.SubString(1, 10)
			UserName = $_.SubString(19, 20)
			ID = $_.SubString(41, 5)
			State = $_.SubString(48, 8)
		}
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling .exe

Post by jvierra »

Her eis the best we can do to solve your issues. It works with no issues but your version of QWINSTA may have slightly different field widths.
Attachments
Test-QWINSTA.psf
(24.21 KiB) Downloaded 164 times
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Calling .exe

Post by obrienc »

Code: Select all

$form1_Load = {
	#TODO: Initialize Form Controls here
}
$buttonFindFreeJumpBox_Click = {
	$Servers = "jump01", "jump02", "jump03"
	foreach ($s in $Servers)
	{
		qwinsta /server:$s |
		Select-Object -Skip 1 |
		ForEach-Object{
			[pscustomobject]@{
				Session  = $_.SubString(1, 10)
				UserName = $_.SubString(19, 20)
				ID	     = $_.SubString(41, 5)
				State    = $_.SubString(48, 8)
			}
		}
	}
}

ERROR: qwinsta : The term 'qwinsta' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
ERROR: path was included, verify that the path is correct and try again.
MainForm.psf (8, 3): ERROR: At Line: 8 char: 3
ERROR: + qwinsta /server:$s |
ERROR: + ~~~~~~~
ERROR: + CategoryInfo : ObjectNotFound: (qwinsta:String) [], CommandNotFoundException
ERROR: + FullyQualifiedErrorId : CommandNotFoundException
ERROR:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling .exe

Post by jvierra »

You do not have qwinsta in your current path.

Did you run my example without changes? Start with that.

qwinsta must be in the Windows system folder. To check do this:

Get-Command qwinsta | select source
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Calling .exe

Post by obrienc »

It's in the system32 folder. And this is in my
Env: %SystemRoot%\system32
This topic is 6 years and 7 months 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