Intro to SQL Server Automation with PowerShell

March 26th, 2015, a date which will live in infamy!

Well, at least for me it will. March 26th, 2015 marks the date I gave my first presentation at my local SQL PASS user group meeting. Here is the picture as proof!

I was very excited to be able to share some of my knowledge with everyone who showed up. I hope to be able to do it again some time.

[Read More]

Gotcha! PowerShell Array Initialization, Difference between PowerShell 2.0 and 4.0

I like to initialize my arrays by strongly typing my variable as an array, and setting it to null. PowerShell doesn’t require strongly typed variables, so why would I want to do this?

The primary reason is because it allows me to deal with an expected object type later in the code. For arrays it gives me access to a count property, which I don’t get with strings, int’s or other non-collection type objects.

[Read More]

Using New-TimeSpan to convert friendly time

The question was this:

This is just a quick/fast snippet in response to that question, but I can think of a ton of enhancements that could made. Hopefully it will be useful as a starting point for someone to customize for their own use.

function Convert-FriendlyTime
{
    param (
        [string]$FriendlyTime 
    )
    
    $Converted = $FriendlyTime.Split(" ")
    
    switch ($Converted[1])
    {
    "days"    { $parms = @{"days"=$Converted[0]}; break ;}
    "hours"   { $parms = @{"hours"=$Converted[0]}; break ;}
    "minutes" { $parms = @{"minutes"=$Converted[0]}; break ;}
    "seconds" { $parms = @{"seconds"=$Converted[0]}; break ;}
    "default" { $parms = $null; break ;}
    }
    if ($parms) {
        New-TimeSpan @parms
    } else {
        Throw "Invalid unit of time '$FriendlyTime'. Please use days, hours, minutes or seconds."
    }
}

Convert-FriendlyTime -FriendlyTime "2 days"
Convert-FriendlyTime -FriendlyTime "68 hours"
Convert-FriendlyTime -FriendlyTime "68 InvalidUnits"

Configure SQL Server Agent using SMO

Saw this on twitter and thought I would throw it up on the blog. Easier than responding on twitter.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;

$SQLServerInstance = "MSSQLSERVER"

$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $SQLServerInstance
$srv.JobServer.MaximumHistoryRows = 1002
$srv.JobServer.Alter()

You can manipulate most if not all SQL Server configurations through SMO. This simple post has inspired me a bit. I think it would be useful to show how I came to find the information I needed to modify this setting. The cool thing about PowerShell is that it’s in there. All the tools you need already exist. Look back for an expansion of this post in the near future.

Get-DiskSpace

My cheap and dirty PowerShell script for dumping the disk space usage for a Windows machine. It was originally some snippets I found on the Internet, which I threw into a script and made it my own.

It uses a simple WMI call to return a set of objects for you to use as you see fit. Most of the time I just pipe it to Format-Table to see the output, but it could be used for other things. Throw in a pipe to Where-Object and you could use it to monitor low disk space. Feel free to bind, spindle or mutilate as you see fit.

[Read More]