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 Execution Policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -force
#Import SharePoint Module
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue
#Update Service Account
Write-Host "Updating Password for Services" -foregroundcolor green
$services = Get-WmiObject win32_Service | where { $_.startname -eq $localaccount}
foreach ($service in $services)
{
#Update Password
Write-Host "Updating Password." -foregroundcolor 13
$status = $service.Change($null,$null,$null,$null,$null,$null, $localaccount, $newpassword)
Write-Host "Status: " $status.ReturnValue -foregroundcolor 13
}
#Stop Service
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
#Start Service
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
#Update Sharepoint Managed Account
$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
#Update App Pool Accounts
Import-Module WebAdministration
#Restart IIS
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
}
}
#Restart IIS
Write-Host "Restarting IIS after password reset. " -foregroundcolor green
iisreset /noforce
Write-Host "IIS Restarted Successfully. " -foregroundcolor green
No comments:
Post a Comment