Twitter

Follow SQLQuill on Twitter

Subscribe

MN Pass

Book Review – "Windows Powershell 3.0 Step By Step" by Ed Wilson, O’Reilly Media, Inc.

I’ve as I’ve mentioned many times on twitter, I’ve become quite obsessed with Powershell over the last year and a half.  I recently joined the O’Reilly Blogger Review Program and here is my first review.

Book Summary

Microsoft Certified Trainer, Ed Wilson, covers all of the facets of Powershell 3.0.  Not only does he teach the reader how to use Powershell, he also teaches them to teach themselves Powershell.  The book starts with the basics and builds to expert levels on many topics in Powershell.  There is a section in the introduction on what chapters to read based on your background and ability and hopes/wants/needs for Powershell.  This helped me pick and choose what to read and focus on.  At the end of each chapter, there are exercises that really test your knowledge and drill the information into your brain.  Also, throughout the book, there are Tips and Notes that help find the important information quickly.  Finally, each chapter has a Quick Reference at the end that summarizes the main points which is extremely helpful in getting the information you need.

My Reactions

Since I got this book, I have used it as a reference everyday.  In fact, I added a snippet to my $profile to open it when I open Powershell.  I’ve seen a few videos and demonstrations of the Get-Command and Get-Member functions, but having the hands on exercises along with the the full explanations helped me finally understand how to use these – and that was in the first chapter!  I love the "Hey, Scripting Guy! Blog" and this book uses a similar approach.  It keeps things simple and builds upon the simple things.  Due to the fact that this book uses a straightforward approach and makes the finding of information very easy, I recommend it to anyone interested in learning and/or improving their Powershell skills.

I review for the O'Reilly Blogger Review Program

Powershell Script for SQLSaturdays and Twitter

SQL Saturdays and Twitter go together like peas and carrots. I’ve found the networking page of any SQL Saturday site is very valuable to not only get in contact with people about the event, but also to stay in contact and build long term relationships. Helping plan, organize and speak at SQLSaturday #99, I didn’t want to loose the conncections I had made, so I decided to check out all the twitter accounts of the people who had registered for for it.

What’s the easiest way to do this? POWERSHELL!!!

I stumbled a few times (one time I opened 83 tabs in Chrome and had to stop using twitter for an hour), but here is the script I came up with.

Notes on the script:

#--------------------------------------------------------------------------------------
#Open a tab in Chrome for each twitter account on a SQLSaturday Networking Page
#Created by Andy Lohn - http://www.sqlfeatherandquill.com/
#--------------------------------------------------------------------------------------

$url = "http://www.sqlsaturday.com/99/networking.aspx" #Change this for any SQLSaturday Networking Page
[int]$StartCount = 0
[int]$PageCount = 4 #note this is 0 relative, so this plus 1 for number of tabs to be opened

$chrome = (gi ~\AppData\Local\Google\Chrome\Application\chrome.exe ).FullName
#This didn’t work on one of my machines, so I just set it to the full path of chrome and it worked.
$webclient = New-Object system.net.webclient
$file1="c:\temp\SQLSatDownload.txt"
$file2="c:\temp\SQLSatList.txt"
$file3="c:\temp\SQLSatListwithAddress.txt"
$TURL = "www.twitter.com/"
$file4="c:\temp\SQLSatCounter.txt"

#Create the files if they don't exist
If (!(Test-Path $file1) )
{New-Item $file1 -Type file}
If (!(Test-Path $file2) )
{New-Item $file2 -Type file}
If (!(Test-Path $file3) )
{New-Item $file3 -Type file}

#Set the counter to keep track for multiple runs - 0 if new
If (Test-Path $file4)
{$SCWork = (get-content $file4)
[int]$StartCount = $SCWork.ToString()}
Else
{New-Item $file4 -Type file
add-content $file4 1
[int]$StartCount = 0}

