安装/卸载Windows服务

安装/卸载Windows服务,windows,visual-studio-2008,powershell,service,Windows,Visual Studio 2008,Powershell,Service,我已经使用VSTS 2008 Windows服务类型项目创建了一个Windows服务项目,现在我想编写脚本,使用PowerShell安装/卸载它 有参考样品或文件吗?您没有提到您使用的语言。很有可能,可以处理它。如果我正确理解您的问题,您首先需要从VSTS中创建安装程序。我已经有一段时间没有做过了,但基本上看起来是这样的: 创建安装程序后,可以使用PowerShell将其自动化 如果您确实希望PowerShell成为您的服务安装程序,可能有一种方法可以通过使用自动化PowerShell中的wi

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


有参考样品或文件吗?

您没有提到您使用的语言。很有可能,可以处理它。

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

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


如果您确实希望PowerShell成为您的服务安装程序,可能有一种方法可以通过使用自动化PowerShell中的windows服务安装程序。

以下是我编写的安装脚本的净化版本。应展示您需要做的一切:

## 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)"
我用的是C#。还有什么想法吗?