84 lines
2.8 KiB
PowerShell
84 lines
2.8 KiB
PowerShell
# Register SV SXP Service Watcher
|
|
# Run this script as Administrator
|
|
|
|
# Check if running as Administrator
|
|
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
|
|
if (-not $isAdmin) {
|
|
Write-Error "This script must be run as Administrator!"
|
|
Write-Host "Please right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# Service configuration
|
|
$serviceName = "SV SXP Service watcher"
|
|
$binaryPath = "C:\Users\dn\Desktop\SXP-Service-watcher\sxp-service-watcher.exe"
|
|
$displayName = "SV SXP Service watcher"
|
|
$description = "SV SXP Service watcher"
|
|
$startupType = "Automatic"
|
|
|
|
# Check if service already exists
|
|
$existingService = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
|
|
|
|
if ($existingService) {
|
|
Write-Host "Service '$serviceName' already exists." -ForegroundColor Yellow
|
|
$response = Read-Host "Do you want to remove and recreate it? (y/n)"
|
|
|
|
if ($response -eq 'y') {
|
|
Write-Host "Stopping service..." -ForegroundColor Cyan
|
|
Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 1
|
|
|
|
Write-Host "Removing service..." -ForegroundColor Cyan
|
|
# Use sc.exe for compatibility with Windows PowerShell 5.1
|
|
$result = sc.exe delete $serviceName
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Error "Failed to remove service: $result"
|
|
exit 1
|
|
}
|
|
|
|
Start-Sleep -Seconds 2
|
|
Write-Host "Service removed successfully." -ForegroundColor Green
|
|
} else {
|
|
Write-Host "Registration cancelled." -ForegroundColor Yellow
|
|
exit 0
|
|
}
|
|
}
|
|
|
|
# Check if binary exists
|
|
if (-not (Test-Path $binaryPath)) {
|
|
Write-Error "Binary not found at: $binaryPath"
|
|
exit 1
|
|
}
|
|
|
|
# Register the service
|
|
try {
|
|
Write-Host "Registering service '$serviceName'..." -ForegroundColor Cyan
|
|
|
|
New-Service -Name $serviceName `
|
|
-BinaryPathName $binaryPath `
|
|
-DisplayName $displayName `
|
|
-Description $description `
|
|
-StartupType $startupType `
|
|
-ErrorAction Stop
|
|
|
|
Write-Host "Service registered successfully!" -ForegroundColor Green
|
|
|
|
# Ask if user wants to start the service
|
|
$startService = Read-Host "Do you want to start the service now? (y/n)"
|
|
|
|
if ($startService -eq 'y') {
|
|
Write-Host "Starting service..." -ForegroundColor Cyan
|
|
Start-Service -Name $serviceName
|
|
|
|
$status = Get-Service -Name $serviceName
|
|
Write-Host "Service status: $($status.Status)" -ForegroundColor Green
|
|
}
|
|
|
|
} catch {
|
|
Write-Error "Failed to register service: $_"
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "`nService registration complete!" -ForegroundColor Green |