2009-06-26 66 views
6

Tôi đã tạo một dự án Dịch vụ Windows bằng cách sử dụng dự án kiểu Dịch vụ Windows VSTS 2008 và bây giờ tôi muốn viết các kịch bản để cài đặt/gỡ cài đặt nó bằng PowerShell.cài đặt/gỡ cài đặt Dịch vụ Windows

Bất kỳ mẫu hoặc tài liệu tham chiếu nào?

Trả lời

4

Bạn không đề cập đến ngôn ngữ bạn đang sử dụng. Nhiều khả năng, các windows install utility có thể xử lý nó.

+0

Tôi đang sử dụng C#. Còn ý tưởng nào nữa không? – George2

+0

InstallUtil yourservice.exe – Glenn

2

Nếu tôi hiểu chính xác câu hỏi của bạn, trước tiên bạn cần phải tạo trình cài đặt từ bên trong VSTS. Nó được một lúc kể từ khi tôi đã làm một, nhưng về cơ bản nó trông như thế này:

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

Một khi bạn đã tạo ra một trình cài đặt, bạn có thể tự động hoá nó với PowerShell.

Nếu bạn thực sự muốn PowerShell làm trình cài đặt dịch vụ, có thể có cách để tự động hoá trình cài đặt dịch vụ Windows từ PowerShell bằng cách sử dụng ServiceInstaller Class.

18

Đây là phiên bản đã được vệ sinh của tập lệnh cài đặt mà tôi đã viết. Phải chứng minh mọi thứ bạn cần làm:

## 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)" 
Các vấn đề liên quan