0

I am trying to start a service remotely after user input.

function Start_Service {
    $ComputerName = $txb_hostname.Text

    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

    $title = 'Start Service'
    $msg   = 'Enter Service Name (e.g. AppVClient):'

    $ServiceName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

    try {
        (Get-WmiObject Win32_Service -ComputerName $ComputerName -filter "Name='$ServiceName'").StartService()
        LogWrite "$ServiceName started."
    } catch {
        LogWrite "Unable to stop $ServiceName"
    }
}

It just returns

unable to start "service"

almost like the

(Get-WmiObject Win32_Service -Filter -ComputerName $ComputerName "Name='$ServiceName'").StartService()

doesn't work.

Any ideas why it isn't working?


Edit:

Actual error message from the catch block:

Missing an argument for parameter 'Filter'. Specify a parameter of type 'System.String' and try again.

Adam
  • 19
  • 1
  • 8
  • 1
    It would be nice if you'd give us some clues: error messages etc. – montonero Dec 07 '18 at 12:43
  • Presumably because your WMI request fails. Output the actual error in the `catch` block. You may want to unroll nested exceptions too, since they usually provide more details about what went wrong. [See here](https://stackoverflow.com/a/38421525/1630171) for more details. – Ansgar Wiechers Dec 07 '18 at 12:43
  • what is the actual error message. It would make sense to evaluate $_.Exception.Message in the catch block. – Stefan Prugg Dec 07 '18 at 12:47
  • i not 100% sure how i would do this, i have never had to capture an error message before. could you assist me with where and what i would put – Adam Dec 07 '18 at 12:53
  • If you just put "$_" in the catch instead of the "LogWrite" – Bernard Moeskops Dec 07 '18 at 12:54
  • @Adam Please follow the link in my previous comment. – Ansgar Wiechers Dec 07 '18 at 12:57
  • 2
    You are giving the `-Filter` query as the `-ComputerName`. Do like this: `(Get-WmiObject Win32_Service -Filter "Name='$ServiceName'" -ComputerName $ComputerName).StartService()` – Theo Dec 07 '18 at 13:22
  • moce -filder along and now i get You cannot call a method on a null-valued expression. – Adam Dec 07 '18 at 13:24
  • Please [edit] your question and update it with your modified code and the complete error message. Also, does the remote host actually have a service of that name? – Ansgar Wiechers Dec 07 '18 at 13:26
  • i am trying to start ccmexec.exe on my own machine and tested on a colleagues error still happens ..... updated code – Adam Dec 07 '18 at 13:38
  • `ccmexec.exe` is the name of an executable, not the name of a service. If you're using that as a filter it's quite obvious that you won't be getting a result. Use `Get-Service` (or `Get-WmiObject Win32_Service | Select-Object Name`) to get the list of service names. – Ansgar Wiechers Dec 07 '18 at 13:46
  • BTW: are you aware you can use `(Get-Service -ComputerName $ComputerName -Name $ServiceName).Start()`? – JohnLBevan Dec 07 '18 at 14:03

1 Answers1

0
(Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "Name='$ServiceName'").StartService()

Moving the -Filter has resolved the issue.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Adam
  • 19
  • 1
  • 8