Friday, March 22, 2013

Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

PowerShell is really a game changer for repeatable processes, isn't it? We've a PowerShell script to generate report on SharePoint content databases size growth SharePoint Content Databases Size - Storage Report, We used to run it on first day of every Month on SharePoint server to generate report.
Why don't we automate it with Windows Task scheduler? Sure! Lets create a scheduled task for PowerShell script with Windows task scheduler in windows server 2008 R2.

How to Create a Scheduled Task for PowerShell Script with Windows Task Scheduler
Here is how you can create Scheduled Tasks manually:

1. Start >> Administrative Tools >> Task Scheduler.  From Actions menu click on "Create a Basic Task"
windows task scheduler run powershell script
2. Give it a Name Description Say: Content Databases Report, and click "Next"
windows task scheduler powershell script 
3. Select the interval you want to run the program. In my case, I chose "Monthly"
powershell script task scheduler windows 2008 r2

4. Specify the Months, Days in which the script to run. I've selected "All Months" and Day 1
schedule powershell script using task scheduler

5. In Action Tab, Select "Start a program" option button. Click Next
task scheduler to run powershell script 
6. In Start a Program Tab:
  • In Program/script, Enter: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • In Arguments, Enter the path of the script. Say: D:\Scripts\ContentDatabaseReport.ps1
  • In Start in, Enter the path where the script is located. say: D:\Scripts
powershell script scheduled task parameters
You must specify value for Start-in field, even though its optional. This is why because, if no value specified there, PowerShell will create the output in "C:\Windows\System32" directory.

7. Select the check box, "Open the Properties dialog for this task when I click Finish" and click Finish button.

8. Now, in the properties dialog, under the General tab, Make sure that the "Run when user is logged on or not" and "Run with highest privileges" check boxes are selected.
You will get a login prompt on clicking "OK" button. Enter the User Name & Password in which the task runs.

Create Tasks in Task Scheduler Command Line:
We can create scheduled task with the command line tool schtasks too!
schtasks /create /TN "Content Database Report" /SC monthly /mo 1 /ST 00:00 /RU "Domain\UserName" /RP "Password" /RL "HIGHEST" /TR "PowerShell D:\Scripts\ContentDatabaseReport.ps1" 

This will set the options "Run with highest privileges" and "Run whether the user is logged on or not"  for the Scheduled Task.

To Run the Scheduled Task on-demand:
  • Right click on the created task and choose Run.
  • switch over to the script file location and verify new report has been generated as an output from the script.
run scheduled task on demand

That's all! We've created a task in windows task scheduler for powershell script!

PowerShell script with parameters in scheduled task: 
Say, We've a Parameter "$WebAppURL" in our PowerShell script:

Param(
       [parameter(Mandatory=$true)] $WebAppURL
     )
 
 # We'll trigger the script as:  .\StorageReport "http://sharepoint.crescent.com"

So in Task scheduler, Add arguments(optional), Enter: d:\scripts\StorageReport "http://sharepoint.crescent.com"

When there are multiple parameters, you can separate them by giving parameter name as key. such as:

Param(
       [parameter(Mandatory=$true)] $WebAppURL,
       [parameter(Mandatory=$true)] $OutPut
     )

# We trigger it as: StorageReport -WebAppURL "http://sharepoint.crescent.com" -output "d:\Reports\StorageReport.csv"


Read more: http://www.sharepointdiary.com/2013/03/create-scheduled-task-for-powershell-script.html#ixzz2OG7t6Ebg


SharePoint Content Databases Size - Storage Report

Every Month I've had to generate the Storage report for SharePoint Content Databases. How about automating it with PowerShell? Sure, Here is my code:

Code:-


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$ReportDate=get-date -Format "MMM-dd-yyyy"

$OutputFN = "D:\Database_Storage_$ReportDate.csv"

#If the File already exists remove it
if (Test-Path "D:\Database_Storage_$ReportDate.csv")
{
    Remove-Item "D:\Database_Storage_$ReportDate.csv"
}

#Write CSV Header
 Add-Content -Path $OutputFN -Value "Web Application Name, URL, Database Name, Database Size-GB"
     
#Get All Web Applications
$webapps = Get-SPWebApplication
foreach($webapp in $webapps)
{
    $ContentDatabases = $webapp.ContentDatabases
     
    foreach($ContentDatabase in $ContentDatabases)
    {
        $ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2)

        Add-Content -Path $OutputFN " $($webapp.Name),  $($webapp.url) , $($ContentDatabase.Name) , $ContentDatabaseSize"
    }
}

#Code To mail the Report
$SMTPClient = new-object System.Net.Mail.smtpClient
$SMTPClient.host = "smtp.crescent.org"

$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.Subject = "SharePoint Storage Report - $ReportDate"
$MailMessage.Body = "Please find attached Database storage report as on $ReportDate"
$MailMessage.From = "SharePoint-Reports@Crescent.com"
$MailMessage.To.add("Salaudeen.Rajack@Crescent.com")
$Attachment = new-object System.Net.Mail.Attachment($OutputFN)
$MailMessage.Attachments.Add($Attachment)
$SMTPClient.Send($MailMessage)

$Attachment.Dispose();
$MailMessage.Dispose();

#Remove the Report from Server once mailed!
Remove-Item "D:\Database_Storage_$ReportDate.csv"


Output: (After adding a Pivot Table)

Tail: Once you have the script ready, You can place it under Task scheduler to run the script on scheduled basis! like: PowerShell.exe D:\Reports\StorageReport.ps1 

Read more: http://www.sharepointdiary.com/2012/05/sharepoint-content-databases-size.html#ixzz2OG8AeulW

No comments:

Post a Comment