invoke-command - script block not getting the variable value?

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 6 years and 5 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
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

invoke-command - script block not getting the variable value?

Post by pls-sapien »

hi all,
im having some issues with a form that tries to invoke-command.
this is the line:

Code: Select all

function Check-Service1-Remote
{
	$result1 = Invoke-Command -ComputerName $script:LocalServer -ScriptBlock { Get-Service $Service1_Name } -Credential $script:Cred -Authentication Kerberos
	
	if ($result1.Status -eq 'Running')
	{
	do stuff
	}
}
the problem is that the $result1 is getting all the services and not just the one i pass in the variable Service1_Name.
i tired configuring the $Service1_Name as $script:Service1_Name but it didnt work as well...

what am i doing wrong?
thanks
Sean
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: invoke-command - script block not getting the variable value?

Post by jvierra »

You have to use the correct command arguments. See help for exact details.

help Invoke-Command -Full.

Always start with help until you get better at scripting.
User avatar
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: invoke-command - script block not getting the variable value?

Post by pls-sapien »

hi jvierra,
thank you for the hint!
i was missing the "using: " i did read the help just forgot this part :)

thanks,
Sean
This topic is 6 years and 5 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