Difference between revisions of "Windows Services"

From MgmtWiki
Jump to: navigation, search
(Solutions)
(Solutions)
 
Line 6: Line 6:
 
* The Windows User need permission to run a service with that user account.  
 
* The Windows User need permission to run a service with that user account.  
 
# First enable [[Windows Group Policy]] editor.
 
# First enable [[Windows Group Policy]] editor.
 +
===PowerShell script===
 +
a function for PowerShell that changes the username, password, and restarts a service on a remote computer (you can use localhost if you want to change the local server). I've used this for monthly service account password resets on hundreds of servers.
 +
 +
You can find a copy of the original at http://www.send4help.net/change-remote-windows-service-credentials-password-powershel-495
 +
 +
It also waits until the service is fully stopped to try to start it again, unlike one of the other answers.
 +
<pre>
 +
Function Set-ServiceAcctCreds([string]$strCompName,[string]$strServiceName,[string]$newAcct,[string]$newPass){
 +
  $filter = 'Name=' + "'" + $strServiceName + "'" + ''
 +
  $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
 +
  $service.Change($null,$null,$null,$null,$null,$null,$newAcct,$newPass)
 +
  $service.StopService()
 +
  while ($service.Started){
 +
    sleep 2
 +
    $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
 +
  }
 +
  $service.StartService()
 +
}
 +
</pre>
  
 
==References==
 
==References==
  
 
[[Category:Best Practice]]
 
[[Category:Best Practice]]

Latest revision as of 15:46, 9 August 2020

Full Title or Meme

Best practices on using Windows Services.

Solutions

  1. First enable Windows Group Policy editor.

PowerShell script

a function for PowerShell that changes the username, password, and restarts a service on a remote computer (you can use localhost if you want to change the local server). I've used this for monthly service account password resets on hundreds of servers.

You can find a copy of the original at http://www.send4help.net/change-remote-windows-service-credentials-password-powershel-495

It also waits until the service is fully stopped to try to start it again, unlike one of the other answers.

Function Set-ServiceAcctCreds([string]$strCompName,[string]$strServiceName,[string]$newAcct,[string]$newPass){
  $filter = 'Name=' + "'" + $strServiceName + "'" + ''
  $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
  $service.Change($null,$null,$null,$null,$null,$null,$newAcct,$newPass)
  $service.StopService()
  while ($service.Started){
    sleep 2
    $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
  }
  $service.StartService()
}

References