Featured guest blogger on the Hey Scripting Guy! blog


This one will be short and sweet, as I just want to express my satisfaction with being a guest blogger on the Hey Scripting Guy! blog. I have been a fan of PowerShell for a few years now, and have been blogging for a few years as well – but to be able to contribute PowerShell content to (in my opinion) the best PowerShell blog on the web was very cool.

I will be speaking on September 15th in Charlotte, NC at the 2nd PowerShell Saturday – and Ed Wilson (MSFT Scripting Guy) reached out to me to gauge my interest in being a guest blogger on the HSG blog. Of course I jumped all over that opportunity!

Long story short, I was featured today as a guest blogger – and my topic was “Build Your SharePoint Internet Presence with PowerShell”. I wrote about and described the presentation I will be unleashing at PSSAT002, while withholding all of the juicy details within the script itself.

I encourage you to read the post as well as all of the posts on the Hey Scripting Guy! blog, as it really is a fantastic source of PoSh content. I also encourage you to come to PSSAT002 in Charlotte if you are anywhere near Charlotte, North Carolina in mid-September.

Here is the link to the post on the Hey Scripting Guy! blog.

Creating a Windows Scheduled Task using PowerShell


I find myself creating Windows Scheduled Tasks very often to run a PowerShell Script on a scheduled basis, and while PowerShell V3 will provide Cmdlets to manage the Scheduling Engine – V2 does not. I typically just create the Scheduled Tasks manually, but that is becoming less desirable as I work on larger SharePoint engagements with multiple environments, each with lots of servers. After doing a little online research, I found a few posts which were pretty close – the one which got me 80% of the way was this great post, which ironically was created for the same reason as mine – warming up SharePoint.

I took the code from the post mentioned above and tweaked it for my purposes, and I ended up with a fairly reusable script which takes a few parameters and also uses Get-Credential to actually accept credentials without the need to type the password in plain-text.

The way this script is composed is to create a task on the current machine by default, but by providing the optional HostName parameter – the script can be run against remote machines. It also will schedule it to start running starting tomorrow at 5:00 AM – it will then run daily at 5:00 AM.

Here’s the script!

<#
.Synopsis
	This PowerShell Script is used to create a scheduled task on a specified machine.
.Description
	This PowerShell Script uses the Schedule.Service COM Object to create a Windows
    Scheduled Task on a specified machine. Using the parameters provided, you can 
    specify the name, hostname, program to launch and the location of the script or file you're
    opening. The person executing the script will have to provide credentials.
.Example
	C:\PS>.\Create-ScheduledTask.ps1 -Description 'SharePoint Warmup' -ScriptPath C:\Scripts\Start-SPWarmUp.ps1
	This example creates a scheduled task on the current machine to run a script at C:\Scripts\Start-SPWarmup.ps1
    daily at 5:00 AM. The script will run with highest privileges.
.Notes
	Name: .\Create-ScheduledTask.ps1
	Author: Ryan Dennis
	Last Edit: 7/02/2012
	Keywords: Create Scheduled Task
.Link
	http://www.sharepointryan.com, http://twitter.com/SharePointRyan
#>

[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][System.String]$Description,
[Parameter(Mandatory=$false)][System.String]$HostName=$ENV:ComputerName,
[Parameter(Mandatory=$false)][System.String]$Program="PowerShell.exe",
[Parameter(Mandatory=$true)][System.String]$ScriptPath
)

# Date Variables #
$date = (Get-Date 05:00AM).AddDays(1)
$taskStartTime = $date | Get-Date -Format yyyy-MM-ddTHH:ss:ms

# Get the credentials #
$creds = Get-Credential
$UserName = $creds.UserName
$Password = $creds.GetNetworkCredential().Password

# Build the Argument based on Prefix, Suffix and ScriptPath parameter #
$argPrefix = '-command "& {'
$argSuffix = '}"'
$programArguments = $argPrefix+$ScriptPath+$argSuffix

