Most of this post is also based on the Microsoft’s Windows PowerShell Quick Reference however despite the sharing scripting runtimes the nature of the both shells differ considerably as described in the previous post: Sitecore PowerShell Console cheat sheet – Part 1. In all cases where it made sense I’ve converted the samples to establish them in Sitecore scenarios.

How to Write Conditional Statements

To write an If statement use code similar to this:

$page = Get-item .;
$changedBy = $page."__Updated by";

if ($changedBy -eq "")
  { "Unspecified author - a system page?" }
elseif ($changedBy -eq $me)
  { "The page has been last edited by me!" }
else
  { "The page has been last edited by "+ $changedBy }

Instead of writing a series of If statements you can use a Switch statement, which is equivalent to VBScript’s Select Case statement:

$page = Get-Item .;
switch ($page.Language) {
    "en" {"This version is in English"}
    "pl" {"This version is in Polish"}
    "tlh-KX" {"This version is in Klingon?!"}
    default {"No idea what this language is!"}
  }

How to Write For and For Each Loops

To write a For statement use code similar to this:

for ($a = 1; $a -le 10; $a++) {$a}

By comparison, a For Each statement might look like this:

cd master:\content\home
foreach ($i in Get-ChildItem .)
{ write-host $i.TemplateName }

How to Write Do Loops

To write a Do loop use code like the following, replacing the code between the curly braces with the code to be executed on each iteration of the loop. Oh: and replacing the code inside the parentheses with the loop condition:

$a = 1
do {$a; $a++} 
while ($a -lt 10)
 
$a = 1
do {$a; $a++} 
until ($a –gt 10)

How to Use .NET Objects and Classes

To use a .NET Framework class static method enclose the class name in square brackets, then separate the class name and the method using a pair of colons:

$master = [Sitecore.Configuration.Factory]::GetDatabase("master"); 
$homeItem = $master.GetItem("/sitecore/content/home"); 
$sample = $master.Templates["sample/sample item"]; 
$myItem = $homeItem.Add("MyItem", $sample);

To create an object reference to a .NET Framework object use the New-Object cmdlet:

$item = Get-Item "master:\media library\Files\Sample";
$mediaItem = New-Object `
                   -type Sitecore.Data.Items.MediaItem 
                   -argumentlist $item

Note. This is a cursory overview of working with .NET. The two techniques shown here will not necessarily work with all .NET classes.

How to Select Properties

To work with or display specified properties of a collection, pipe the returned results to the Select-Object cmdlet:

Get-Item master:\content\Home | `
  Select-Object DisplayName, TemplateName

How to Sort Data

To sort data returned by Windows PowerShell simply pipe that data to the Sort-Object cmdlet, specifying the property you want to sort by:

Get-ChildItem -recurse | 
    Sort-Object DisplayName | 
    Select-Object DisplayName, TemplateName

You can also add the –descending or -ascending parameters to specify a sort order:

Get-ChildItem -recurse |
    Sort-Object PageSaved -descending | 
    Select-Object DisplayName, __Updated, "__Updated By"

You can even sort by multiple properties:

Get-ChildItem -recurse | 
    Sort-Object TemplateName, "__Updated By" | 
    Select-Object TemplateName, "__Updated By", DisplayName

How to Work with WMI

To get computer information using WMI call the Get-WMIObject cmdlet followed by the class name:

Get-WMIObject Win32_BIOS

If the class you are interested in does not reside in the cimv2 namespace simply include the –namespace parameter:

Get-WMIObject SystemRestore `
    -namespace root\default

To access data on another computer use the –computername parameter:

Get-WMIObject Win32_BIOS `
    –computername atl-ws-01

To limit returned data, use a WQL query and the –query parameter:

Get-WMIObject -query `
    "Select * From Win32_Service `
        Where State = 'Stopped'"

How to “Interrogate” an Object

To get information about the properties and methods of an object retrieve an instance of that object and then “pipe” the object to the Get-Member cmdlet. For example, this command returns the properties and methods available when working with processes:

Get-Item . | Get-Member

More tips coming soon… In the mean time please download the fresh version of the plugin with the latest improvements:

[Download & Enjoy]

All feedback appreciated!

Aperture_ScienceScience isn’t about why, it’s about why not!? You ask: why is so much of our science dangerous?

I say: why not marry safe science if you love it so much. In fact, why not invent a special safety door that won’t hit you in the butt on the way out, because YOU ARE FIRED!

Yes you, box your stuff, out the front door, parking lot, car, goodbye.

Cave Johnson

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Monday, January 23rd, 2012 at 4:04 pm and is filed under .Net Framework, ASP.NET, PowerShell, Sitecore, Software, Software Development, Solution, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.