Tuesday, February 25, 2014

Start all the stopped websites in SharePoint 2010 farm (Using PowerShell)

# Start all the stopped websites in SharePoint 2010 farm
function StartStoppedWebSitesInFarm
{
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $websites = Get-WmiObject -namespace "root/webadministration" -class Site -ComputerName $server.name -Authentication 6 | Where {$_.GetState().ReturnValue -ne 1} | select name
     
        if ($websites -eq $null) {
            Write-Host "No web sites are stopped on server $($server.Name)" -foregroundcolor green
            continue
        }
        else
        {
            foreach ($webObj in $websites)
             {
                $web = $webObj.name
             
                if($web -eq 'Default Web Site')
                {
                   Write-Host "Default Web Site is stopped in $($server.Name), so skipping this server" -foregroundcolor yellow    
                   break  
                }
                else
                {        
                    Get-WmiObject -Namespace 'root\webadministration' -Class Site -ComputerName $server.name -Authentication 6 -Filter "Name='$web'" | Invoke-WmiMethod -Name Start
                    Write-Host "web site $web is started on server $($server.Name)" -foregroundcolor green
                }
             }
        }
     
    }
}
example:

Get all the web sites which are stopped in the SharePoint 2010 Farm (Using PowerShell)

# Get all the websites stopped in the SharePoint 2010 farm
function GetStoppedWebSitesInFarm
{
   
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $websites = Get-WmiObject -namespace "root/webadministration" -class Site -ComputerName $server.name -Authentication 6 | Where {$_.GetState().ReturnValue -ne 1} | select name
        if ($websites -eq $null) {
            Write-Host "No web sites are stopped on server $($server.Name)" -foregroundcolor green
            continue
        }
        else
        { 
            foreach ($web in $websites)
             {
                Write-Host "web site '$($web)' is stopped on server $($server.Name)" -foregroundcolor red
             }
        }
      
    }  
}
Example:

Start all the application pools which are stopped in the SharePoint 2010 Farm (Using PowerShell)

# Start the application pools stopped in the SharePoint 2010 farm
function StartStoppedAppPools
{
   
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $apppool = Get-WmiObject -Namespace root\MicrosoftIISv2 -Class IIsApplicationPoolSetting -ComputerName $server.name -Authentication 6 | Where { $_.AppPoolState -eq '4'} | select name
        if ($apppool -eq $null) {
            Write-Host "No app pools are stopped on server $($server.Name)" -foregroundcolor green
            continue
        }
        else
        { 
         foreach ($appObj in $apppool)
             {
        $app = $appObj | Out-String
           $apppoolname = $app.Substring($app.LastIndexOf('/') + 1,($app.Length-$app.LastIndexOf('/'))-1).Trim()
             
              if($apppoolname -eq 'SharePoint Web Services Root')
              {
                 Write-Host "App Pool $apppoolname does not require to start in $($server.Name)" -foregroundcolor yellow           
              }
              else{
            Write-Host "Starting the app pool '$($apppoolname)'"
            Get-WmiObject -Namespace 'root\webadministration' -Class ApplicationPool -ComputerName $server.name -Authentication 6 -Filter "Name='$apppoolname'" | Invoke-WmiMethod -Name Start
             
                  Write-Host "App Pool $apppoolname is started on server $($server.Name)" -foregroundcolor green
                 }
             }
        }
    }  
}

Example

Get all the Application Pools which are stopped in the SharePoint 2010 Farm (Using PowerShell)

# Get all the application pools stopped in the SharePoint 2010 farm
function GetAppPoolStatusFarm
{
   
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $apppool = Get-WmiObject -Namespace root\MicrosoftIISv2 -Class IIsApplicationPoolSetting -ComputerName $server.name -Authentication 6 | Where { $_.AppPoolState -eq '4'} | select name
       
        if ($apppool -eq $null) {
            Write-Host "No app pools are stopped on server $($server.Name)" -foregroundcolor green
            continue
        }
        else
        { 
            foreach ($appObj in $apppool)
             {
                  $app = $appObj | Out-String
               $apppoolname = $app.Substring($app.LastIndexOf('/') + 1,($app.Length-$app.LastIndexOf('/'))-1).Trim()
                 
                  if($apppoolname -eq 'SharePoint Web Services Root')
                  {
                      Write-Host "App Pool '$($apppoolname)' is stopped on server $($server.Name)" -foregroundcolor yellow
                  }
                  else
                  {
                    Write-Host "App Pool '$($apppoolname)' is stopped on server $($server.Name)" -foregroundcolor red
                  }
             }
        }
      
    }  
}

Example

Tuesday, February 18, 2014

Restart SharePoint Services on SharePoint 2010 farm

Step 1. Create a folder with 2 powershell blank files as below image.







