安装/卸载 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1047336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:42:45  来源:igfitidea点击:

install/uninstall a Windows Service

windowsvisual-studio-2008powershellservice

提问by George2

I have created a Windows Service project using VSTS 2008 Windows Service type project and now I want to write scripts to install/uninstall it using PowerShell.

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

Any reference samples or documents?

任何参考样本或文件?

回答by Richard Berg

Here's a sanitized version of an install script I wrote. Should demonstrate everything you need to do:

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

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

回答by Glenn

You didn't mention what language you are using. More than likely, the windows install utilitycan handle it.

你没有提到你使用的是什么语言。很有可能,Windows 安装实用程序可以处理它。

回答by Robert Harvey

If I understand your question correctly, you first need to create an installer from within VSTS. It's been awhile since I've done one, but it basically looks like this:

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

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

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

Once you have created an installer, you can automate it with PowerShell.

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

If you really do want PowerShell to be your service installer, there might be a way to automate the windows service installer from PowerShell by using the ServiceInstaller Class.

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