powershell - Use a variable as the verb when calling a cmdlet -


i trying write script start, stop or restart service on remote computer.

i found this:

start-service -inputobject $(get-service -computer computer1 -name spooler) 

and started building script around it.

this have:

$action = read-host "start, stop or restart service?" $serv = read-host "service name?" $comp = read-host "computer name?" "$action"-service -inputobject $(get-service -computer "$comp" -name "$serv") 

when run script error:

at u:\serv.ps1:4 char:10 + "$action"-service -inputobject $(get-service -computer "$comp" -name "$serv") +          ~~~~~~~~ unexpected token '-service' in expression or statement. @ u:\serv.ps1:4 char:19 + "$action"-service -inputobject $(get-service -computer "$comp" -name "$serv") +                   ~~~~~~~~~~~~ 

i have tried instead:

$action = read-host "start, stop or restart service?" $action = $action + "-service" $serv = read-host "service name?" $comp = read-host "computer name?" "$action" -inputobject $(get-service -computer "$comp" -name "$serv") 

but similar error:

at u:\serv.ps1:5 char:11 + "$action" -inputobject $(get-service -computer "$comp" -name "$serv") +           ~~~~~~~~~~~~ unexpected token '-inputobject' in expression or statement. @ u:\serv.ps1:5 char:24 + "$action" -inputobject $(get-service -computer "$comp" -name "$serv") +                        ~~ unexpected token '$(' in expression or statement.     + categoryinfo          : parsererror: (:) [], parseexception     + fullyqualifiederrorid : unexpectedtoken      unexpected token '-inputobject' in expression or statement.         + categoryinfo          : parsererror: (:) [], parseexception         + fullyqualifiederrorid : unexpectedtoken 

how can pass input - "start", "stop" or "restart" verb part of command?

that's rather convoluted way of managing services powershell. consider doing instead:

$action = read-host "start, stop or restart service?" $serv   = read-host "service name?" $comp   = read-host "computer name?"  $svc = get-service -computer $comp -name $serv switch ($action) {   'start'   { $svc | start-service }   'stop'    { $svc | stop-service }   'restart' { $svc | restart-service } } 

if reason must construct cmdlet name have use call operator (&):

& $action ... 

without operator powershell won't recognize string cmdlet, (try to) print via out-default.

also, consider using promptforchoice() method of host ui instead of read-host:

$message  = 'manage services' $question = 'start, stop or restart service?'  $choices = new-object collections.objectmodel.collection[management.automation.host.choicedescription] $choices.add((new-object management.automation.host.choicedescription -argumentlist '&start')) $choices.add((new-object management.automation.host.choicedescription -argumentlist 'sto&p')) $choices.add((new-object management.automation.host.choicedescription -argumentlist '&restart'))  $action = $host.ui.promptforchoice($message, $question, $choices, 0)  switch ($action) {   0 { $svc | start-service }   1 { $svc | stop-service }   2 { $svc | restart-service } } 

Comments