Friday 26 December 2014

Find SharePoint content Database size and send email.


Find Content Database size and send email to specified users:
 
$date = Get-Date -Format "dd-MM-yyyy"

#Variables that you can change to fit your environment
$TXTFile = "C:\Users\pstsqladmin\Desktop\DB Size\SPContentDatabase_$date.xls"
$SMTPServer = "IP Address of SMTP"
$emailFrom = "emailID@domain.com"
$emailTo = "mailIDs"
$subject = "Content Database size report"
$emailBody = "Automated report on Content databases size"

$webapps = Get-SPWebApplication
foreach($webapp in $webapps)
{
    $ContentDatabases = $webapp.ContentDatabases
    Add-Content -Path $TXTFile -Value "Content databases for $($webapp.url)"
    foreach($ContentDatabase in $ContentDatabases)
    {
    $ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2)
    Add-Content -Path $TXTFile -Value "-     $($ContentDatabase.Name): $($ContentDatabaseSize)GB"
    }
}
if(!($SMTPServer) -OR !($emailFrom) -OR !($emailTo))
{
Write-Host "No e-mail being sent, if you do want to send an e-mail, please enter the values for the following variables: $SMTPServer, $emailFrom and $emailTo."
}
else
{
Send-MailMessage -SmtpServer $SMTPServer -From $emailFrom -To $emailTo -Subject $subject -Body $emailBody -Attachment $TXTFile
}

Get Site collection status


Get SharePoint Site Collection Status in a Web Application

Below Code will help to get all the site collection status in a web application and save the consolidate output in an excel file.

$WebApp= Read-Host 'Enter Web Applcation URL to get site status'

$Sites = Get-SPWebApplication $WebApp | Get-SPSite -limit all  | foreach {
   
 if ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $false)
    {
        $Result ="Not locked"
    }
    elseif ($_.ReadOnly -eq $true -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true)
    {
        $Result = "Read-Only"
    }
    elseif ($_.WriteLocked -eq $true -and $_.ReadLocked -eq $false -and $_.ReadOnly -eq $false)
    {
        $Result = "Adding Content Prevented"
    }
    elseif ($_.ReadOnly -eq $null -and $_.ReadLocked -eq $null -and $_.WriteLocked -eq $null)
    {
        $Result="No Access"
    }
    #Write the Result to CSV file separeted with Tab character
    $_.RootWeb.Title +"`t" + $_.URL + "`t" + $Result | Out-File "File Location\Filename.xls" -Append
}