Link to home
Create AccountLog in
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of DevSupport
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
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thank You so much!