Recently I have been dabbling a bit with Ubuntu and I really liked the command line approach because it feels like you got this powerful sword that you can wield around. Linux is great, but I am still a Windows person (bad I know) but its the truth. So I was looking around and reading and at one point today I wondered if you can connect to a Linux shell through Windows Powershell, but it didnt seem to evident. All the thinking and reading got me to search for a Powershell 2.0 “download page”, which doesnt really exist. You have to download an update for Windows XP (if you are running Windows XP like me). Windows 7 has Powershell installed by default. Once you have downloaded and installed it you are ready to use Windows Powershell. I found two items under my Accessories –> Windows Powershell menu:
The first item takes you directly to the command line and the second one takes you to an IDE for debugging and testing your Powershell scripts, which I found useful for syntax checking. My first script was just a test to print some text on the commandline:
To execute you run this in the commandline (if you have saved the script above to a folder called powershellscripts):
The code above was just for myself and so that I could get some of basic mechanics right. My goal was to create a script that could connect to a SQL Server database and retrieve data, and store it. I also found two places useful as reference for examples:
I looked at a few samples and came across a sample that connected to an OLE DB (Microsoft Access) and what struck me was that the code looked a lot like standard ADO.NET code, although there are a few differences. It seems variables are declared with a dollar ($) sign and there are no semicolons (being a C# person that was a surprise). With the sample I started creating my own sample that connected to a SQL Server 2008 Express database:
As you can see from the code parameters are passed with a param(…) and class instances are created with the new-object keyword. If you type a variable it will be printed as is the case with the $tbl variable.
I know there is a whole lot more that can be done, but I was surprised with how easily I could connect to a database.