Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Sunday, June 17, 2012

Disk Size Report of all SharePoint Databases using PowerShell into Mailbox


As an administrator I always use to find the database size of all the SharePoint DB. Which I use to do with the PowerShell script, but lately I got pissed off doing this same activity everyday so I decided finally, write a PowerShell script and using a windows scheduler automate this. So now I will get all these details on my mail every morning when I come to office.
 


So what the below script do………………

The script first section (1) is used to get information about the mail exchange. 

The script second section (2) is, for-each loop which gets content database of all the web application. This then writes this information on to a file.

The script third Section (3) is, for-each loop which get all the SharePoint database size details which is also appended to the file.

Finally the fourth section (4) sends this information on a mail as an attachment.
The result of this code will be:

So the full code for download is :
#Get SharePoint Content database sizes 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
$date = Get-Date -Format "dd-mm-yyyy"
 
#Variables that you can change to fit your environment 
$TXTFile = "C:\ContentDatabase_$date.txt" 
$SMTPServer = "yourmailserver" 
$emailFrom = "SharePointReports@company.com" 
$emailTo = "youradmin@company.com" 
$subject = "Content Database size reports" 
$emailBody = "Daily/Weekly/Monthly report on Content databases"
 
$webapps = Get-SPWebApplication 
foreach($webapp in $webapps) 
{ 
    $ContentDatabases = $webapp.ContentDatabases 
    Add-Content -Path $TXTFile -Value "";
    Add-Content -Path $TXTFile -Value "----------------------------------------";
    Add-Content -Path $TXTFile -Value "Content databases for $($webapp.url)" 
    Add-Content -Path $TXTFile -Value "----------------------------------------";
    Add-Content -Path $TXTFile -Value "";
    foreach($ContentDatabase in $ContentDatabases) 
    { 
        $ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2) 
        Add-Content -Path $TXTFile -Value "-     $($ContentDatabase.Name): $($ContentDatabaseSize)GB" 
    } 
} 
 
$databases = Get-SPDatabase
Add-Content -Path $TXTFile -Value "";
Add-Content -Path $TXTFile -Value "------------------------------------";
Add-Content -Path $TXTFile -Value "Content Size of Individual Databases";
Add-Content -Path $TXTFile -Value "------------------------------------";
Add-Content -Path $TXTFile -Value "";
foreach($database in $databases)
{
    Add-Content -Path $TXTFile -Value  " $($database.Name)  ---- $($database.disksizerequired/1024/1024) MB"    
}
 
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 
}
Now How to use windows scheduler to automate this mailer on a daily basis you can refer to my earlier post http://sharepointdrive.blogspot.sg/2012/04/automating-sharepoint-site-backup-using_14.html

Please let me know if you face any difficulty in understanding or running this up. Also for more information on PowerShell go to http://sharepointrelated.com/

Saturday, April 14, 2012

Automating SharePoint site backup using PowerShell and windows Task Scheduler (Part 1)

Powershell Backup

SharePoint administrators need to run regular backups using PowerShell, the STSADM tool or in Central Administration. But taking these backups on a daily basis can be a tedious process, hence either we can sit back and take backup, waiting for it to get over or we can go home and sleep on the couch, while the PowerShell and Task Scheduler take cares of the rest.

So in this series we will see
  1. How to write a PowerShell script to take Backup. (Part 1)
  2. How to use windows scheduler to automate the Backup on a daily basis. (Part 2)
Creating PowerShell script to take Backup

I have included the whole script first which i have then explained below in phases
Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
 {
    $today = (Get-Date -Format dd-MM-yyyy)
    $backupDirectory = "D:\Backup\DailySiteCollectionBackUp\$today"
  # Backup file Location
    $backupFile = "D:\Backup\DailySiteCollectionBackUp\$today\Backup.dat"
  # Log file location
    $logFile = "$backupDirectory\BackupLog.log"    
  # Address of the Site Collection to backup
    $Site = "http://a4md03082:10000/"
  
 # Location of the Backup Folder
    if (-not (Test-Path $backupDirectory)) 
    { 
      [IO.Directory]::CreateDirectory($backupDirectory)
      #New-Item $logPath -type $backupDirectory
    } 
 
 # Get backup start date and time 
    $backupStart = Get-Date -format "MM-dd-yyyy HH.mm.ss"
  
  # creates a log file  Start-Transcript -Path 
    Start-Transcript -Path $logFile
    
 # This will actually initiate the backup process. 
      Write-Host    
      Write-Host    
      Write-Host "Backup starting at $backupStart for $Site "    
      Write-Host "******************************************"
     Backup-SPSite -Identity $Site -Path $backupFile -Force
     $backupComplete = Get-Date -format "MM-dd-yyyy HH.mm.ss"
      Write-Host    
      Write-Host    
      Write-Host "Backup Completed at $backupComplete for $Site "    
      Write-Host "******************************************"
 
 Stop-Transcript 
 }
Catch
 {
  $ErrorMessage = $_.Exception.Message
  write "$today BackUp Failed   $ErrorMessage  ">>$logFile

 }

a) It will load the SharePoint snap IN
Including Powershell SnapIn
Including PowerShell SnapIN
b) It will create a folder with the current Date if not present.

Creating BackUp Folder
Note: Do change the path according to the place where you want to store the backup.

c) Then we will run the “Start Transcript/Stop-Transcript” block command. This will log all command that the user types and all output that appears on the console. Here we will require to pass the log file name. Between the “Start Transcript/Stop-Transcript” we will take the backup with the Backup-SPSite which will take the backup of the specified site

Create Backup Folder

Note: Start Transcript/Stop-Transcript command does not runs on the PowerShell ISE, this will run only on the console, which is the way we are going to do while running it from windows task scheduler.

d) Now after the whole script is created save it with a .ps1 extension.



Now in the Part 2 we will understand on how to run this script at a particular time of the day automatically.