powershell script error

Posted on 2016-11-15
Last Modified: 2016-11-17
I was wondering if you have seen this error before? Perhaps you can suggest a better script that sets up specific logon account to run a specific service on multiple computers.

Thank you so much!
I downloaded this script from Powershell gallery and it fails with the error “Stop-Service : Cannot find any service with display name 'System.ServiceProcess.ServiceController'.

# Script location
$ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition

# New Service Account
$ServiceAccount = "testtarget\Username"  
$ServicePassword = "<Password>"

# Array of Services which should be changed, Use the real "Display Name" of each Service
$Srvs = ('Servicene Name');

#Prompt for an administartiv user name and password
$Cred = Get-Credential
#Read the servers listed in the server.txt file located in the script folder
$servers = Get-Content "c:\scripts\servers.txt"
foreach ($server in $servers)
    # Stop, Configure and Restart of the listed Services above
         foreach ($Srv in $Srvs)
        $gsrv = Get-Service -DisplayName $Srv
          Write-Host "Service $Srv on $server" -foreground "Green";
        Stop-Service -DisplayName $gsrv -Force
        Set-Service -InputObject $gsrv -StartupType Automatic
        $service = gwmi win32_service -ComputerName $server -filter "displayname='$Srv'" -Credential $cred
        $service.Change($null, $null, $null, $null, $null, $null, $ServiceAccount, $ServicePassword).ReturnValue
        # Status
        if ($service.Change().ReturnValue -eq "0")
            Write-Host "Logon successfully Changed" -ForegroundColor "Green"
           Write-Host "Have a look for Status" $service.Change().ReturnValue "at" -ForegroundColor "Red"
        Start-Service -DisplayName $Srv

Stop-Service : Cannot find any service with display name 'System.ServiceProcess.ServiceController'.
At line:8 char:9
+         Stop-Service -DisplayName $gsrv -Force
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenDisplayName,Microsoft.PowerShell.Commands.StopServiceCommand

