Created
July 7, 2015 23:03
-
-
Save philoushka/c5e0325cc6f14352938f to your computer and use it in GitHub Desktop.
Install a Windows Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$serviceName = "My Service Name" | |
$exePath = "C:\Program Files\foo\bar.exe" | |
$username = "someUser" | |
$password = convertto-securestring -String "somePassword" -AsPlainText -Force | |
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password | |
$existingService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" | |
if ($existingService) | |
{ | |
"'$serviceName' exists already. Stopping." | |
Stop-Service $serviceName | |
"Waiting 3 seconds to allow existing service to stop." | |
Start-Sleep -s 3 | |
$existingService.Delete() | |
"Waiting 5 seconds to allow service to be uninstalled." | |
Start-Sleep -s 5 | |
} | |
"Installing the service." | |
New-Service -BinaryPathName $exePath -Name $serviceName -Credential $cred -DisplayName $serviceName -StartupType Automatic | |
"Installed the service." | |
$ShouldStartService = Read-Host "Would you like the '$serviceName ' service started? Y or N" | |
if($ShouldStartService -eq "Y") | |
{ | |
"Starting the service." | |
Start-Service $serviceName | |
} | |
"Completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment