-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate-NewService.ps1
82 lines (70 loc) · 3.09 KB
/
Create-NewService.ps1
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
# -----------------------------------------------------------------------------
# Script: Create-NewService.ps1
# Author: Rob Waight
# Created: 10/06/2018
# Version: 0.1
# Creates a new service based on user input
#
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service
# If you are using this script, why not just use the New-Service PowerShell command?
# -----------------------------------------------------------------------------
[CmdLetBinding()]
param(
[switch]$svcName,
[switch]$svcDisp,
[switch]$svcDesc,
[switch]$svcStartType,
[switch]$binPath
)
Start-Transcript $ENV:USERPROFILE\Desktop\Create-NewService_Transcript_$(Get-Date -format 'yyyyMMdd-HHmmss').txt; Clear
$WorkDir = (Get-Item -Path ".\" -Verbose).FullName
Write-Host "I'm writing the transcript to your desktop. Your working directory is $WorkDir" -ForegroundColor Yellow
# Service Name
If ( ! $svcName ){$svc_Name = Read-Host -Prompt 'Enter the service name'}
Else{
Write-Host "Why aren't you just using the New-Service PowerShell command?" -ForegroundColor Yellow
$svc_Name = $svcName
}
# Service Display Name
If ( ! $svcDisp ){$svc_DisplayName = Read-Host -Prompt 'Enter the service display name'}
Else{
Write-Host "Why aren't you just using the New-Service PowerShell command?" -ForegroundColor Yellow
$svc_DisplayName = $svcDisp
}
# Service Description
If ( ! $svcDesc ){$svc_Description = Read-Host -Prompt 'Enter the service description'}
Else{
Write-Host "Why aren't you just using the New-Service PowerShell command?" -ForegroundColor Yellow
$svc_Description = $svcDesc
}
# Service Startup Type
Do {
If ( ! $svcStartType ){$svc_StartType = Read-Host -Prompt 'Enter the service startup type, options are: Manual, Automatic, Disabled'}
Else{
Write-Host "Why aren't you just using the New-Service PowerShell command?" -ForegroundColor Yellow
$svc_StartType = $svcStartType
}
}
Until(($svc_StartType -eq "Manual") -or ($svc_StartType -eq "Automatic") -or ($svc_StartType -eq "Disabled"))
# Service Binary Path
If ( ! $binPath ){$svc_binPath = Read-Host -Prompt 'Enter the full binary path'}
Else{
Write-Host "Why aren't you just using the New-Service PowerShell command?" -ForegroundColor Yellow
$svc_binPath = $binPath
}
Clear; Write-Host "Your working directory is $WorkDir" -ForegroundColor Yellow
# Exit if the service name already exists
if (Get-Service $svc_Name -ErrorAction SilentlyContinue) {
Write-Host "The service $svc_Name already exists, re-run the script and try again!" -ForegroundColor Red
Sleep 3; Pause; Exit
}
Try {
# Attempt to creat the service
New-Service -Name $svc_Name -BinaryPathName $svc_binPath `
-DisplayName $svc_DisplayName -StartupType $svc_StartType -Description $svc_Description
}
Catch { Write-Host "An error occured." -ForegroundColor Red -NoNewline; Sleep 3; Pause }
Write-Host "Getting information about the new service:"; Get-Service -Name $svc_Name
Write-Host "I've written the transcript to your desktop." -ForegroundColor Yellow
Write-Host "The script will exit after you.. " -NoNewLine; Pause
Stop-Transcript; Exit