Wednesday, 6 February 2019

Deploy WSP Files Globally



Add-PSSnapin Microsoft.SharePoint.PowerShell


Get-ChildItem "E:\testfolder\" -Filter *.wsp |
Foreach-Object {
    $name =  $_.Name
     Add-SPSolution -LiteralPath "E:\testfolder(Create folder and keep all wsp files in this folder)\$($name)"
     #Install-SPSolution -Identity $name -GACDeployment -Force -CompatibilityLevel {14,15} -FullTrustBinDeployment
     Install-SPSolution -Identity $name -WebApplication "https://webapplication" -GACDeployment -Force -CompatibilityLevel {14,15} -FullTrustBinDeployment
    Write-Host $name
   
}
 stsadm -o execadmsvcjobs
#Install-SPSolution -Identity ABC.wsp -GACDeployment -CompatibilityLevel {14,15} -FullTrustBinDeployment

Get Incomming e-mail settings in a library



Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Web Application URL 
#$WebAppURL="http://webapp1/"
$Report="D:\temp\IncomingEmails.csv"

 $AllWebs =
'https://webapp2',
#'https://webapp3/',
#'https://webapp4/',


#foreach($WebAppURL in $AllWebs)
#{


# Enumerate all lists in the web application
$AllWebs|  Get-SPWebApplication  | Get-SPSite -limit ALL | Get-SPWeb -Limit ALL | ForEach-Object {
#Get the Current Site
$Site = $_
#Get all Lists with Incoming Email enabled
 $Site.Lists | Where-Object { ($_.CanReceiveEmail) -and ($_.EmailAlias) }| ForEach-Object {
      New-Object -TypeName PSObject -Property @{
              ListName = $_.Title
              SiteUrl = $Site.Url
              Email = $_.EmailAlias
               lastModified=$_.LastItemModifiedDate
               itemCount = $_.ItemCount  }
   }
} | Export-CSV $Report -NoTypeInformation

#}
Write-host "Incoming Emails Report Generated!"

Get External DB List


Add-PSSnapin Microsoft.SharePoint.PowerShell
# Save in a variable the external lists you wish to report on
# in this example, the variable contains all external lists
# in a specific web application
$Lists = Get-SPWebApplication https://sitename| Select -ExpandProperty Sites | Select -ExpandProperty AllWebs | Select -ExpandProperty Lists | where {$_.hasexternaldatasource -eq $true}
# Save the expression to obtain the ECT name in a variable
$e=@{Expression={$_.DataSource.GetEntity().Name}}
# Output external lists grouped by ECT
$Lists | Sort $e | Format-Table Title, ParentWebUrl –Groupby $e

Lock/ Unlock sites


Lock/UnLock sites:

Add-PSSnapin Microsoft.SharePoint.Powershell
$CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition
$Sites= Get-content "$CurrentDirectory\R2Readonly\sitename.txt";
#$Sites= Get-content "$CurrentDirectory\R2Readonly\R2sitename.txt";

Add-Content -Path $logpath -Value ("SiteCollection Url"+","+"Site Owner" )

$input = read-host "Please enter 1 to lock and 2 for unlock sites"
foreach ($site in $Sites)
{
    if($input -eq 1)
    {
         Set-SPSite -Identity $site -LockState "ReadOnly"
        write-host $site " locked" -BackgroundColor Yellow -ForegroundColor Red
      }
   if($input -eq 2)
    {
    
        Set-SPSite -Identity $site -LockState "Unlock"
        write-host $site " Unlock" -BackgroundColor Yellow -ForegroundColor Red
    }
}

Get external lists



Add-PSSnapin Microsoft.SharePoint.PowerShell

$CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition

$Sites= Get-content "$CurrentDirectory\webapp.txt"
$csvPath = "$CurrentDirectory\ExternalList.csv"


foreach ($site in $Sites)
  {
    
      write-host " WebApplication :  $($site)" ;
$SPWebApp =  Get-SPWebApplication $site
 
#then we can pipe the web application object to get all Site collection   
   
$SCall = $SPWebApp | Get-SPsite -Limit All   
 
#this command will get all site collection present in the web application and storing it in a PS object so that we can loop it to get all subsite present in site collection   
   
foreach($sitecollection in $SCall )   
{   

    write-host " Site Collectioin : $($sitecollection.Url)" ;
    $SCwithSubsite = $sitecollection.AllWebs    
      foreach($site in $SCwithSubsite )   
       {   
          write-host "Subsite url : $($site.Url)"
           foreach($list in $site.Lists)   
          {   
            write-host "list  Name: $($list.Title)" -ForegroundColor Yellow
          
              if($list.hasexternaldatasource )   
                {   
                 write-host "list  Name: $($list.Title)" -ForegroundColor Red
          
                    
                      add-content $csvPath "  $($site.Url), $($list.Title),$($list.DataSource.GetEntity().Name)"
                }
             
            } 
         } 
} 
}

Start windows services on Multiple servers


Create Text file in your Desktop with Name server.txt and Service.txt and add the servers and Service accordingly.


$serverList  = Get-Content -Path $env:USERPROFILE\Desktop\Server.txt
$serviceList = Get-Content -Path $env:USERPROFILE\Desktop\Service.txt

ForEach ($server in $serverList)
{
    ForEach ($service in $serviceList)
    {
        Set-Service -ComputerName $server -Name $service -Status Running -StartupType Automatic
        Write-Host "service Started on server" -ForegroundColor Green
    }
}