Article Directory :: Computers & Technology Articles

Powershell How-To - Part 1

By Rajib Bahar

Subscribe to Rajib Bahar's RSS feed using any feed reader!

Republish: EasyPublish
Published: 22Dec2008
Word count: 644
Viewed: 414 time(s)
Bookmark this article using any bookmark manager!
Get Free Content For Your Site

I was excited hear about powershell feature in SQL server 2008. Initially, it was not clear to me what it was, yet, I was enthused about learning something new. I imagined power shell would involve something in the command line. What happens after that was a complete mystery to me. Anyways, I managed to spend some extra time to learn about the powershell technology. I am going to split this article into multiple series. In the 1st part, I will cover how to work with variables and various helpful commandlets.

Powershell is an extended shell that allows us to manipulate objects and do various scripting tasks. It takes the adavantage of .NET. We will often run into CmdLet in powershell. A commandlet is a .NET object that exposes functionalities for us. For example, there is a cmdlet called "New-Variable" which can be used to declare variables.

To start running powershell type in "sqlps" in command line prompt. If you do not know what cmdlets are available for use then simply type in "get-alias". This handy little cmdlet can show you the shorthand for the available cmdlet. This will get you started so you can start exploring cmdlets. There is another cmdlet named "get-command" that lists all available cmdlets.

PS SQLSERVER:> get-alias

CommandType Name Definition ----------- ---- ---------- Alias ac Add-Content Alias asnp Add-PSSnapin Alias clc Clear-Content ...

When I started working with powershell, I struggled to find documentation on cmdlet. I looked in books online and it did not have much at that time. Only some references were there related to cmd-lets. Anyways, there is no need to despair. A cmdlet called "Get-Help" that can help you pull cmdlet related documentation from the command line. You need to supply the name of the cmdlet as the argument. Other useful arguments available for most cmdlets include "-detailed", "-full". Those flags allows you to bring additional information on the parameter. Amongst the, the "-full" flag is the most comprehensive. Also, if you type in "Get-Help *" then you can see a list of all available help topics. Powershell documentation reminded me of the manpages in unix computer and the RTM mantra.

Strings, variables, objects:

Declaring and working with variables is very simple. It can be achieved via cmdlets such as "New-Variable", "Set-Variable". Also, we can declare it like one would do it in scripting languages like perl, php.

PS SQLSERVER:> $rnd = New-Object "Random" PS SQLSERVER:> $rnd.Next() 778277127 PS SQLSERVER:> $str = "Me" PS SQLSERVER:> $i = 1

In the output above, we declared a variable namely $rnd using the Random class in .NET framework. The next line invoked the Next() method to generate the random number. The New-Object cmdlet can help us instantiate any .NET framework classes. However, keep it in mind that we have access to a subset of .NET framework classes. We can refer to any class that SMO can refer to. That's the only constraint I found here. In addition to building reference type variables, we can also create primitive types such as string and integer. The example above $str, $i does that respectively.

How do I know which variable is of which type? It's fair question due to the fact that there are no enforcement of type declaration. We simply call the method GetType() on the variable. See the output below.

PS SQLSERVER:> $i.GetType()

IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType

There are special variables in powershell. Here is a list of them and what they do:

1. $_ - Contains the current object in the pipeline, used in script blocks, filters, and the where statement. 2. $Args - Contains an array of the parameters passed to a function from command line. 3. $Error - the object containing error information. 4. $Home - user's home directory. 5. $PsHome - Windows PowerShell installation directory.

This is all for the 1st part.

Bookmark this article using any bookmark manager! Subscribe to Rajib Bahar's RSS feed using any feed reader!

EasyPublish™ this article - publishers click here

More articles by Rajib Bahar

Free Report!
Ten Essential Secrets Of Article Marketing ... Grab Your Free
Copy
Now:




We respect your privacy.


Need Content?
Regular Top Quality Content for your Blog, Ezine or Website ...
Delivered Direct,
For Free!

Click For Details



Arts & Entertainment
Automotive
Business - General
Computers & Technology
Finance & Investment
Food & Drink
Health & Fitness
Home & Family
Internet Marketing/Online Business
Legal
Pets & Animals
Politics & Government
Reference & Education
Religion & Faith
Self-Improvement/Motivation
Social
Sports & Recreation
Travel & Leisure
Writing & Speaking

More computing articles:

  • How To Use Multiple Static IPs On A Cloud Server (Dirik Hameed)
    Cloud Servers are the new type of must-have servers that demand less management and maintenance and are the choice of server for many companies and businesses.

  • How To Discuss The Variety Of S-a-a-S I.T Solutions (Dirik Hameed)
    Look through a variety of functional software and cloud solutions at Managed Service Expert

  • Discussing How Much Should You Pay For Domains? (Dirik Hameed)
    Having a website for your company these days is pretty much crucial in order to attract new business and keep up with your competitors. However, domain registration can sometimes prove to be costly, so there are certian factors that need to be taken into consideration when deciding on how much you are willing to pay.

  • Reasons Companies Have To Glance At IP Transit Solutions For The Time Ahead (Dirik Hameed)
    Securing IP transit with your bandwidth provider is likely to become more common in the future

  • Which Communication Tool Helps Make Globalization Easy (Stacy Robinson)
    An International Sim card is one of the major communication tools that are making globalization easy. Using this Sim card, one can maintain their communication as if they were still in their home countries, using their home networks. The calling rates are also cheaper using the international sim card for international calls than with the ordinary Sim cards.

We Automatically Distribute Articles
To Thousands Of Publishers And Web Sites:

Submit Article
All content is viewed and used by you at your own risk and we do not warrant the accuracy or reliability of any of the information. The views expressed are those of the individual contributing authors and not necessarily those of this web site, or its owner, Takanomi Limited.
 
Copyright © 2012 Takanomi Ltd. Company no. 5629683. All rights reserved. | Privacy | Legal | Contact Information