-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallWinService.ps1
More file actions
129 lines (100 loc) · 4.06 KB
/
InstallWinService.ps1
File metadata and controls
129 lines (100 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
[CmdLetBinding(DefaultParameterSetName="None")]
param
(
[string]$serverName,
[Parameter(Mandatory = $true)]
[string]$serviceName,
[Parameter(Mandatory = $true)]
[string]$servicePath,
[int]$serviceType = 0x10,
[string]$startMode = "Automatic",
[string]$displayName = $serviceName,
[string]$description = "",
[Parameter(Mandatory = $true, ParameterSetName="Cred")]
[string]$userName = "",
[Parameter(Mandatory = $true, ParameterSetName="Cred")]
[string]$password = "",
[int]$errorControl = 1,
[string]$loadOrderGroup = "",
[string[]]$loadOrderGroupDependcies = "",
[string[]]$dependencies = "",
[switch]$interactDesktop
)
# determine server name is local or remote machine
$server = (".", $serverName)[!$serverName -eq $null]
$service = Get-WmiObject -Namespace "root\cimv2" -Class "Win32_Service" `
-ComputerName $server -EnableAllPrivileges -Filter "Name='$serviceName'" -Impersonation 3
if($service)
{
Write-Warning ("Service {0} exists{1}. Nothing to install" -f $serviceName, (""," on $serverName")[!$serverName -eq $null])
exit 0;
}
$connection = new-object System.Management.ConnectionOptions
$connection.EnablePrivileges = $true
$connection.Impersonation = "Impersonate"
$scope = new-object System.Management.ManagementScope("\\$server\root\cimv2", $connection)
$scope.Connect()
$managment = new-object System.Management.ManagementClass(
$scope,
(new-object System.Management.ManagementPath("Win32_Service")),
(new-object System.Management.ObjectGetOptions))
$params = $managment.GetMethodParameters("Create")
$params["Name"] = $serviceName
$params["DisplayName"] = $displayName
$params["PathName"] = $servicePath
$params["ServiceType"] = $serviceType
$params["ErrorControl"] = $errorControl
$params["StartMode"] = $startMode
$params["DesktopInteract"] = ($true, $false)[!$interactDesktop]
$params["StartName"] = ($null, $userName)[!$userName -eq ""]
$params["StartPassword"] = $password
$params["LoadOrderGroup"] = ($null, $loadOrderGroup)[!$loadOrderGroup -eq ""]
$params["LoadOrderGroupDependencies"] = ($null, $loadOrderGroupDependcies)[!$loadOrderGroup -eq ""]
$params["ServiceDependencies"] = ($null, $dependencies)[!$dependencies -eq ""]
Write-Output ("Input parameters:{0}{1}" -f [System.Environment]::NewLine, ($params | Format-List * -Force | Out-String))
Write-Output "Creating service..."
$result = $managment.InvokeMethod("Create", $params, $null)
$strResult = $result | Format-List * -Force | Out-String
if($result.ReturnValue -ne 0)
{
Write-Error ("Service didn't install. Return value is {0}.{1}{2}" -f $result.ReturnValue, [System.Environment]::NewLine, $strResult)
exit 1;
}
Write-Output ("Service was created:{0}" -f $strResult)
$repeatCount = 5
do {
Start-Sleep -Seconds 2
Write-Output "Checking service state..."
$service = Get-WmiObject -Namespace "root\cimv2" -Class "Win32_Service" `
-ComputerName $server -EnableAllPrivileges -Filter "Name='$serviceName'" -Impersonation 3
} while(!$service -and --$repeatCount -gt 0)
if(!$service)
{
Write-Error "Service wasn't installed at there moment."
exit 1;
}
Write-Output ($service | Format-List * -Force | Out-String)
if (!($service.Started))
{
Write-Output "Starting service $serviceName on server $serverName..."
$result = $service.StartService()
$strResult = $result | Format-List * -Force | Out-String
if($result.ReturnValue -ne 0)
{
Write-Error ("Service didn't start. Return values is {0}.{1}{2}" -f $result.ReturnValue, [System.Environment]::NewLine, $strResult)
exit 1;
}
Write-Output ("Service was started: {0}" -f $strResult)
}
$repeatCount = 5
do {
Start-Sleep -Seconds 2
Write-Output "Checking service state..."
$service = Get-WmiObject -Namespace "root\cimv2" -Class "Win32_Service" `
-ComputerName $server -EnableAllPrivileges -Filter "Name='$serviceName'" -Impersonation 3
} while(!$service.Started -and --$repeatCount -gt 0)
if(!$service.Started)
{
Write-Error "Service wasn't started at there moment."
exit 1;
}