簡體   English   中英

安裝/卸載Windows服務

[英]install/uninstall a Windows Service

我使用VSTS 2008 Windows服務類型項目創建了一個Windows服務項目,現在我想編寫腳本來使用PowerShell安裝/卸載它。

任何參考樣品或文件?

這是我寫的安裝腳本的清理版本。 應該展示你需要做的一切:

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

你沒有提到你正在使用的語言。 Windows安裝實用程序很可能可以處理它。

如果我正確理解您的問題,您首先需要在VSTS中創建安裝程序。 我已經做了一段時間了,但它基本上是這樣的:

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

創建安裝程序后,您可以使用PowerShell自動執行安裝程序。

如果您確實希望PowerShell成為您的服務安裝程序,則可以使用ServiceInstaller類從PowerShell自動執行Windows服務安裝程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM