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: PowerShell for EPiServer – cheat sheet – Part 1. In all cases where it made sense I?ve converted the samples to establish them in EPiServer scenarios.

How to Write Conditional Statements

To write an If statement use code similar to this:

$page = Get-CurrentPage;
$changedBy = $page.ChangedBy;
$me = [EPiServer.Security.PrincipalInfo]::Current;
$myName = $me.Name;

if ($changedBy -eq "")
  { "Unspecified author - a system page?" }
elseif ($changedBy -eq $myName)
  { "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-CurrentPage;
switch ($page.PageChildOrderRule) {
    0 {"Undefined sort order. "}
    1 {"Most recently created page will be first in list"}
    2 {"Oldest created page will be first in list"}
    3 {"Sorted alphabetical on name"}
    4 {"Sorted on page index"}
    5 {"Most recently changed page will be first in list"}
    6 {"Sort on ranking, only supported by special controls"}
    7 {"Oldest published page will be first in list"}
    8 {"Most recently published page will be first in list"}
    default {"No idea what that means!"}
  }

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:

foreach ($i in Get-ChildItem .)
{ $i.PageTypeName }

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:

$parent = Get-CurrentPage;
$myPage = [EPiServer.DataFactory]::Instance.GetDefaultPageData(
              $parent.PageLink, "[AlloyTech] Standard page");
$myPage.PageName = "My New Page";
$myPage.URLSegment = [EPiServer.Web.UrlSegment]::CreateUrlSegment($myPage);
Save-Page($myPage);

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

$page = Get-CurrentPage;
$urlBuilder = New-Object '
                   -type EPiServer.UrlBuilder 
                   -argumentlist $page.LinkURL;
[EPiServer.Global]::UrlRewriteProvider.ConvertToExternal(
    $urlBuilder, $page.PageLink, [System.Text.UTF8Encoding]::UTF8);
$currentPath = $urlBuilder.Path

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-CurrentPage | Select-Object PageName, PageTypeName

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 PageName | 
    Select-Object PageName, PageTypeName

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

Get-ChildItem -recurse | 
    Sort-Object PageSaved -descending | 
    Select-Object PageName, PageSaved, ChangedBy

You can even sort by multiple properties:

Get-ChildItem -recurse | 
    Sort-Object PageTypeName, ChangedBy | 
    Select-Object PageTypeName, ChangedBy, PageName

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]

Get it while it?s hot ? still includes 3 bonus script collections!

How to install?

Extract the DLL form the ZIP file into the BIN folder of your web application to install the plugin. Remove the DLL to uninstall it.

All feedback appreciated!

In lieu of the regular disclaimer:

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 Tuesday, May 10th, 2011 at 10:08 am and is filed under .Net Framework, ASP.NET, C#, Code Samples, Downloadable, EPiServer, PowerShell, 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.