2009-06-26 218 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)"