Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Thursday, May 19, 2016

PowerShell Script : Get the SharePoint Content Databases which are not mapped to Nintex Database

Here is the PowerShell script to get SharePoint Content Databases which are not mapped to Nintex Database in a SharePoint 2010 farm.


#Load all the assemblies that we need to use          
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint.Administration') | Out-Null          
[System.Reflection.Assembly]::LoadWithPartialName('Nintex.Workflow') | Out-Null          
[System.Reflection.Assembly]::LoadWithPartialName('Nintex.Workflow.Administration') | Out-Null          
[System.Reflection.Assembly]::LoadWithPartialName('Nintex.Workflow.Common') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Nintex.Workflow.ContentDbMappingCollection') | Out-Null          
           
#Add the SharePoint PowerShell snapin (in case it's not already loaded)          
if(-not(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
 Add-PSSnapin Microsoft.SharePoint.PowerShell;          
}          
       
$DBs=Get-SPContentDatabase

foreach ($DB in $DBs)
{

    [Nintex.Workflow.ContentDbMapping]$contentDbMapping;          

    $contentDbMapping = [Nintex.Workflow.ContentDbMappingCollection]::ContentDbMappings.GetContentDbMappingForSPContentDb($DB.Id);

    if($contentDbMapping -eq $null)          
    {          
        Add-Content e:\sam\NMR.txt $DB.Name
    }    
}


Let me know if you have any question.

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:

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

Tuesday, February 18, 2014

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.