Powershell To update Services Password, Sharepoint Managed Acount Password and IIS AppPool Password
Many a times there is a need to update the changed password in Virtual Machines. It can be done manually. But doing it via Powershell will save us a lot of time.
Background
After browsing for a while, I couldn't find an article that pointed me in doing all the above using one script. So I thought to share the powershell comands that I just put together, which can be optimized/refactored by admins to use them in prod environment.
Using the code
//
// Save the below powershell code as ps1 and run in Powershell
// Update Domain, UserName and Password with your actual values
//
$localaccount = "domain\username"
$newpassword = "************"
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -force
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue
Write-Host "Updating Password for Services" -foregroundcolor green
$services = Get-WmiObject win32_Service | where { $_.startname -eq $localaccount}
foreach ($service in $services)
{
Write-Host "Updating Password." -foregroundcolor 13
$status = $service.Change($null,$null,$null,$null,$null,$null, $localaccount, $newpassword)
Write-Host "Status: " $status.ReturnValue -foregroundcolor 13
}
Write-Host "Stop all running Services." -foregroundcolor green
Get-WmiObject win32_Service | where { ($_.startname -eq $localaccount -And $_.state -ne "Stopped" -And $_.startmode -ne "Disabled") } | stop-service -force
Write-Host "Restarting stopped Services" -foregroundcolor green
Get-WmiObject win32_Service | where { ($_.startname -eq $localaccount -And $_.state -ne "Running" -And $_.startmode -ne "Disabled") } | start-service
$ManagedAccount = Get-SPManagedAccount -Identity $localaccount
$securePassword = ConvertTo-SecureString -String $newpassword -AsPlainText -Force
Write-Host "Please wait while Passwod for SharePoint Managed Account gets updated." -foregroundcolor red
Set-SPManagedAccount -Identity $ManagedAccount -ExistingPassword $securePassword -Confirm:$false
Write-Host "Password for SahrePoint Updated Successfully" -foregroundcolor green
Import-Module WebAdministration
Write-Host "Restarting IIS. " -foregroundcolor green
iisreset /noforce
$appPools = Get-ChildItem IIS:\appPools | where {$_.state -ne "Started"}
foreach ($appPool in $appPools)
{
if ($appPool.processModel.userName -eq $localaccount)
{
$appPoolName = $appPool.Name
$appPool.processModel.password = $newpassword
$appPool | Set-Item
$appPool.Recycle()
Write-Host "Password for AppPool " $appPoolName " updated successfully" -foregroundcolor green
}
}
Write-Host "Restarting IIS after password reset. " -foregroundcolor green
iisreset /noforce
Write-Host "IIS Restarted Successfully. " -foregroundcolor green
Rocky Fernandes has over 11 years of experience in Software Industry mostly developing Web Applications. Other than having vast experience in various Microsoft Technologies like SharePoint, ASP.Net, C#, VB.Net, Silverlight, WCF, MVC, Web API, SQL Server, ADO.Net, ADOMD.Net, LINQ, Entity Framework, Exchange Web Service(EWS) API, he also has vast experience in Client Side Scripting like JavaScript, jQuery, JSON, JSONP, CSS and HTML5. He is very much passionate in programming and enjoys doing it. In the academic side he holds a BE Degree in Computer Science & Engineering. In the certification side he holds certifications in Sharepoint 2010, SharePoint 2013, ASP.Net & SQL Server.
In most of the free time, he will be spending in technical activities like researching solutions, reading technical articles and learning new stuff. He believes that following Best Practices and Coding standard is a way to go for producing an efficient, quality & satisfactory application.
Homepage: http://www.smartsolutionshub.com/