Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Script - Restart Services and Delete SPS Cache to the Farm

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
Gonzalo Becerra
Asked:
Gonzalo Becerra
  • 2
  • 2
2 Solutions
 
skipper68Commented:
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
 
Gonzalo BecerraAuthor Commented:
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
 
skipper68Commented:
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
 
Gonzalo BecerraAuthor Commented:
Excellent!!! thanks a lot for your help!!! :)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now