2009-06-26 219 views
6

我一直在使用VSTS 2008年的Windows服务型项目创建一个Windows服务项目,现在我想编写脚本安装/使用PowerShell的卸载它。安装/卸载Windows服务

任何参考样本或文件?

回答

4

你没有提到你使用的是什么语言。更可能的是,windows install utility可以处理它。

+0

我正在使用C#。还有什么想法? – George2 2009-06-26 07:59:09

2

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

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

一旦你创建了一个安装程序,您可以使用PowerShell自动化。

如果你确实想PowerShell来为您服务的安装程序,有可能是通过使用ServiceInstaller Class自动从PowerShell中的Windows服务的安装方式。

18

这里有一个安装脚本,我写的消毒版本。应该证明你需要做的一切:

## 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)"