SQL Saturday #223 – Beginning Powershell for DBAs 1.0 with Sean McCown

Powershell is EXCITING!!!

Write a single line of text to a file – It takes whole mess of VB Script to do what Powershell will do in ONE LINE.

“Book another holiday.” | out-file ./myfile.txt -append

List all the processes on the box – lots of VB code vs a single call to get-process in Powershell.

get-service | out-file ./myservices.txt

get-processes

psdrive – allows you to access the registry and sql server like you would any other drive.

Concatenate string – “My name is: $z” – will concatenate irregardless of data type of $z

$s = “localhost”

ping $s

$array = “server1”, “server2”, “server3”

$array

$array[2]

get-service | get-member (or use gm)

get-process | gm

get-service | format-table (FT) <list of columns, comma delimited>

FL – format list

get-service |%{$_.Kill()} – will kill all services and reboot your machine

invoke-sqlcmd

$server | %{invoke-sqlcmd -serverInstance $_ query “select * from ….”}

$server | %{invoke-sqlcmd -serverInstance $_  -Inputfile}

dir | %{$_.script()} -out-file ./mytables.txt

Carrier method

Leave a Reply

Your email address will not be published. Required fields are marked *