Month: December 2008

Powershell Tip: Put Your Things into the Library

I have had several people comment on my presentation at IndyPASS about the use of what I termed a ‘Library File’. So I decided to give an example of one and explain it usefullness a little. First off, this file is nothign more than a plain ole powershell file. What it is used for is to store the call to your favorite powershell function files so that you do not have to constantly type the . ./LoadMyFunctionPlease.ps1 calls at the top of your scripts. Below is an example of the contents of my example file….. #Source all of our coolio libraries . ./CheckPing.ps1 . ./CheckDrive.ps1 [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) . ./CheckDBBackupSimple.ps1 Here I just simply place what I deem to be my ‘Cool’ or ‘Coolio’, for the west coast hipsters, function calls and assembly loads. So from the example above, you can clearly see that I am ‘loading’ into my environment my two powershell functions for checkping and checkdrive…as well as the .NET assembly for SMO. Lastly, another CheckDBBackupSimple.ps1 to get the CheckDBBackupSimple function. Now instead of killing my fingers every time I write a new powershell script. I merely have to use the syntax that I used in my previous post on reading in a file. . ./SQLScripts_Lib.ps1 A whole lot less typing involved this way. Hopefully, this post will cut down on the amount of carpal tunnel syndrome out there....

Read More

Powershell Tip: Read a Text File

So building upon our last example, we would like to of course run this function for a certain list of servers. We could just hard code another script to use in order to write out all those servers….but that would be labor intensive and we want to simplify! So the easier answer would be to just keep a list of servers in a text file. Easy to update…Easy to maintain. Then we would consume this list in our script and call our little backup function recursively. So here we go….First, you would create a simple list of server in a text file. Place that file in your scripts directory. It should contain one server entry per line. Now let’s put together our script… # Sample Script to do monitoring . ./SQLScripts_Lib.ps1 #Read in a list of servers foreach ($srv in Get-Content “C:\SQLScripts\ServerList.txt”) { GetBackupInfo($srv) }   That’s it. Completely too simple. The Get-Content call reads in the contents of the file that it is given(full path please). We stick this in a variable and pass it off to our function. Now all we have to do if we add a server is update a simple text file. Hope this helps some people out! Cheers!AJ Tags: powershell, sql+server+2008+powershell, sql+server, oop, scripting, windows+powershell,...

Read More