Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

Saturday, July 19, 2014

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 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

Thursday, March 6, 2014

Finding SiteTemplate type in SharePoint 2010 & 2013

Many a times, we may not have access to the SharePoint environment. But we may want to know the SharePoint Site Template type without logging into the Servers and without using Site Settings/Administration.

Here are the steps:

1) View the Page source(In IE, Right Click on the page and click View Source. In Chrome, Click Ctrl + I).
Search for the Keyword g_wsaSiteTemplateId


If you see something like var g_wsaSiteTemplateId = 'STS#0';, then the site was created using Team Site. You can map the value of g_wsaSiteTemplateId  to a specific Site Template.
For example 'STS#0' is the code for Team Site


2) If you want to know all templates open PowerShell and run 
Get-SPWebTemplate | format-table -auto | Out-File -FilePath  "C:\temp\Output.txt" 
You will get a list of all templates
If you don't have a local SharePoint environment, you can find them from Bernd Kemmler's Blog

3) Some of the most commonly used  are
  • STS#0        Team Site
  • STS#1        Blank Site
  • WIKI#0      Wiki Site
  • BLOG#0     Blog Site
  • CMSPUBLISHING#0  Publishing Site               
  • BLANKINTERNET#0  Publishing Site                                                     

Thats it.

Good Luck.