Link to home
Start Free TrialLog 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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You so much!