Monday, July 9, 2012

Start a specific service on all SharePoint Servers in a FARM (WFE and APP Servers) using PowerShell


//Services Like SPTimerV4, SPAdminV4, iisadmin, W3SVC, WAS
        $serviceName = "Name of the service to be started"
        // Loop thru all the servers in a SharePoint Farm
        foreach ($server in (get-spserver | Where {$_.Role -ne "Invalid"}) )
        {
            Write-Host Restarting $serviceName in $server.Name
            $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$serviceName'"
            $service.InvokeMethod('StopService',$Null)
            Start-Sleep -s 5
            $service.InvokeMethod('StartService',$Null)
            Start-Sleep -s 5
            $service.State           
        }



Upgrading SharePoint Feature using PowerShell


$site = get-spsite "http://mysite"
$features = $site.QueryFeatures("web", $true);

foreach($feature in $features)
{
        if($feature.Definition.DisplayName -eq "FeatureName")
        {       
            $feature.Upgrade($true);
        }
}