PowerShell Primer Share Point 2010

PowerShell is a powerful scripting environment that leverages the flexibility of the .NET Framework to allow command line users to develop scripts and utilities that can automate administrative tasks. Unlike many command line tools, PowerShell has been designed to deal with objects rather than plain text output. Most command line tools are effectively executables and as such can read only text-based input from the command line and return only text-based output to the console. PowerShell introduces the concept of a cmdlet, Power Shell- specific commands that are derived from the System. Management. Automation. Cmdlet class and are created using the .NET Framework. PowerShell uses an object pipeline to pipe the output of one cmdlet to the next cmdlet in a chain so that objects can be passed between functions simply.

Many cmdlets are available for use with PowerShell, and users are free to create their own cmdlets using tools such as Visual Studio. To make it easier for you to manage cmdlets, they are commonly packaged together as snap-ins. Snap-ins usually contain all of the cmdlets for managing particular products or services. For example, the Microsoft. SharePoint.PowerShell snap-in contains the out-of-the-box snap-ins for SharePoint 2010.

When using legacy command line tools, you may find it difficult to remember the names of the various tools. No standard is used for naming or passing parameters. For PowerShell cmdlets, a verb-noun naming convention has been adopted, which makes it easier for users to guess the name of a command. By using the get-command cmdlets, you can get a list of the commands that are available. This command also accepts -verb or –noun as parameters for filtering the output. For example, you could enter the following command to retrieve a list of commands relating to the SPWeb object:

get-command -noun spweb

As well as a standard naming convention for cmdlets, PowerShell also imposes a standard convention for passing parameters. Parameters are always preceded with a hyphen. You can see this in the preceding example. You can view help for a particular cmdlet by passing the parameter -?.

One really useful feature of the PowerShell command line interface is the ability to use tab expansion. As developers, we’ve become used to having tools such as IntelliSense to remind us of our options as we enter code. The same idea works with PowerShell: When entering a command name, if we enter part of the name and then repeatedly press the tab key, we can cycle through the available commands matching our input. When entering parameters for a command, if we enter the preceding hyphen we can also cycle through the list of available parameters. These two features, combined with a standard naming convention, make learning PowerShell scripting a relatively straightforward process.

Using Objects

As mentioned, PowerShell deals with objects rather than text. This means that you can often set or query properties or execute methods on the object that is returned by a particular cmdlet. For example, the following command returns an SPWeb object :

Get-SPWeb -Identity http://localhost

If we execute this command, we’ll find that a URL is returned. This is the default output when no specific property has been called.

We can get a list of the available members for the resultant SPWeb object by passing the output of this command to the get-member command using the pipe character, as follows:

Get-SPWeb -Identity http://localhost|get-member

After we’ve found the property in which we’re interested, we can either retrieve the value by placing parenthesis around the command or by assigning the output of the command to a variable and then querying the variable:

(Get-SPWeb –Identity http://localhost).Title or $web=Get-SPWeb -Identity http://localhost $web.Title

PowerShell variables are always prefixed with $ and persist for the duration of a session. We can examine the type of a variable by using the GetType method as shown:

$web.GetType()

If we need to view more than one property from an object or collection of objects, you can use the Select-Object cmdlet to specify the properties that you require:

(Get-SPFarm).Service|Select-Object -Property TypeName,Status

This command will return a list of services on a farm along with their statuses. Here’s another way to write the same command:

(Get-SPFarm).Service|select TypeName,Status

This shortened version of the Select-Object command uses a technique known as aliasing. Many commonly used commands have simpler aliases, and a full list can be retrieved using the following command:

Get-Alias

As well as being able to specify which properties are shown when displaying a collection of objects, we can also filter which objects appear in the collection by using the Where-Object cmdlet. Again, this cmdlet has an alias: Where. Let’s consider the properties that are available for this cmdlet. Table shows the comparison operators.

Table:Comparison Operators

Table:Comparison Operators

Table:Logical Operators

Table:Logical Operators

As well as comparison operators, we can combine comparison by using the logical operators shown in Table. Using these two techniques, we can create queries such as this:

(Get-SPFarm).Services|Where {$_.TypeName -Like "*data*"}|Select TypeName, Status

Note the use of the $_ variable. This is a system-defined variable that evaluates to the current object in the object pipeline—in other words, the output of the preceding command. When the preceding command returns an enumerable collection, the where command will iterate through the collection; therefore, $_ will evaluate to an instance of an object in the collection rather than the entire collection.

Using Functions

As well as being able to execute command chains and use variables, we can also define functions using PowerShell. These functions work similarly to those in any other programming language—the only minor difference is that all uncaptured output within a function is returned to the caller. For example, we can create the following simple function:

function addNumbers($first,$second) {"Adding numbers" return $first + $second }

We can call this function by entering the command (note the method of passing named parameters):

addNumbers -first 1 -second 2

Here’s the resultant output:

Adding numbers 3

This is expected. However, what isn’t expected is that if we examine the data type of the return value by piping the output to Get-Member, we find two return types, string and int32. If we want to use our function in a chain, this is not ideal. The reason this has happened is that the Adding Numbers message is uncaptured output—that is, it isn’t assigned to a variable or passed to a cmdlet—and as a result it forms part of the output. We can prevent this from occurring by modifying the function as follows:

function addNumbers($first,$second) { Write-Host "Adding numbers" return $first + $second }

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Share Point 2010 Topics