?
Solved

Script - Restart Services and Delete SPS Cache to the Farm

Posted on 2013-11-11
4
Medium Priority
?
511 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 2000 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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

800 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