Most of this is based on the Microsoft’s Windows PowerShell Quick Reference however despite the sharing scripting runtimes the nature of the both shells are pretty different (although the differences are not as vast as one might think).

 

Windows PowerShell PowerShell Console for EPiServer
Interactive – command can ask for confirmations and can be aborted. User can be solicited to provide input. Batch – all commands are being executed in one go, the script has no chance to ask questions, go or no-go decisions have to be solved within the script.
Supports colouring. Supports plain text output only.
Supports command line arguments for running scripts. All arguments are defined directly within the script or derived from context automatically.
Can access any file depending on the rights of the user. Can only access files the web application identity can write to. Cannot access files on user’s machine but rather operates on the server’s file system. Cannot operate with elevated privileges.

That said, I considered that enough of the Reference document is irrelevant in the EPiServer scenario that it’s beneficial for the users of the console to have a bespoke cheat sheet created especially for the purpose of this plugin.

The content & samples of the original cheat sheet has been adjusted to more closely reflect scenarios usable for an EPiServer admin or developer.

So here go the EPiServer specific tips

How to get Current Page

To get the page for the location you’re in you can use either one:

Get-Item .
 
Get-CurrentPage

How to get Current Page’s children

To get the children of the page you’re currently in:

Get-ChildItem


To get the children of the page you’re currently in and all of its children children (whole branch):

Get-ChildItem –recurse

How to Modify a page

the modification depends on whether you’re modifying the Page Template defined properties or the PageData (POCO – C#) properties. In case of the former the modification is scripted into the attached property (which I script within the environment to create the writable clone behind the scenes)

Get-CurrentPage |
ForEach-Object { $_.MainBody = “<p>Hello World</p>” }


but in case of the POCO properties you need to do part of the work yourself:

Get-CurrentPage |
ForEach-Object {
$writable = $_.CreateWritableClone();
$writable.PageName = “Hello World”;
Save-Page($writable);
}

How to restart the application

To restart the application use the following command:

Restart-Application

How to get system properties and environment variables

Some more important variables are mapped onto the PowerShell variables by the plugin. Notable variables (value depending on your server configuration):

Name Sample value
$AppPath C:\EPiServer\Sites\ExampleEPiServerSite3\
$AppVPath / (app virtual folder)
$tempPath C:\Windows\TEMP
$tmpPath C:\Windows\TEMP

Additionally you can list all environment variables by using:

[System.Environment]::GetEnvironmentVariables()


and get the value of a specific variable with e.g.:

[System.Environment]::GetEnvironmentVariable("ComputerName")

PowerShell Language and environment specific tips

How to Insert Comments

To insert a comment, use the pound sign (#):

# This is a comment, not a line to be run.

How to Insert Line Breaks

To insert a line break into a Windows PowerShell script use the backtick (`) :

$b = `
"This is a continuation of the line."


You can also break a line at the pipe separator (|) character (assuming your line uses the pipeline):

Get-ChildItem C:\Scripts |
Sort-Object Length –Descending

How to Create Multi-Command Lines

To put multiple commands on a single line, separate those commands using a semicolon:

$a = 1,2,3,4,5; $b = $a[2]; $b

Hint. This script writes-out the value of $b which is 3

How to Make Comparisons

Windows PowerShell cmdlets (like Where-Object) use a special set of comparison operators, including those shown in the following table.

Each of these operators can be made case sensitive by adding a c immediately after the hyphen. For example, -ceq represents the case-sensitive equals operator; -clt is the case-sensitive less than operator.

-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not equal to
-like Like (uses wildcards for matching)
-notlike Not like (uses wildcards for matching)

How to Read a Text File

To read the contents of a text file into a variable, call the Get-Content cmdlet followed by the path to the text file:

$a = Get-Content C:\Scripts\Test.txt


Each line in the file ends up as an item in the array $a. If you want to access a single line in the file you can simply specify the index number corresponding to that line:

$a[0]


This command echoes back the last line in $a:

$a[-1]


Bonus. To determine the number of lines, words, and characters in a text file use this command:

Get-Content c:\scripts\test.txt |
measure-object -line -word -character

How to Write to a Text File

To save data to a text file use the Out-File cmdlet:

Get-CurrentPage | Out-File C:\Scripts\Test.txt


To append data to an existing file, add the –append parameter:

Get-CurrentPage | Out-File C:\Scripts\Test.txt –append

Attention. Unlike in the regular Windows PowerShell console you cannot use the MS-DOS redirection characters (> for write, >> for append) when using EPiServer hosted PowerShell.

Another option is to use the Export-CSV cmdlet to save data as a comma-separated-values file:

Get-Process | Export-CSV C:\Scripts\Test.txt

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!

Disclaimer – Responsibility: With great powers comes great responsibility – this tool can do a lot of harm to a lot of content in a very short time – backup your system before using the plugin! I will not be held responsible for any use of it. Test your scripts on your development server first! Test on an exact copy of production before running scripts on production content.

Disclaimer – Security: While the tool requires a membership in either the “WebAdmin” or “Administrators” group, to execute, protect it with a <location> entry in web.config, to add another layer of security especially if your administration UI is exposed to the public. Manage your group memberships & rights responsibly!

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



This entry (Permalink) was posted on Monday, May 9th, 2011 at 9:00 am and is filed under .Net Framework, ASP.NET, C#, EPiServer, PowerShell, 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.