Solved

Script - Restart Services and Delete SPS Cache to the Farm

Posted on 2013-11-11
4
505 Views
Last Modified: 2013-11-11
Dear All,

Currently I'm looking for a script that Restart the IISAdmin Service and the Timer Job Service for all the Servers in the SharePoint 2010 Farm.

I was working on the following script, but probably I'm wrong with different thinks because is not working.

Could you help me to fix the script?

I would like the following steps for each server:
1- Stop IISAdmin Service
2- Stop the SPTimerV4 Service
3- Find for cache.ini into the following directory "C:\ProgramData\Microsoft\SharePoint\Config" and delete all the XML Files where the cache.ini is located and setup the Cache.ini to value 1.


[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"}            
Import-Module BitsTransfer
$farm = Get-SPFarm
$Services = 'DNSCache','CertPropSvc'

function DeleteCache {
$a = get-wmiobject -computername $server -query "SELECT * FROM win32_directory WHERE name ='C:\\ProgramData\\Microsoft\\SharePoint\\Config'"
$folders = Get-ChildItem $a.Name

    foreach ($xmlfile in $folders)
        {
            if ($xmlfile -like "*.xml")
                {
				   $xmlfile.Delete()
                }
       
        }

$folders = Get-ChildItem C:\Temp\Script
foreach ($folder in $folders)
    {
    $items = Get-ChildItem $folder.FullName -Recurse
    foreach ($item in $items)
        {
            if ($item.Name.ToLower() -eq "cache.ini")
                {
                    $cachefolder = $folder.FullName
                }
               
        }
    }

       
$a = Get-Content  $cachefolder
$a  = 1
Set-Content $a -Path $cachefolder
}

function StopService {
	foreach ($Service in $Services) {
	
    $WMI_Service= Get-WmiObject Win32_Service -ComputerName $server |Where-Object {$_.name -eq $Service}
	
    if ($WMI_Service -ne $null)                         
    {                             
        
		Write-Host -ForegroundColor Red "Stopping Service:"$WMI_Service.Name " Server:$server"
		$WMI_Service.InvokeMethod('StopService',$null) | Out-Null
        Start-Sleep -Seconds 5
		$WMI_Service= Get-WmiObject Win32_Service -ComputerName $server |Where-Object {$_.name -eq $Service}
		Write-Host -ForegroundColor Red "Service Stooped Succesfull:"$WMI_Service.Name " Server:$server" " Status: "$WMI_Service.State
		Write-Host ""
		
    }   
		
    else            
    {            
        write-host -ForegroundColor Yellow "Could not find Sharepoint 2010 "$WMI_Service" on "$server            
    }
	}	
}

function StartService {

		foreach ($Service in $Services) {
	
    $WMI_Service= Get-WmiObject Win32_Service -ComputerName $server |Where-Object {$_.name -eq $Service}
	
    if ($WMI_Service -ne $null)                         
    {                             
		Write-Host -ForegroundColor Green "Starting Service:"$WMI_Service.Name " Server:$server"
        $WMI_Service.InvokeMethod('StartService',$null) | Out-Null                            
        sleep -Seconds 5
		$WMI_Service= Get-WmiObject Win32_Service -ComputerName $server |Where-Object {$_.name -eq $Service}
        Write-Host -ForegroundColor Green "Service Started Succesfull: " $WMI_Service.Name " Server:$server" " Status:"$WMI_Service.State
		Write-Host ""
    }   
		
    else            
    {            
        write-host -ForegroundColor Yellow "Could not find Sharepoint 2010 "$WMI_Service" on "$server            
    }
	}
	
}

foreach ($server in $servers)            
{   
    Write-Host "Stopping Services"
	StopService
	Write-Host ""
	Write-Host -BackgroundColor Yellow -ForegroundColor Red "Deleting SharePoint Cache"
	DeleteCache
	Start-Sleep 5
	Write-Host ""
	Write-Host "Starting Services"
	StartService
}

Open in new window

0
Comment
Question by:Gonzalo Becerra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
skipper68 earned 500 total points
ID: 39638711
There are 2 caches you can clear in SharePoint, the configuration cache, and the BLOB cache.  
The Configuration Cache script I use is here:
ClearConfigCache.txt

It's really thorough and commented so you can follow it.  

If you just want to flush the BLOB (configuration) cache it'd be easier to do it this way:
$webApp = Get-SPWebApplication "<WebApplicationURL>"
[Microsoft.SharePoint.Publishing.PublishingCache]::FlushBlobCache($webApp)
Write-Host "Flushed the BLOB cache for:" $webApp
0
 
LVL 1

Author Comment

by:Gonzalo Becerra
ID: 39638742
skipper68 thanks a lot, your script is great to delete the SharePoint Cache, could you help me to add an IISReset on Each Server in the farm before delete the cache? Thanks in advance,
0
 
LVL 9

Assisted Solution

by:skipper68
skipper68 earned 500 total points
ID: 39638800
Definitely.  Here's the PowerShell script for that:
cls
 Write-Host "Loading SharePoint Commandlets"
 Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
 Write-Host -ForegroundColor Green " Commandlets Loaded ... Loading Variables"
 Write-Host
 [array]$servers= Get-SPServer | ? {$_.Role -eq "Application"}
 $farm = Get-SPFarm
 foreach ($server in $servers)
 {
      Write-Host -ForegroundColor Blue "Attempting to reset IIS for $server"
         iisreset $server /noforce "\\"$_.Address
         iisreset $server /status "\\"$_.Address
         Write-Host
         Write-Host -ForegroundColor Green "IIS has been reset for $server"
         Write-Host
 }
 Write-Host -ForegroundColor Green "IIS has been reset across the SharePoint Farm"
 Start-Sleep -Seconds 5
 Write-host

Open in new window

0
 
LVL 1

Author Closing Comment

by:Gonzalo Becerra
ID: 39639744
Excellent!!! thanks a lot for your help!!! :)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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