Solved

Script - Restart Services and Delete SPS Cache to the Farm

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now