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.
Locked
User avatar
pls-sapien
Posts: 28
Joined: Tue Apr 05, 2016 9:03 pm

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

Post by pls-sapien » Tue Sep 26, 2017 1:37 am

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

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Sep 26, 2017 2:00 am

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: 28
Joined: Tue Apr 05, 2016 9:03 pm

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

Post by pls-sapien » Tue Sep 26, 2017 3:44 am

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

thanks,
Sean

Locked