{"id":32,"date":"2013-08-24T19:25:58","date_gmt":"2013-08-24T19:25:58","guid":{"rendered":"http:\/\/www.sqlkitten.com\/?p=32"},"modified":"2013-08-24T19:25:58","modified_gmt":"2013-08-24T19:25:58","slug":"sql-saturday-223-beginning-powershell-for-dbas-1-0-with-sean-mccown","status":"publish","type":"post","link":"http:\/\/www.sqlkitten.com\/?p=32","title":{"rendered":"SQL Saturday #223 &#8211; Beginning Powershell for DBAs 1.0 with Sean McCown"},"content":{"rendered":"<p>Powershell is EXCITING!!!<\/p>\n<p>Write a single line of text to a file &#8211; It takes whole mess of VB Script to do what Powershell will do in ONE LINE.<\/p>\n<p>&#8220;Book another holiday.&#8221; | out-file .\/myfile.txt -append<\/p>\n<p>List all the processes on the box &#8211; lots of VB code vs a single call to get-process in Powershell.<\/p>\n<p>get-service | out-file .\/myservices.txt<\/p>\n<p>get-processes<\/p>\n<p>psdrive &#8211; allows you to access the registry and sql server like you would any other drive.<\/p>\n<p>Concatenate string &#8211; &#8220;My name is: $z&#8221; &#8211; will concatenate irregardless of data type of $z<\/p>\n<p>$s = &#8220;localhost&#8221;<\/p>\n<p>ping $s<\/p>\n<p>$array = &#8220;server1&#8221;, &#8220;server2&#8221;, &#8220;server3&#8221;<\/p>\n<p>$array<\/p>\n<p>$array[2]<\/p>\n<p>get-service | get-member (or use gm)<\/p>\n<p>get-process | gm<\/p>\n<p>get-service | format-table (FT) &lt;list of columns, comma delimited&gt;<\/p>\n<p>FL &#8211; format list<\/p>\n<p>get-service |%{$_.Kill()} &#8211; will kill all services and reboot your machine<\/p>\n<p>invoke-sqlcmd<\/p>\n<p>$server | %{invoke-sqlcmd -serverInstance $_ query &#8220;select * from &#8230;.&#8221;}<\/p>\n<p>$server | %{invoke-sqlcmd -serverInstance $_\u00a0 -Inputfile}<\/p>\n<p>dir | %{$_.script()} -out-file .\/mytables.txt<\/p>\n<p>Carrier method<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Powershell is EXCITING!!! Write a single line of text to a file &#8211; It takes whole mess of VB Script to do what Powershell will do in ONE LINE. &#8220;Book another holiday.&#8221; | out-file .\/myfile.txt -append List all the processes &hellip; <a href=\"http:\/\/www.sqlkitten.com\/?p=32\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/32"}],"collection":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=32"}],"version-history":[{"count":2,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":38,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions\/38"}],"wp:attachment":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}