Wednesday, January 19, 2011

SQL Tips: Powershell Basics for DBAs

We have been discussing about PowerShell in a regular interval. In such a way, this is another tip for you about PowerShell. Using PowerShell is as easy as it presents you with things as drives. If you wish to use C drive, you can use “CD C:” in PowerShell as you do in DOS. Moreover, if you wish to access your registry, you can do it by typing “CD HKLM:”. If you wish to access SQL Server via PowerShell, it is required to type “CD SQLSERVER:”. Now, let’s try connecting to a SQL Server and to a specific DB. If you wish to do so, you can simply type the following command.
>cd SQLServer:\SQL\ServerName\Default\Databases\DBName
As you’ve clearly understood the structure of PowerShell, you can easily switch between servers, databases or any other objects as you’ve to replace the name of the objects and leave the rest the same. Now, let’s have a look about the different drives with you can work. The command we’ve provided is “psdrive” and it is required to type it as follows in the PowerShell Prompt.

If you run this command, you’ll get output as shown below. Moreover, the output will be slightly different depending upon what you’ve loaded on your box and most will be the same. However, I’ve provided some interesting ones.
•    HKCU – HKey_Current_User registry hive.
•    Env – here you can see all the environment variables and values for your box.
•    HKLM – Hkey_Local_Machine registry hive.
•    Variable – here you can see all the built-in and user-defined variables for your active session.
•    Function -- here you can see all the built-in and user-defined functions for your active session.
Another most aspect of PowerShell is cmdlet. It is as same as commands in DOS and is easy to work with. The consistence of cmdlet makes it easier to work with. Some of the examples of cmdlets are get-service, out-file, get-process, format-table and get-content. If you wish to pass a parameter to a cmdlet, simply type “-“ followed by the parameter name and then a space. Check the following examples out.
>get-service –computername Server1

See, no more wondering if you have to use a dash or a slash, and no more wondering if you have to put a space between the parameter or not. They all work the same. Of course, for all string inputs you’re welcome to use double or single quotes if there aren’t any spaces in the string it isn’t necessary. All cmdlets handle errors in the same way too. Each one has a set of common parameters that they all support: debug, errorAction, ErrorVariable, and WarningAction, just to name a few. This means that error handling and debugging are built into every cmdlet so functionality will be the same across the board no matter if you’re working with SQL Server, IIS, Exchange, or anything else. It’s all the same.
In this command, dash and slash work same. Moreover, if you wish you can put a space between the parameter or you can simply avoid it. All of the cmdlets work same in handling errors, debug, error action, error variable and warning action. Therefore, if you’re working in SQL Server, IIS, Exchange or anything else, they’re all the same.

B y