简体   繁体   中英

install/uninstall a Windows Service

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.

Any reference samples or documents?

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

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

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:

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

Once you have created an installer, you can automate it with 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM