Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Windows Service in Powershell for "Network Service" account?

I want to create a Windows Service using Powershell. Creating it for a given user is piece of cake. I used this function adapted from here.

function ReinstallService1 ($serviceName, $binaryPath, $login, $pass) {       Write-Host "installing service"     # creating credentials which can be used to run my windows service     $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force     $mycreds = New-Object System.Management.Automation.PSCredential ($login, $secpasswd)      # creating widnows service using all provided parameters     New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds     Write-Host "installation completed" } 

My question is: How can I create for "Network Service" account?

If I modify New-Service line and remove credential parameter, the service is created for "Local System" account. Nearly miss.

New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic  

I googled a lot and saw no way of indicating service account. If I try to use Credential parameter for user "NETWORK SSERVICE", I don't know what password to put and if I invent one (just in case cmdlet ignores it) it doesn't work. The error is:

New-Service : Service 'XXXX (XXXX)' cannot be created due to the following error: The account name is invalid or does not exist, or the password is invalid for the account name specified

like image 232
Oscar Foley Avatar asked Feb 05 '13 13:02

Oscar Foley


People also ask

How do you create a service in PowerShell?

Install and uninstall itself (using Windows PowerShell service management functions). Start and stop itself (using the same set of functions). Contain a short C# snippet, which creates the PSService.exe that the SCM expects (using the Add-Type command). Make the PSService.exe stub call back into the PSService.

How do I start a Windows PowerShell service?

To start or stop a service through PowerShell, you can use the Start-Service or the Stop Service cmdlet, followed by the name of the service that you want to start or stop. For instance, you might enter Stop-Service DHCP or Start-Service DHCP.

How do I enable a service in PowerShell?

Type the following command to enable a service and press Enter: Set-Service -Name "SERVICE-NAME" -Status running -StartupType automatic For example, this command enables the printer spooler service using PowerShell: Set-Service -Name "spooler" -Status running -StartupType automatic Quick note: You may able to use the ...


2 Answers

The correct name of the account is NT AUTHORITY\NETWORK SERVICE.

like image 163
Ansgar Wiechers Avatar answered Sep 24 '22 06:09

Ansgar Wiechers


This is the final version of Reinstall Service for everyone's benefit, specially for Aniket.

function ReinstallService ($serviceName, $binaryPath, $description, $login, $password, $startUpType) {         Write-Host "Trying to create service: $serviceName"          #Check Parameters         if ((Test-Path $binaryPath)-eq $false)         {             Write-Host "BinaryPath to service not found: $binaryPath"             Write-Host "Service was NOT installed."             return         }          if (("Automatic", "Manual", "Disabled") -notcontains $startUpType)         {             Write-Host "Value for startUpType parameter should be (Automatic or Manual or Disabled) and it was $startUpType"             Write-Host "Service was NOT installed."             return         }          # Verify if the service already exists, and if yes remove it first         if (Get-Service $serviceName -ErrorAction SilentlyContinue)         {             # using WMI to remove Windows service because PowerShell does not have CmdLet for this             $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"              $serviceToRemove.delete()             Write-Host "Service removed: $serviceName"         }          # if password is empty, create a dummy one to allow have credentias for system accounts:          #NT AUTHORITY\LOCAL SERVICE         #NT AUTHORITY\NETWORK SERVICE         if ($password -eq "")          {             #$secpassword = (new-object System.Security.SecureString)             # Bug detected by @GaTechThomas             $secpasswd = (new-object System.Security.SecureString)         }         else         {             $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force         }         $mycreds = New-Object System.Management.Automation.PSCredential ($login, $secpasswd)          # Creating Windows Service using all provided parameters         Write-Host "Installing service: $serviceName"         New-Service -name $serviceName -binaryPathName $binaryPath -Description $description -displayName $serviceName -startupType $startUpType -credential $mycreds          Write-Host "Installation completed: $serviceName"          # Trying to start new service         Write-Host "Trying to start new service: $serviceName"         $serviceToStart = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"         $serviceToStart.startservice()         Write-Host "Service started: $serviceName"          #SmokeTest         Write-Host "Waiting 5 seconds to give time service to start..."         Start-Sleep -s 5         $SmokeTestService = Get-Service -Name $serviceName         if ($SmokeTestService.Status -ne "Running")         {             Write-Host "Smoke test: FAILED. (SERVICE FAILED TO START)"             Throw "Smoke test: FAILED. (SERVICE FAILED TO START)"         }         else         {             Write-Host "Smoke test: OK."         }  } 
like image 39
Oscar Foley Avatar answered Sep 22 '22 06:09

Oscar Foley