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

The zipped file contains the presentation and all of the samples (ones that I demo’d, and ones I didn’t have time to demo). The demos are:

  • Demo 01 – Common PowerShell Tasks – Complete.PS1
  • Demo 02 – PowerShell Basics – Complete.PS1
  • Demo 03a – SQL Server 2008 Mini Shell – Complete.PS1
  • Demo 03b – Server and Database Properties.PS1
  • Demo 03c – SQL Server Services and Jobs.PS1
  • Demo 03d – Scripting SQL Server Objects.PS1
  • Demo 03e – Searching for Objects.PS1
  • Demo 03f – Basic DDL and DML Import.PS1
  • Demo 03f – Basic DML.PS1
  • Demo 03g – Backup and Restore.PS1
  • Demo 04 – SQL Server PSX – Complete.PS1
  • Demo 05 – SQL Server PSSnapin – Complete.PS1
  • Demo – SSIS.PS1
  • Demo – SSRS.PS1
  • Out-Report from Chad Miller

I have used a lot of resources when I was creating all these samples, and I have tried to enumerate them in my presentation. And I also just realized I had a lot of samples and was only able to get through 2/3 of them.

I enjoy presenting at VANPASS, it is always a great crowd. Please feel free to drop me a line, or comment on my blog, or message me on twitter(sqlbelle) – if you have other SQL Server PowerShell questions, or if you had questions at the presentation that I wasn’t able to address.

Thanks to Richard Baumet and Scott Stauffer for inviting me to speak.
And thanks to Idera too – I love using your PowerShell Plus!

VN:F [1.9.22_1171]
Rating: 9.9/10 (8 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

SQL Server PowerShell : Basics – Connecting to SQL Server

PowerShell is a great tool that allows DBAs and Developers alike to script, manage and automate SQL Server tasks. Most of the objects and tasks will require SMO (SQL Server Management Objects).

Basic Steps

1. Set Execution Policy

Depending on what you need to do, you may need to set the Execution Policy in your Powershell console. In my case, I will be using it to access SQL Server for administrative purposes, so I will be setting mine to have Unrestricted access to my server. Make sure you review the security guidelines in your organization before you set your environment to Unrestricted

PS> Set-ExecutionPolicy Unrestricted

2. Load assemblies

First, you will need to load SQL Server related assemblies. For SMO, you will usually want to load the following assemblies:

  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SqlEnum
  • Microsoft.SqlServer.SmoEnum
  • Microsoft.SqlServer.ConnectionInfo

When you load the assemblies, you have to use the following format:

[Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, `
      Version=9.0.242.0, Culture=neutral, ` 
      PublicKeyToken=89845dcd8080cc91")

A couple of things to note here:

  • the backtick is the line continuation character, ie if your line of code spans multiple lines, you have to use a backtick
  • you can get the version of the assembly either by :
    • going to the GAC (C:\Windows\assembly), right click on assembly and click on properties
    • use reflector

SQL Server SMO assembly

Read the rest of this entry »

VN:F [1.9.22_1171]
Rating: 8.5/10 (26 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)

How to Connect to an Instance Name Using SMO

1. Create your Visual Studio Project

2. Add the following references in your project

  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SmoEnum
  • Microsoft.SqlServer.SqlEnum

3. Include the following using statement:

using Microsoft.SqlServer.Management.Smo;

4. Add your server object. Normally, you would use the following format in your Server object:

String serverName  = "JUBILEE";
Server srv         = new Server(serverName);
Database db        = srv.Databases["AdventureWorks"];
txtServerInfo.Text = srv.Information.VersionString +
                     System.Environment.NewLine;

However, if you have an instance name, you have to specify the SERVER\INSTANCE name in your Server object. In C# code:

String serverName = “JUBILEE” + @”\” + “SQL01“;

Server srv         = new Server(serverName);
Database db        = srv.Databases["AdventureWorks"];
txtServerInfo.Text = srv.Information.VersionString +
                     System.Environment.NewLine;

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
`