gwmi : User credentials cannot be used for local connections
At line:10 char:20
+ ...  $service = gwmi win32_service -ComputerName $server -filter "display ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At line:11 char:9
+         $service.Change($null, $null, $null, $null, $null, $null, $Se ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:13 char:13
+         if ($service.Change().ReturnValue -eq "0")
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Start-Service : Service 'Q Processor (QProcessor)' cannot be started due to the following error: Cannot start service
QProcessor on computer '.'.
At line:21 char:9
+         Start-Service -DisplayName $Srv
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
Question by:creative555
  • 5
  • 3
  • 2
LVL 69

Expert Comment

ID: 41888778
Stop-Service -DisplayName $gsrv -Force

Open in new window

needs to be
Stop-Service -InputObject $gsrv -Force

Open in new window

You should change the Start-Service the same way.
LVL 69

Expert Comment

ID: 41888789
Regarding the other errors, you can not run this script with your local machine as server. WMI does not allow to provide user credentials for the local machine (the reasons are complex). You've used '.', which is the local machine.
LVL 84

Expert Comment

ID: 41888835
There are several blatant errors in that script; it has obviously never been tested in the version posted.
* -DisplayName instead of -Inputobject in Stop-Service
* -ComputerName argument missing for Get-Service, so the script will always start and stop the service locally
* The return value of the Change() WMI method isn't tested correctly
* Start-Service is running against a local service as well

That said, check out my former solution at
It sets the "Logon as a service" right as well, if you want it to (and put ntrights.exe into the script's folder).
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Author Comment

ID: 41890634
thank you so much! I used this script from here. One request.

I have a different service name on other servers. But all of them have one name in common.

How would you modify the script so, for example, it would update all the services with the name "*NAME*""

I tried this and it didn't work. I also tried to put two service names with commas....
$ServiceName = ("*SpecificName*");

Author Comment

ID: 41890654
Also for the second server I got this error
ComputerName : Server02
UserRight    : System.Management.Automation.RuntimeException:  Granting SeServiceLogonRight to testtarget\adms.mig.svc on
               \\SErver02OpenPolicy:   ***Error*** OpenPolicy -1073610729
GetService   :
LVL 84

Accepted Solution

oBdA earned 500 total points
ID: 41890951
This now accepts the service name as command line argument, so you can run it in a loop. The error with the path to ntrights.exe is fixed as well.
So create a csv file for the logon information:
"ServiceName", "ServiceAccount", "ServicePassword"
"Service1", "Domain\Account1", "TopSecret"
"Service2", "Domain\Account2", "DontTell"

Open in new window

Then you can loop it for example like this:
$ComputerList = Get-Content C:\Temp\Servers.txt
Import-Csv C:\Temp\Services.csv | % {C:\Temp\Set-ServiceLogon.ps1 -ServiceName $_.ServiceName -ServiceAccount $_.ServiceAccount -ServicePassword $_.ServicePassword -ComputerList $ComputerList}

Open in new window

	[string[]]$ComputerList = @($ENV:ComputerName),
	$TimeoutStop = 120,		# Seconds
	$TimeoutStart = 120		# Seconds
$SetUserRight = $True
$ntrights = "$(Split-Path -Path $MyInvocation.MyCommand.Path -Parent)\ntrights.exe"

$WmiReturnValueMap = @{	## 'Change method of the Win32_Service class',
	[uint32]0 =		'The request was accepted.'
	[uint32]1 =		'The request is not supported.'
	[uint32]2 =		'The user did not have the necessary access.'
	[uint32]3 =		'The service cannot be stopped because other services that are running are dependent on it.'
	[uint32]4 =		'The requested control code is not valid, or it is unacceptable to the service.'
	[uint32]5 =		'The requested control code cannot be sent to the service because the state of the service (Win32_BaseService.State property) is equal to 0, 1, or 2.'
	[uint32]6 =		'The service has not been started.'
	[uint32]7 =		'The service did not respond to the start request in a timely fashion.'
	[uint32]8 =		'Unknown failure when starting the service.'
	[uint32]9 =		'The directory path to the service executable file was not found.'
	[uint32]10 =	'The service is already running.'
	[uint32]11 =	'The database to add a new service is locked.'
	[uint32]12 =	'A dependency this service relies on has been removed from the system.'
	[uint32]13 =	'The service failed to find the service needed from a dependent service.'
	[uint32]14 =	'The service has been disabled from the system.'
	[uint32]15 =	'The service does not have the correct authentication to run on the system.'
	[uint32]16 =	'This service is being removed from the system.'
	[uint32]17 =	'The service has no execution thread.'
	[uint32]18 =	'The service has circular dependencies when it starts.'
	[uint32]19 =	'A service is running under the same name.'
	[uint32]20 =	'The service name has invalid characters.'
	[uint32]21 =	'Invalid parameters have been passed to the service.'
	[uint32]22 =	'The account under which this service runs is either invalid or lacks the permissions to run the service.'
	[uint32]23 =	'The service exists in the database of services available from the system.'
	[uint32]24 =	'The service is currently paused in the system.'
$ProgressCount = 0
ForEach ($ComputerName In $ComputerList) {
	Write-Progress -Activity "[$($ProgressCount)/$($ComputerList.Count)] Changing service account for '$($ServiceName)' to $($ServiceAccount)'" -Status $ComputerName -PercentComplete (100 * $ProgressCount / $ComputerList.Count)
	$ProgressCount += 1
	$Return = "" | Select-Object -Property 'ComputerName', 'ServiceName', 'UserRight', 'GetService', 'Change', 'Stop', 'Start'
	$Return.ComputerName = $ComputerName
	$Return.ServiceName = $ServiceName
	Try {
		$Step = 'UserRight'
		If ($SetUserRight) {
			$Output = & $ntrights -m "\\$($ComputerName)" -u $ServiceAccount.Trim('.\') +r SeServiceLogonRight 2>&1
			If ($LastExitCode -eq 0) {
				$Return.$Step = 'OK'
			} Else {
				Throw ($Output -join ' ')
		} Else {
			$Return.$Step = 'SKIPPED'
		$Step = 'GetService'
		If (-not ($ServiceWmi = Get-WmiObject -Class Win32_Service -Filter "Name='$($ServiceName)'" -ComputerName $ComputerName -ErrorAction Stop)) {
			Throw "Get-WmiObject -Class Win32_Service : Cannot find any service with service name '$($ServiceName)'."
		$ServiceNet = Get-Service -Name $ServiceName -ComputerName $ComputerName -ErrorAction Stop
		$Return.$Step = 'OK'
		$Step = 'Change'
		$ChangeResult = $ServiceWmi.Change($null, $null, $null, $null, $null, $null, $ServiceAccount, $ServicePassword)
		If ($ChangeResult.ReturnValue -eq 0) {
			$Return.$Step = 'OK'
			$Step = 'Stop'
			If ($ServiceNet.Status -eq 'Running') {
			$ServiceNet.WaitForStatus('Stopped', [Timespan]::FromSeconds($TimeoutStop))
			$Return.$Step = 'OK'
			$Step = 'Start'
			$ServiceNet.WaitForStatus('Running', [Timespan]::FromSeconds($TimeoutStart))
			$Return.$Step = 'OK'
		} Else {
			$Return.$Step = "$($ChangeResult.ReturnValue): $($WmiReturnValueMap[$ChangeResult.ReturnValue])"
	} Catch {
		$Return.$Step = $_.Exception
	} Finally {
Write-Progress -Activity "Done" -Completed 

Open in new window

The error on the second server is probably a permissions issue; make sure you run it from an elevated prompt and with administrative permissions on the machines. I ran it successfully remotely against W2k12R2.

Author Comment

ID: 41891663
Do I need to change this line: string[]$ComputerList = @($ENV:ComputerName),

      [string[]$ComputerList = @($ENV:ComputerName),

I tried replacing it with this, but it didn't work
      [string[]$ComputerList = @($ComputerList),

Author Comment

ID: 41891672
LVL 84

Assisted Solution

oBdA earned 500 total points
ID: 41891681
Save the script as I posted it, don't change anything (note that the problem with the ntrights.exe path from the other question has been fixed in the version I posted above, no need to hardcode the script path there anymore!).
It's meant as an all-purpose script, not for a hardcoded set of servers and services.
Then use it in another script just like any cmdlet (only with the path to the script you saved).
So put the two lines you inserted into a separate script
$ComputerList = ...
Import-Csv ...

Open in new window


Author Comment

ID: 41891701
Thank you so much! It is working now! Perfect

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question