Avatar of DevSupport
DevSupport
 asked on

powershell variable inside quotes

Hi Experts,

I am trying to get the value from a dropdown into the Invoke Command to Start a windows service in a remote system.

When I do write-host $dispname I get the value as "Some Service" but when I try to run the Invoke-Command I get the error below:


function Start-TC{
$dispname = $objAppDropDown.SelectedItem
write-host $dispname
Invoke-Command -Computername $objappsvrname.Text -ScriptBlock { Start-Service -DisplayName "$dispname"  }

}

Error:
With "$dispname" in quotes:

Cannot bind argument to parameter 'DisplayName' because it is an empty string.

Without quotes:
Cannot bind argument to parameter 'DisplayName' because it is null.

Full script attached
scr-1.txt
Powershell

Avatar of undefined
Last Comment
DevSupport

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
DevSupport

ASKER
If we have more than one arguments for Scriptblock, should we add them to argument list separated by spaces?

Also can we send a script file to be executed in a remote system through scriptblock?

Please let me know.

Thank You!
DevSupport
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DevSupport

ASKER
Thank You so much!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck