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
Solved

Script - Restart Services and Delete SPS Cache to the Farm

Posted on 2013-11-11
4
491 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

792 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