If ($StartCount -eq 0)  #Populate the local file.
{Clear-Content $file1
$webclient.DownloadFile($url,$file1)

$test = get-content $file1
Clear-Content $file2
Clear-Content $file3
$Tester = $test | Select-String -pattern "<a href=""<a href="http://www.twitter.com/"">http://www.twitter.com/"</a> |
% {$_ -replace "<span id=""ContentPlaceHolder1.*<a href="http://www.twitter.com/"">http://www.twitter.com/"</a>, ""} |
% {$_ -replace """ class=""noarrrow"">.*</span>", ""} |
% {$_ -replace "<a href="http://twitter.com/#!/"">http://twitter.com/#!/"</a>, ""} |
% {$_ -replace "                    ", ""} |
where {$_ -ne ""}

foreach ($T in $Tester)
{Add-content $file3 ($TURL + $T)}
}
$TestMethod = (Get-Content $file3)[$StartCount .. ($StartCount + $PageCount)]     $TestMethod | % { &amp; $chrome $_ }
Clear-Content $file4
Add-Content $file4 ($StartCount + $PageCount + 1)

#To restart or change url, run this:
#remove-item $file4

This has to be real easy to change for linkedin – I’m more a twitter guy at this point.  If you do alter it, to work for linkedin, let me know!

Create a List of Links from Google Reader Starred Items with Powershell

Inspired by Jason Strate’s (Blog| Twitter) post “PowerShell: Download List of SQL Server MVPs”, which was inspired by John Samson’s (Blog| Twitter) post “The Best Database Administrators Automate Everything” – which also inspired me.  I decided to create a Powershell script to grab my starred items in Google Reader and format them into an HTML Page with links for my Weekly Challenges/Good Reading Posts.

The Set Up

I had an HTML template created for these pages, so I split it into three parts:

  1. The portion above the Good Reading and Links – I saved this to a text file located at “c:\temp\TopTemplate.txt”
  2. The Good Reading and Links
  3. The portion below the Good reading and Links – I saved this to a text file located at “c:\temp\BotTemplate.txt”

I then looked at my public page for Starred Items on Google Reader.  Note this script works for any public listing on Google Reader, so I can create a Public Page and change the URL in this script and it still works.

The Code

##------------------------##
##--Reader Starred Items--##
##------------------------##
$webclient = New-Object system.net.webclient
$SourceFile = "c:\temp\Reader.txt"
$HTMLPage = "C:\temp\GoodReading.txt"
$TopTemplate = "c:\temp\TopTemplate.txt"
$BotTemplate = "c:\temp\BotTemplate.txt"
$url="http://www.google.com/reader/shared/user/14135896925018987441/state/com.google/starred"
$webclient.DownloadFile($url,$SourceFile)
#Get the titles with links
$itemtitle = Select-String $SourceFile -pattern "h2 class=""item-title""" |
% {$_ -replace "<div id=""items""><div class=""item""><h2 class=""item-title""><div class="""">", ""} |
% {$_ -replace "</div></h2>", ""} |
% {$_ -replace "<div class=""clear""></div></div></div> <div class=""item""><h2 class=""item-title""><div class="""">", ""} |
% {$_ -replace "c:\\temp\\Reader\.txt:[0-9]*:", ""}
#Get the details with links
$iteminfo = Select-String $SourceFile -pattern "<div class=""item-info"">" |
% {$_ -replace "c:\\temp\\Reader\.txt:[0-9]*:<div class=""item-info"">", ""} |
% {$_ -replace "</div>", ""}
$i=0
$FinalContent = " "
foreach ($t in $itemtitle)
{$FinalContent= $FinalContent +"<li>"+ $itemtitle[$i] + " " + $iteminfo[$i]+"</li>" + "`r`n "
$i++}
$FinalContent = "<p><strong>Good Reading from the Week</strong>:</p>" + "`r`n" + "<ul>" + "`r`n" + $FinalContent + "`r`n" + "</ul>"
$TopTemp = Get-Content $TopTemplate
$BotTemp = Get-Content $BotTemplate
$FinalContent = $TopTemp + $FinalContent + $BotTemp
Set-Content $HTMLPage $FinalContent
invoke-item $HTMLPage

When the script completes, I simply update a couple of statuses and change the photo and it’s good to go!

How To: Replace a string in all files in a directory with Powershell

Today I realized I made a fairly significant mistake.  I had misspelled a column name in a bunch of procedures and views that I had updated.  Not a big deal, except each procedure and view was a file and I had about 75 files.  Only a few of them contained this change, I don’t know which ones, but most of them had other changes.  What to do…

  • I could manually check all files with a few routine of Keystrokes – that might take an couple of hours.
  • I could check sysdepends for dependancies – nope none of the procedures have been executed.
  • I could check syscomments for the column name – that would kind of work, but I would still need to manually update it.
  • Powershell!!  I could find and replace in the files and execute them as well!

I used the following sites as they have good examples.

Here’s what I came up with:

##Finds all the occurrences in the Views folder##
foreach ($f in Get-ChildItem -path "C:\Temp\Views\" -Filter *.sql | sort-object)
{$Location = $f.fullname
Select-String -path $Location -pattern "Andee"| out-file -filePath "C:\Temp\ListoObjects.txt" -append}

##Replaces all the occurrences in the Views folder##
foreach ($f in Get-ChildItem -path "C:\Temp\Views\" -Filter *.sql | sort-object)
{(Get-Content $f.fullname) |
Foreach-Object {$_ -replace "Andee", "Andy"} |
Set-Content $f.fullname}