$service = New-Object -ComObject "Schedule.Service"
$service.Connect($Hostname)
$rootFolder = $service.GetFolder("\")
$taskDefinition = $service.NewTask(0)
$regInfo = $taskDefinition.RegistrationInfo
$regInfo.Description = $Description
$regInfo.Author = $UserName
$settings = $taskDefinition.Settings
$settings.Enabled = $True
$settings.StartWhenAvailable = $True
$settings.Hidden = $False
$triggers = $taskDefinition.Triggers
$trigger = $triggers.Create(2)
#$startTime = "2006-05-02T22:00:00"
$trigger.StartBoundary = $taskStartTime
$trigger.DaysInterval = 1
$trigger.Id = "DailyTriggerId"
$trigger.Enabled = $True
$Action = $taskDefinition.Actions.Create(0)
$Action.Path = $Program
$Action.Arguments = $programArguments
$Principal = $taskDefinition.Principal
# Principal.RunLevel -- 0 is least privilege, 1 is highest privilege #
$Principal.RunLevel = 1
$rootFolder.RegisterTaskDefinition($Description, $taskDefinition, 6, $UserName, $Password, 1)

Get SharePoint Site Quota Templates using PowerShell


A week or two ago I posted a function that allows you to create new SharePoint Site Collection Quota Templates using PowerShell. In that post I mentioned that two more functions (Get, Remove) would be coming soon.

Today I am sharing the function which allows you to get SharePoint Quota Templates. Again, this code is almost verbatim taken from the book by Gary and Shannon, but I have added comment-based-help and put it into an advanced function with CmdletBinding attributes.

The code is pretty simple, and again if you want a detailed walk through – I strongly encourage you to check out Gary and Shannon’s book!

Without further ado, here is the function which I have called Get-SPQuotaTemplate. I’m also finishing up the third function, Remove-SPQuotaTemplate – look for that post very soon!

Simply dot-source the function and then run it like a standard Windows PowerShell Cmdlet.

function Get-SPQuotaTemplate {
<#
.Synopsis
 This advanced function retrieves a SharePoint Site Quota Template.
.Description
 This function uses .NET code to retrieve the Quota Templates collection
 using a Microsoft.SharePoint.Administration.SPWebService object.
.Example
 C:\PS>Get-SPQuotaTemplate -Name "Custom"

 This example retrieves a SharePoint Quota Template called Custom in the
 current farm.
.Example
 C:\PS>Get-SPQuotaTemplate

 This example retrieves all SharePoint Quota Templates in the current farm.
.Notes
 Name: Get-SPQuotaTemplate
 Author: Ryan Dennis
 Last Edit: 5/10/2012
 Keywords: Quota Template, Quotas and Locks
.Link
 http://www.sharepointryan.com
 http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)][String]$Name
)
$Templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates
 if ($Name)
 {
 $Templates | Where-Object {$_.Name -eq $Name}
 }
 else
 {
 $Templates
 }
}

Create SharePoint Site Quota Templates using PowerShell


Last week I was stumbling through a site provisioning script, when I realized I was missing something that I needed – the ability to create a custom quota template and then apply it to a site collection. After doing some quick Get-Command work to try and find the out-of-the-box PowerShell Cmdlets, I quickly realized there are no OOTB Cmdlets to do this.

I was a little frustrated that this didn’t’ exist, but of course when I come across gaps in the OOTB PowerShell support for Microsoft SharePoint 2010 – I immediately look at that as an opportunity to not only figure out the problem, but to share the solution. Luckily for me, as I’ve done a few times now – I came across the solution in Gary Lapointe and Shannon Bray’s book. While they didn’t have a function, they did detail the essentials on how to create the objects necessary to provision quota templates.

After spending a little time this weekend putting it all together, I’m left with a pretty decent function that allows you to create site quota templates easily using PowerShell!

I have added comment-based help to this function, as my intention was to make it as reusable and thorough as possible. The code is pretty simple, and if you want a detailed walk through – I strongly encourage you to check out Gary and Shannon’s book!

Without further ado, here is the function which I have called New-SPQuotaTemplate. I’m also finishing up two other functions, Get-SPQuotaTemplate and Remove-SPQuotaTemplate – look for those soon!

Simply dot-source the function and then run it like a standard Windows PowerShell Cmdlet.

function New-SPQuotaTemplate {
<#
.Synopsis
	This advanced function creates a new Site Quota Template.
.Description
	This function uses .NET code to instantiate an instance of an 
	SPQuotaTemplate class. Once the object is created, an instance of the 
	SPWebService class is instantiated and the Quota Template is added to the 
	Quota Templates Collection.
.Example
	C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 2GB -StorageWarningLevel 1GB -UserCodeMaximiumLevel 100 -UserCodeWarningLevel 75
	
	This example creates an SP Quota Template called Custom with a maximum size
	of 2GB and a warning size of 1GB. Sandboxed solutions are 
	limited to 100, with a warning level of 75.
.Example
	C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 4GB -StorageWarningLevel 3GB
	
	This example creates an SP Quota Template called Custom with a maximum size
	of 4GB and a warning size of 3GB
.Notes
	Name: New-SPQuotaTemplate
	Author: Ryan Dennis
	Last Edit: 4/27/2012
	Keywords: Quota Template, Quotas and Locks
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][Int64]$StorageMaximumLevel,
[Parameter(Mandatory=$true)][Int64]$StorageWarningLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeMaximumLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeWarningLevel
)
# Instantiate an instance of an SPQuotaTemplate class #
Write-Verbose "Instantiating an instance of an SPQuotaTemplate class"
$Quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
# Set the Properties #
Write-Verbose "Setting properties on the Quota object"
$Quota.Name = $Name
$Quota.StorageMaximumLevel = $StorageMaximumLevel
$Quota.StorageWarningLevel = $StorageWarningLevel
$Quota.UserCodeMaximumLevel = $UserCodeMaximumLevel
$Quota.UserCodeWarningLevel = $UserCodeWarningLevel
# Get an Instance of the SPWebService Class #
Write-Verbose "Getting an instance of an SPWebService class"
$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
# Use the Add() method to add the quota template to the collection #
Write-Verbose "Adding the $($Name) Quota Template to the Quota Templates Collection"
$Service.QuotaTemplates.Add($Quota)
# Call the Update() method to commit the changes #
$Service.Update()
}