Step 2. Open Sam.Functions.PS1 file and copy the below code snippet.
 # Stop specified service on all servers within the farm
function Stop-ServiceOnFarm
{
    param ($ServiceName)
   
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"
       
        if ($service -eq $null -or $service.State -eq "Stopped") {
            continue
        }
       
        Write-Host "Stopping '$($ServiceName)' on $($server.Name)..."
        $service.InvokeMethod('StopService',$Null)
       
        do {
            Start-Sleep -s 5
            $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"   
        } while ($service.State -ne "Stopped")
    }  
}
# Starts specified service on all servers within the farm
function Start-ServiceOnFarm
{
    param ($ServiceName)
   
     foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
     {
        $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"
       
        if ($service -eq $null -or $service.State -eq "Started" -or $service.State -eq "Starting") {
            continue
        }
       
        Write-Host "Starting '$($ServiceName)' on $($server.Name)..."
        $service.InvokeMethod('StartService',$Null)
     }
}
# Restarts specified service on all servers within the farm
function Restart-ServiceOnFarm
{
    param ($ServiceName)
   
    Stop-ServiceOnFarm $ServiceName
    Start-ServiceOnFarm $ServiceName
}
# Restarts SharePoint Services on all servers within the farm
function Restart-TimerandAdminService
{
    Restart-ServiceOnFarm "SPTimerV4"
    Restart-ServiceOnFarm "SPAdminV4"
}

Step 3. Open the file Sam.RestartAdmin-TimerServicesOnFarm and copy the below snippet. 
if ($SCRIPT:scriptDir -eq $null -or $SCRIPT:scriptDir -eq "")
{
 $SCRIPT:scriptDir = (Get-Location -PSProvider FileSystem).ProviderPath
}
# ** Load My functions
Write-Host "* Loading Functions..."
. (Join-Path $scriptDir "Sam.Functions.ps1")
Restart-TimerandAdminService

Step 4. Close and Save the files and execute the .\Sam.RestartAdmin-TimerServicesOnFarm.ps1 file from any server in the farm.

Restart IIS in all servers in a SharePoint 2010 farm


Step 1. Create a folder with 2 powershell blank files as below image.








 
Step 2. Open Sam.Functions.PS1 file and copy the below code snippet.
# Stop specified service on all servers within the farm
function Stop-ServiceOnFarm
{
    param ($ServiceName)
     
    foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
    {
        $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"
         
        if ($service -eq $null -or $service.State -eq "Stopped") {
            continue
        }
         
        Write-Host "Stopping '$($ServiceName)' on $($server.Name)..."
        $service.InvokeMethod('StopService',$Null)
         
        do {
            Start-Sleep -s 5
            $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"   
        } while ($service.State -ne "Stopped")
    }  
}

# Starts specified service on all servers within the farm
function Start-ServiceOnFarm
{
    param ($ServiceName)
     
     foreach ($server in (Get-SPServer | Where {$_.Role -ne "Invalid"}) )
     {
        $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$($ServiceName)'"
         
        if ($service -eq $null -or $service.State -eq "Started" -or $service.State -eq "Starting") {
            continue
        }
         
        Write-Host "Starting '$($ServiceName)' on $($server.Name)..."
        $service.InvokeMethod('StartService',$Null)
     }
}

# Restarts IIS on all servers within the farm
function Restart-IIS
{
    Stop-ServiceOnFarm "W3SVC"
    Stop-ServiceOnFarm "SMTPSVC"
    Stop-ServiceOnFarm "IISADMIN"
     
   Start-ServiceOnFarm "IISADMIN"
   Start-ServiceOnFarm "SMTPSVC"
   Start-ServiceOnFarm "W3SVC"
}
 

Step 3. Open the file Sam.RestartIISOnFarm and copy the below snippet.
 
  if ($SCRIPT:scriptDir -eq $null -or $SCRIPT:scriptDir -eq "")
{
 $SCRIPT:scriptDir = (Get-Location -PSProvider FileSystem).ProviderPath
}

# ** Load My functions
Write-Host "* Loading Functions..."
. (Join-Path $scriptDir "Sam.Functions.ps1")

Restart-IIS

Step 4. Close and Save the files and call the .\Sam.RestartIISOnFarm.ps1 file from any server in the farm.

Monday, February 17, 2014

Merge SharePoint Log files in a farm

As we know that SharePoint logs are generated in all servers in a farm. So if you would like to find the log for your application issue, you may need to look into the log in all WFE/Index servers. In that case you do not need to open logfile from each server rather you can use Merge-SPLogFile powershell command in one server to get the ULS logs for the entire farm and investigate it.
Example:


Once the logfile got generated and you can open in UlsViewer tool to epen the logfile.
One intersting thing is that you can also filter logfiles by specifying ‘second’ duration in the command. This is really required as thousands of logs got created in a minute.



To know more in details please refer the below link