Sql Server Powershell

Sharepoint 2010 Edit Page Button Missing

I just fought with this for two days.  Apparently, the “edit page” button on Sharepoint 2010 only shows on regular pages, not web part pages.  Who knew.  This was a migrated Sharepoint application from 2007 to 2010, so I had no idea that the page was a web part page and not a regular page.

Sql Server Powershell Snapin

Might as well show you the code to load the Sql Server snapin for powershell as well.  I have this in my $profile, as I use the snapin constantly.

function LoadSQLSnapin()
# Add the SQL Server Provider.
$ErrorActionPreference = "Stop"
if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
   throw "SQL Server Provider for Windows PowerShell is not installed."
   $item = Get-ItemProperty $sqlpsreg
   $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
# Set mandatory variables for the SQL Server provider
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000
# Load the snapins, type data, format data
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml

Get ServerNames from SQL Server in PowerShell

I see a lot of posts on monitoring sql server that use a “Servers.txt” to iterate the server names.  We (and I imagine a lot of other places as well) store our server information in sql server and not a text file.  Here is how you get said list of servers directly from sql server instead of a text file:

$Servers = Invoke-Sqlcmd -Query "SELECT
   FROM dbo.[Servers];"
foreach($server in $Servers)
       Write-Host $server.ServerName

You’ll need to load the sql server snapin for this to work correctly.

Compare jobs with PowerShell

I’m more amazed every day at how much easier powershell makes administering sql server.  We currently have this big messy home-grown monitoring system to compare and track changes in sql server.  It’s complicated, fragmented and difficult to debug.  I just duplicated the whole job-compare section of the monitoring system with 9 lines of code.  This will tell you which jobs were added and which ones were removed.  I’ve not tested it thoroughly (nay, at all), like what happens when you change a schedule on a job or whatnot, but this is a good start:

$jobs = Get-ChildItem
$jobsPrevious = Import-Clixml -Path C:\JobsPrevious.xml
$difference = Compare-Object -ReferenceObject $jobsPrevious -DifferenceObject $jobs
$Removed = @($difference | Where {$_.SideIndicator -eq "<="} | foreach {$_.InputObject}) -join ", "
$Added = @($difference | Where {$_.SideIndicator -eq "=>"} | foreach {$_.InputObject}) -join ", "

Write-Host "Removed:  $Removed"
Write-Host ""
Write-Host "Added:  $Added"

This just does the jobs, but there’s no reason why you couldn’t apply this code to other nodes in the SMO object model.

Powershell is amazing.  If you’re not using powershell to administer SQL Server, you’re really doing yourself a HUGE disservice.  Use at your own risk.

Update:  The xml that returns from get-childitem will be huge.  Also note, that you should actually write the $jobs out to file and re-load it using the Import-Clixml in order for the compare-object to compare properly.  I’m STILL working on getting this up to snuff, but I’m currently stretched too thin to dedicate any time to this at the moment.  I’ll put up a better post once I’m done with it, but this is a good starting point.

Powershell script to check if LiteSpeed is installed

You could easily modify this to check if anything is installed really.

function CheckLiteSpeed($machineName)
       Write-Host $machineName
       $version = "";
       $reg = [WMIClass]"\\$machineName\root\default:stdRegProv"
       $HKEY_LOCAL_MACHINE = 2147483650
       $strKeyPath = "SOFTWARE\Imceda\SqlLiteSpeed\Engine"
       $version = $reg.GetStringValue($HKEY_LOCAL_MACHINE,$strKeyPath,"Version").svalue
       if($version -eq $null)
               return "Not Installed"
       return $version
$destFile = "C:\LiteSpeedInstall.csv"
$servers = Get-Content "C:\Servers.txt";
       Remove-Item -LiteralPath $destFile
Add-Content -LiteralPath $destFile "ServerName,LiteSpeedVersion"
foreach($server in $servers)
       $strVersion = "";
       $strVersion = CheckLiteSpeed($server);
       Add-Content -LiteralPath $destFile "$server,$strVersion";

Use at your own risk.  Not rigorously tested.

Bender out.

I want to go to Dallas

I think there was a Redd Foxx joke about going to Dallas…but I digress..

I'm a sponge!

The folks at sqlskills.com are offering up free tuition to their 5-day Internals and Performance class in Dallas, February 21-25.  I would kill for a chance to take this class.  (seriously, email me at bender@dirtydeedsdonedirtcheap.com).  I’m not too sure about the bottomless tea part though.  I would imagine it would burn the nethers badly.

I’ve been absolutely devouring all the content that these guys have been throwing out lately, and would love a chance to learn it straight from the authors themselves.  I’ve watched pretty much all of their MCM training videos (some twice!).  I’ve also been studiously going through the Sql Server 2008 Internals book that they’ve co-authored.  Honestly, I didn’t know how much I didn’t know until I recently committed to learning everything I can about Sql Server.

Now, why I deserve this:

  1. I recently have stepped up into the Senior DBA position at my place of employment, hence my studiousness.
  2. Since undertaking said studiousness, I’ve found a forgotten love for learning.  I’m really really enjoying learning all the internals of Sql Server.  (I’m a born-again DBA of sorts I guess).
  3. My company won’t pay for it.
  4. I want to become a Sql Server MCM.  I’m trying really hard to get to that level.
  5. I just want to know everything about Sql Server!  I’m a sponge.
  6. I’m really excited about learning from Paul, Kimberly, Bob and even Brent!

Bender out.

PowerShell script to get only the most recent backups

Here’s a neat PowerShell script to grab only the most recent backup files.  Given a path, it will recurse the directories and only return the most recent *.bak file in all subdirectories.

$path = "H:\SourceDir"
$destPath = "\\Destination"

function CopyLastBackup
        Remove-Item "$destPath\*" -include "*.bak"
        $dirs = Get-ChildItem $path | where {$_.PSIsContainer} | where {$_.GetFiles().Count -ne 0}
        foreach($dir in $dirs)
			$file = Get-ChildItem -LiteralPath $dir.FullName -include "*.bak" | sort CreationTime -Descending | select -First 1
        	Copy-Item -LiteralPath $file.FullName -Destination $destPath -force
            write-host $file 
        return 0;
        write-host $error[0]
        return 1

$ret = CopyLastBackup
if($ret -eq 1)
    throw "Failure"