Solved

PowerShell script to change Username and password for log on as service (using domain account) on remote server

Posted on 2014-01-28
4
4,451 Views
Last Modified: 2015-04-19
I am fairly new to Powershell and need to write a script to change the username and password on remote servers that have services that are set to use "log on as" using a domain account.  Is there a way to check this and then set the username and password
0
Comment
Question by:CAD_SB
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 39816790
I wrote a fairly short script to reset and update service account passwords where service account matched the one provided.

The idea behind this script requires:
1) The service account you are looking for
2) The password you plan to change to in the AD
3) The list of servers you want to scan.

$acctname = "domain\user"
$svcpwd  = "password"
#Text file
$servers = (gc serverlist.txt) 
#from AD 
#Import-Module ActiveDirectory
#$servers = (Get-ADComputer -Filter *)

foreach ($server in $servers) 
{
#Query service
gwmi win32_service -computername $server | % { 
if ($_.StartName -eq $acctname)
{
$servicename = $_.Name
write-host "updating $servicename"
$svcnm = gwmi win32_service -computername $server -filter "name='$servicename'"
#Update Service
$svcnm.change($null,$null,$null,$null,$null,$null,"$UserName",$svcpwd,$null,$null,$null) 
#Stop Service
$svcnm.stopservice
#Start Service
$svcnm.startservice
}
}
}

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 39853966
I had previously inserted the wrong code snippet, find the correct one above.
0
 

Author Comment

by:CAD_SB
ID: 39855031
perfect - thankyou
0
 

Expert Comment

by:BSAIT
ID: 40732690
Is it possible to scan a remote server and select "all services" utilising the specific AD service account and change the password.
I would like to firstly reset the AD password account then select a server on the domain and reset the password for all services that use the specified service account?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now