【问题标题】:install/uninstall a Windows Service安装/卸载 Windows 服务
【发布时间】:2009-06-26 04:27:39
【问题描述】:

我已经使用 VSTS 2008 Windows 服务类型项目创建了一个 Windows 服务项目,现在我想编写脚本来使用 PowerShell 安装/卸载它。

任何参考样本或文件?

【问题讨论】:

    标签: windows visual-studio-2008 powershell service


    【解决方案1】:

    这是我编写的安装脚本的净化版本。应该展示你需要做的一切:

    ## delete existing service
    # have to use WMI for much of this, native cmdlets are incomplete
    $service = Get-WmiObject -Class Win32_Service -Filter "Name = 'My Service'"
    if ($service -ne $null) 
    { 
        $service | stop-service
        $service.Delete() | out-null 
    }
    
    ## run installutil
    # 'frameworkdir' env var apparently isn't present on Win2003...
    $installUtil = join-path $env:SystemRoot Microsoft.NET\Framework\v2.0.50727\installutil.exe
    $serviceExe = join-path $messageServerPath MyService.exe
    $installUtilLog = join-path $messageServerPath InstallUtil.log
    & $installUtil $serviceExe /logfile="$installUtilLog" | write-verbose
    
    $service = Get-WmiObject -Class Win32_Service -Filter "Name = 'My Service'"
    
    # change credentials if necessary
    if ($user -ne "" -and $password -ne "")
        { $service.change($null, $null, $null, $null, $null, $null, $user, $password, $null, $null, $null) | out-null }
    
    # activate
    $service | set-service -startuptype Automatic -passthru | start-service
    write-verbose "Successfully started service $($service.name)"
    

    【讨论】:

      【解决方案2】:

      您没有提及您使用的语言。 windows install utility 很可能可以处理它。

      【讨论】:

      • 我正在使用 C#。还有什么想法吗?
      【解决方案3】:

      如果我正确理解您的问题,您首先需要从 VSTS 中创建安装程序。我已经有一段时间没有做过了,但它基本上是这样的:

      http://csharpcomputing.com/Tutorials/Lesson22.htm

      创建安装程序后,您可以使用 PowerShell 将其自动化。

      如果您确实希望 PowerShell 成为您的服务安装程序,则可能有一种方法可以使用 ServiceInstaller Class 从 PowerShell 自动化 Windows 服务安装程序。

      【讨论】:

        猜你喜欢
        • 2011-12-10
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        相关资源
        最近更新 更多