nothing within a script block executes

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 forum is a space to discuss coding involving Graphical User Interfaces (GUI) in PowerShell or using WinForms controls, and technical issues related to development.

- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
This topic is 1 year and 6 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
mqh77777
Posts: 254
Last visit: Fri Oct 10, 2025 11:01 am
Answers: 0
Has voted: 1 time

nothing within a script block executes

Post by mqh77777 »

Product: PowerShell Studio 2023 (64 Bit)
Build: v5.8.232
OS: Windows 11.0.22631
PS Version(s): 5.1.22621.1, 7.4.2


I have this simple code. the $statusbar text only works outside of the Invoke-Command -Scriptblock. Why? I need to have a few commands run within the Invoke-Command that run on the remote machine, where $PC=$PCNameBox.Text can be any system in your environment that you have access too. But so far nothing within the Invoke-Command runs.

Code: Select all

$buttonExecuteAllCommands_Click = {
	
	$PC = $PCNameBox.Text
	$richtextbox.Clear()
	$statusbar1.text = 'Running commands, please wait.'
	Start-Sleep -Seconds 3
	Invoke-command -ComputerName $PC -ScriptBlock {
		$statusbar1.text = 'Running commands, please wait.'
		Start-Sleep -Seconds 3
		$statusbar1.text = 'All done!'
	}

	$statusbar1.text = 'All done!'
}
User avatar
apowershelluser
Posts: 212
Last visit: Wed Oct 29, 2025 8:18 pm
Answers: 2
Been upvoted: 1 time

Re: nothing within a script block executes

Post by apowershelluser »

I’m guessing it’s similar to a job where you shouldn’t pass form elements in the script block since it’s running in a different thread
User avatar
mqh77777
Posts: 254
Last visit: Fri Oct 10, 2025 11:01 am
Answers: 0
Has voted: 1 time

Re: nothing within a script block executes

Post by mqh77777 »

So far not even ChatGPT can write code that works the way I need. I have the computer name field ($PC) which can be any computer in your Org. I need to run cmd.exe on the remote $PC. so it has to run CMD /c " then an .EXE that is on the remote machine" . <<<< yes this .exe is on my remote machines.

Start-Process only works on my local machine. And so far all of the code snippets ChatGPT has given me fail. i.e. running Invoke-Command and passing it the $PC and the commands to run on the remote machine.
Any help is greatly appreciated.
User avatar
apowershelluser
Posts: 212
Last visit: Wed Oct 29, 2025 8:18 pm
Answers: 2
Been upvoted: 1 time

Re: nothing within a script block executes

Post by apowershelluser »

Maybe a working directory for the content?

It's been sometime since I've had to do it because we have other options in my env

$ComputerName = "remotePC" # Specify the remote computer's name
$ScriptBlock = {
Start-Process "C:\temp\yourapp.exe" -Wait -NoNewWindow -WorkingDirectory "C:\temp"
}

Invoke-Command -ComputerName $ComputerName -ScriptBlock $ScriptBlock
This topic is 1 year and 6 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