Category Archives: SMO

Re-Post: SQL Server and PowerShell WebCasts

I created a 2 part webcast on SQL Server and PowerShell last November/December, and I just wanted to re-post it in my site.

SQL Server PowerShell: Part I

Posted on November 25, 2009

Overview: This video is part 1 of a 2 part video series that goes through PowerShell basics with demonstrations on how to use PowerShell with SQL Server.

http://vimeo.com/moogaloop.swf?clip_id=7826965&server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=00adef&fullscreen=1

SQL Powershell Part One authored by Donabel Santos (@sqlbelle) on Vimeo.

Author: @sqlbelle

Recorded On: 17″ MacBook Pro

Software: Telestream’s ScreenFlow

SQL Server PowerShell: Part II

Posted on December 3, 2009

Overview: This video is part 2 of a 2 part video series that demonstrates how to use PowerShell with SQL Server, using the SSMS built-in mini shell, and using .NET and SMO.

http://vimeo.com/moogaloop.swf?clip_id=7855533&server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=ff9933&fullscreen=1

SQL Powershell Part Two Donabel Santos (@sqlbelle) on Vimeo.

Created for the Edmonton PASS user group by Donabel Santos at Black Ninja Software.

Author: @sqlbelle
Recorded On: 17″ MacBook Pro
Software: Telestream’s ScreenFlow

VANPASS Presentation and Demo Files on SQL Server and PowerShell

I did a presentation today for VANPASS on PowerShell and SQL Server – thank you to those who attended! It was a great lively crowd 🙂 Thanks to Idera too – for the freebies – and to Black Ninja Software for the pizza and pop!

Here are the files as promised: VANPASS – SQL Server and PowerShell – Donabel Santos

Read more

SQL Server PowerShell : Search for SQL Server Objects Using PowerShell

I have posted previously 3 different ways of searching for SQL Server Objects (How to Search for Columns in SQL Server ).

Here’s a fourth one!

Check out how powerful and flexible PowerShell is when you need to look for a database object. In the script below, I only search databases, tables, columns, and indexes. But in reality, really, sky is the limit!
Read more

SQL Server PowerShell : How to Audit Your SQL Server Instance Properties Using PowerShell and SMO

Here is a simple script to audit your SQL Server Instance Properties.

Note that the property names are not hardcoded. We query each of these properties, and use those to display the property values.

If you prefer, you can also query directly each of the properties of an instance. If this is the case, just specify your server object and then the property name. For example:

$serverObject.BackupDirectory

The list of properties can be found at the end of the post.
Read more

SQL Server PowerShell : How to Restore SQL Server Databases Using SMO and PowerShell

A follow-up to my previous post: How to Restore SQL Server Databases Using SMO and PowerShell

In this post I will show you how you can restore your database to :

1. the existing database (same database name)
2. a different database (different database name, different mdf and ldf)

As with the previous post, to do a SQL Server restore in SQL Server, you will need to use the SMO SqlBackup method. In SQL Server 2008, you will need to load Microsoft.SqlServer.SmoExtended assembly otherwise, you will get the following error:

Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure the assembly containing this type is loaded.

Other assemblies you may want to load are:

[powershell collapse=”false” firstline=”1″ gutter=”true” smarttabs=”true” tabsize=”4″ toolbar=”true”]

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
[/powershell]
Read more

SQL Server PowerShell : How to Backup SQL Server Databases Using SMO and PowerShell

PowerShell makes it easier to manage even your database backups and restore.

To do a SQL Server backup in SQL Server, you will need to use the SMO SqlBackup method. In SQL Server 2008, you will need to load Microsoft.SqlServer.SmoExtended assembly otherwise, you will get the following error:

Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure the assembly containing this type is loaded.

Other assemblies you may want to load are:

[powershell collapse=”false” firstline=”1″ gutter=”true” smarttabs=”true” tabsize=”4″ toolbar=”true”]

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
[/powershell]

Also another point to note is the types of backup you can do. BackupActionType specifies the type of backup. Valid values for this option are Database, Files, Log

Here’s the script. This script is for one specific database. If you want to use this for several database, you will just need to use this code inside a loop.
Better yet, put this in a function, and call this in a loop. I will try to do that sometime soon.
Read more