首页 > 解决方案 > 远程启动服务

问题描述

我正在尝试在用户输入后远程启动服务。

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"
    }
}

它只是返回

无法启动“服务”

几乎像

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

不起作用。

任何想法为什么它不起作用?


编辑:

来自块的实际错误消息catch

缺少参数“过滤器”的参数。指定“System.String”类型的参数,然后重试。

标签: powershell

解决方案


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

搬家-Filter解决了这个问题。


推荐阅读