<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cognifide Blog &#187; EPiServer</title>
	<atom:link href="http://www.cognifide.com/blogs/episerver/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cognifide.com/blogs</link>
	<description>The Cognifide.Com Tech Hub</description>
	<lastBuildDate>Mon, 17 Jun 2013 08:46:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>PowerShell for EPiServer – cheat sheet – Part 2</title>
		<link>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-2/</link>
		<comments>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-2/#comments</comments>
		<pubDate>Tue, 10 May 2011 12:40:53 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[cheat sheet]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://4.5</guid>
		<description><![CDATA[&#160; 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 &#8211; cheat sheet &#8211; Part 1. In all cases where it made sense I’ve converted the samples [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>Most of this post is also based on the Microsoft’s <a title="Windows PowerShell Quick Reference" href="http://www.microsoft.com/downloads/en/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&amp;displaylang=en" target="_blank">Windows PowerShell Quick Reference</a> however despite the sharing scripting runtimes the nature of the both shells differ considerably as described in the previous post: <a href="http://blog.najmanowicz.com/2011/05/09/powershell-for-episerver-cheat-sheet-part-1/" target="_blank">PowerShell for EPiServer &#8211; cheat sheet &#8211; Part 1</a>. In all cases where it made sense I’ve converted the samples to establish them in EPiServer scenarios.</p>
<h2>How to Write Conditional Statements</h2>
<p>To write an If statement use code similar to this:</p>
<pre>$page = Get-CurrentPage;
$changedBy = $page.ChangedBy;
$me = [EPiServer.Security.PrincipalInfo]::Current;
$myName = $me.Name;

if ($changedBy -eq &quot;&quot;)
  { &quot;Unspecified author - a system page?&quot; }
elseif ($changedBy -eq $myName)
  { &quot;The page has been last edited by me!&quot; }
else
  { &quot;The page has been last edited by &quot;+ $changedBy }</pre>
<p>Instead of writing a series of If statements you can use a Switch statement, which is equivalent to VBScript’s Select Case statement:</p>
<p><span id="more-105"></span></p>
<pre>$page = Get-CurrentPage;
switch ($page.PageChildOrderRule) {
    0 {&quot;Undefined sort order. &quot;}
    1 {&quot;Most recently created page will be first in list&quot;}
    2 {&quot;Oldest created page will be first in list&quot;}
    3 {&quot;Sorted alphabetical on name&quot;}
    4 {&quot;Sorted on page index&quot;}
    5 {&quot;Most recently changed page will be first in list&quot;}
    6 {&quot;Sort on ranking, only supported by special controls&quot;}
    7 {&quot;Oldest published page will be first in list&quot;}
    8 {&quot;Most recently published page will be first in list&quot;}
    default {&quot;No idea what that means!&quot;}
  }</pre>
<h2>How to Write For and For Each Loops</h2>
<p><span></span></p>
<h2></h2>
<p>To write a For statement use code similar to this:</p>
<pre>for ($a = 1; $a -le 10; $a++) {$a}</pre>
<p>By comparison, a For Each statement might look like this:</p>
<pre>foreach ($i in Get-ChildItem .)
{ $i.PageTypeName }</pre>
<h2>How to Write Do Loops</h2>
<p>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:</p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td valign="top" width="47%">
<pre>$a = 1
do {$a; $a++}
<b>while</b> ($a -lt 10)</pre>
</td>
<td valign="top" width="6%">&#160;</td>
<td valign="top" width="47%">
<pre>$a = 1
do {$a; $a++}
<b>until</b> ($a –gt 10)</pre>
</td>
</tr>
</tbody>
</table>
<h2>How to Use .NET Objects and Classes</h2>
<p>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: </p>
<pre>$parent = Get-CurrentPage;
$myPage = <b>[EPiServer.DataFactory]::Instance</b>.GetDefaultPageData(
              $parent.PageLink, &quot;[AlloyTech] Standard page&quot;);
$myPage.PageName = &quot;My New Page&quot;;
$myPage.URLSegment = <b>[EPiServer.Web.UrlSegment]::CreateUrlSegment($myPage);</b>
Save-Page($myPage);</pre>
<p>To create an object reference to a .NET Framework object use the New-Object cmdlet: </p>
<pre>$page = Get-CurrentPage;
<b>$urlBuilder = New-Object '
                   -type EPiServer.UrlBuilder
                   -argumentlist $page.LinkURL;</b>
[EPiServer.Global]::UrlRewriteProvider.ConvertToExternal(
    $urlBuilder, $page.PageLink, [System.Text.UTF8Encoding]::UTF8);
$currentPath = $urlBuilder.Path</pre>
<p><b>Note</b>. This is a cursory overview of working with .NET. The two techniques shown here will not necessarily work with all .NET classes. </p>
<h2>How to Select Properties</h2>
<p>To work with or display specified properties of a collection, pipe the returned results to the Select-Object cmdlet:</p>
<pre>Get-CurrentPage | Select-Object PageName, PageTypeName</pre>
<h2>How to Sort Data</h2>
<p>To sort data returned by Windows PowerShell simply pipe that data to the Sort-Object cmdlet, specifying the property you want to sort by:</p>
<pre>Get-ChildItem -recurse |
    <b>Sort-Object PageName</b> |
    Select-Object PageName, PageTypeName</pre>
<p>You can also add the –descending or -ascending parameters to specify a sort order:</p>
<pre>Get-ChildItem -recurse |
    <b>Sort-Object PageSaved -descending</b> |
    Select-Object PageName, PageSaved, ChangedBy</pre>
<p>You can even sort by multiple properties:</p>
<pre>Get-ChildItem -recurse |
    <b>Sort-Object PageTypeName, ChangedBy</b> |
    Select-Object PageTypeName, ChangedBy, PageName</pre>
<h2>How to Work with WMI</h2>
<p>To get computer information using WMI call the Get-WMIObject cmdlet followed by the class name:</p>
<pre>Get-WMIObject Win32_BIOS</pre>
<p>If the class you are interested in does not reside in the cimv2 namespace simply include the <b>–namespace</b> parameter:</p>
<pre>Get-WMIObject SystemRestore `
    -namespace rootdefault</pre>
<p>To access data on another computer use the <b>–computername</b> parameter:</p>
<pre>Get-WMIObject Win32_BIOS `
    –computername atl-ws-01</pre>
<p>To limit returned data, use a WQL query and the <b>–query</b> parameter:</p>
<pre>Get-WMIObject -query `
    &quot;Select * From Win32_Service `
        Where State = 'Stopped'&quot;</pre>
<h2>How to “Interrogate” an Object</h2>
<p>To get information about the properties and methods of an object retrieve an instance of that object and then “pipe” the object to the <b>Get-Member</b> cmdlet. For example, this command returns the properties and methods available when working with processes: </p>
<pre>Get-Item . | Get-Member</pre>
<p><strong>More tips coming soon… </strong>In the mean time please download the fresh version of the plugin with the latest improvements:</p>
<p align="center"><a title="Download Powershell console for Cheat Sheet 1" href="http://www.najmanowicz.com/blog_bin/Cognifide.EPiserverControls.PowerShell.CheatSheet1.zip" target="_blank">[Download &amp; Enjoy]</a> </p>
<p><strong>Get it while it’s hot – still includes 3 bonus script collections!</strong></p>
<h2>How to install?</h2>
<p>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.</p>
<p>All feedback appreciated!</p>
<p><span style="color: #ff0000"><strong>In lieu of the regular disclaimer</strong>: </span></p>
<blockquote>
<p><font color="#ff0000"><em><img style="border-right-width: 0px;margin: 3px 20px 0px 7px;padding-left: 0px;padding-right: 0px;float: left;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="Aperture_Science" align="left" src="http://blog.najmanowicz.com/wp-content/uploads/2011/05/Aperture_Science_thumb.png" width="150" height="150" />Science isn&#8217;t about why, it&#8217;s about why not!? </p>
<p></em></font><font color="#ff0000"><em>You ask: why is so much of our science dangerous?</p>
<p>I say: why not marry safe science if you love it so much. In fact, why not invent a special safety door that won&#8217;t hit you in the butt on the way out, because YOU ARE FIRED!</em></font></p>
<p><font color="#ff0000"><em>Yes you, box your stuff, out the front door, parking lot, car, goodbye.</p>
<p></em></font></p>
<p align="right"><font color="#ff0000">&#8211; <strong><a href="http://apscience.wikia.com/wiki/Cave_Johnson" target="_blank">Cave Johnson</a> </p>
<p></strong></font></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell for EPiServer – cheat sheet – Part 1</title>
		<link>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-1/</link>
		<comments>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-1/#comments</comments>
		<pubDate>Mon, 09 May 2011 12:47:02 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[cheat sheet]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://4.7</guid>
		<description><![CDATA[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). &#160; Windows PowerShell PowerShell Console for EPiServer Interactive – command can ask for confirmations and can be [...]]]></description>
			<content:encoded><![CDATA[<p>Most of this is based on the Microsoft’s <a title="Windows PowerShell Quick Reference" href="http://www.microsoft.com/downloads/en/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&amp;displaylang=en" target="_blank">Windows PowerShell Quick Reference</a> 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).</p>
<p>&#160;</p>
<table border="1" cellspacing="2" cellpadding="12">
<tbody>
<tr>
<th valign="top" width="50%">Windows PowerShell</th>
<th valign="top" width="50%">PowerShell Console for EPiServer</th>
</tr>
<tr>
<td valign="top"><strong>Interactive</strong> – command can ask for confirmations and can be aborted. User can be solicited to provide input.</td>
<td valign="top"><strong>Batch</strong> – 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.</td>
</tr>
<tr>
<td valign="top">Supports colouring.</td>
<td valign="top">Supports plain text output only.</td>
</tr>
<tr>
<td valign="top">Supports command line arguments for running scripts.</td>
<td valign="top">All arguments are defined directly within the script or derived from context automatically.</td>
</tr>
<tr>
<td valign="top">Can access any file depending on the rights of the user.</td>
<td valign="top">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.</td>
</tr>
</tbody>
</table>
<p>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.</p>
<p><span id="more-106"></span></p>
<p>The content &amp; samples of the original cheat sheet has been adjusted to more closely reflect scenarios usable for an EPiServer admin or developer.</p>
<h1>So here go the EPiServer specific tips</h1>
<p><span></span></p>
<h2>How to get Current Page</h2>
<p>To get the page for the location you’re in you can use either one:</p>
<table border="0" cellspacing="0" cellpadding="0" width="97%">
<tbody>
<tr>
<td valign="top" width="47%">
<pre>Get-Item .</pre>
</td>
<td valign="top" width="6%">&nbsp;</td>
<td valign="top" width="47%">
<pre>Get-CurrentPage</pre>
</td>
</tr>
</tbody>
</table>
<h2>How to get Current Page’s children</h2>
<p>To get the children of the page you’re currently in:</p>
<pre>Get-ChildItem</pre>
<p>
  <br />To get the children of the page you’re currently in and all of its children children (whole branch): </p>
</p>
<pre>Get-ChildItem –recurse</pre>
<h2>How to Modify a page</h2>
<p>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)</p>
<pre>Get-CurrentPage |
ForEach-Object { $_.MainBody = “&lt;p&gt;Hello World&lt;/p&gt;” }</pre>
<p>
  <br />but in case of the POCO properties you need to do part of the work yourself: </p>
</p>
<pre>Get-CurrentPage |
ForEach-Object {
$writable = $_.CreateWritableClone();
$writable.PageName = “Hello World”;
Save-Page($writable);
}</pre>
<h2>How to restart the application</h2>
<p>To restart the application use the following command:</p>
<pre>Restart-Application</pre>
<h2>How to get system properties and environment variables</h2>
<p>Some more important variables are mapped onto the PowerShell variables by the plugin. Notable variables (value depending on your server configuration):</p>
<table border="1" cellspacing="1" cellpadding="5">
<tbody>
<tr>
<td valign="top" width="34%"><strong>Name</strong></td>
<td valign="top" width="65%">Sample value</td>
</tr>
<tr>
<td valign="top" width="34%">$AppPath</td>
<td valign="top" width="65%">C:EPiServerSitesExampleEPiServerSite3</td>
</tr>
<tr>
<td valign="top" width="34%">$AppVPath</td>
<td valign="top" width="65%">/ (app virtual folder)</td>
</tr>
<tr>
<td valign="top" width="34%">$tempPath</td>
<td valign="top" width="65%">C:WindowsTEMP</td>
</tr>
<tr>
<td valign="top" width="34%">$tmpPath</td>
<td valign="top" width="65%">C:WindowsTEMP</td>
</tr>
</tbody>
</table>
<p><strong></strong></p>
<p>Additionally you can list all environment variables by using:</p>
<pre>[System.Environment]::GetEnvironmentVariables()</pre>
<p>
  <br />and get the value of a specific variable with e.g.: </p>
</p>
<pre>[System.Environment]::GetEnvironmentVariable(&quot;ComputerName&quot;)</pre>
<h1>PowerShell Language and environment specific tips</h1>
<h2>How to Insert Comments</h2>
<p>To insert a comment, use the pound sign (#):</p>
<pre># This is a comment, not a line to be run.</pre>
<h2>How to Insert Line Breaks</h2>
<p>To insert a line break into a Windows PowerShell script use the backtick (`) :</p>
<pre>$b = `
&quot;This is a continuation of the line.&quot;</pre>
<p>
  <br />You can also break a line at the pipe separator (|) character (assuming your line uses the pipeline): </p>
</p>
<pre>Get-ChildItem C:Scripts |
Sort-Object Length –Descending</pre>
<h2>How to Create Multi-Command Lines</h2>
<p>To put multiple commands on a single line, separate those commands using a semicolon:</p>
<pre>$a = 1,2,3,4,5; $b = $a[2]; $b</pre>
<p><strong>Hint.</strong> This script writes-out the value of <strong>$b</strong> which is <strong>3</strong></p>
<h2>How to Make Comparisons</h2>
<p>Windows PowerShell cmdlets (like <strong>Where-Object</strong>) use a special set of comparison operators, including those shown in the following table.</p>
<p>Each of these operators can be made case sensitive by adding a <strong>c</strong> immediately after the hyphen. For example, <strong>-ceq </strong>represents the case-sensitive equals operator; <strong>-clt</strong> is the case-sensitive less than operator.</p>
<table border="1" cellspacing="1" cellpadding="5">
<tbody>
<tr>
<td valign="top" width="34%"><strong>-lt</strong></td>
<td valign="top" width="65%">Less than</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-le</strong></td>
<td valign="top" width="65%">Less than or equal to</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-gt</strong></td>
<td valign="top" width="65%">Greater than</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-ge</strong></td>
<td valign="top" width="65%">Greater than or equal to</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-eq</strong></td>
<td valign="top" width="65%">Equal to</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-ne</strong></td>
<td valign="top" width="65%">Not equal to</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-like</strong></td>
<td valign="top" width="65%">Like (uses wildcards for matching)</td>
</tr>
<tr>
<td valign="top" width="34%"><strong>-notlike</strong></td>
<td valign="top" width="65%">Not like (uses wildcards for matching)</td>
</tr>
</tbody>
</table>
<p><strong></strong></p>
<p><strong></strong></p>
<h2>How to Read a Text File</h2>
<p>To read the contents of a text file into a variable, call the <strong>Get-Content</strong> cmdlet followed by the path to the text file:</p>
<pre>$a = Get-Content C:ScriptsTest.txt</pre>
<p>
  <br />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: </p>
</p>
<pre>$a[0]</pre>
<p>
  <br />This command echoes back the <em>last </em>line in $a: </p>
</p>
<pre>$a[-1]</pre>
<p>
  <br /><strong>Bonus</strong>. To determine the number of lines, words, and characters in a text file use this command: </p>
</p>
<pre>Get-Content c:scriptstest.txt |
measure-object -line -word -character</pre>
<h2>How to Write to a Text File</h2>
<p>To save data to a text file use the <strong>Out-File</strong> cmdlet:</p>
<pre>Get-CurrentPage | Out-File C:ScriptsTest.txt</pre>
<p>
  <br />To append data to an existing file, add the –<strong>append</strong> parameter: </p>
</p>
<pre>Get-CurrentPage | Out-File C:ScriptsTest.txt –append</pre>
<p><strong>Attention. </strong>Unlike in the regular Windows PowerShell console you cannot use the MS-DOS redirection characters (&gt; for write, &gt;&gt; for append) when using EPiServer hosted PowerShell.</p>
<p>Another option is to use the <strong>Export-CSV</strong> cmdlet to save data as a comma-separated-values file:</p>
<pre>Get-Process | Export-CSV C:ScriptsTest.txt</pre>
<p><strong>More tips coming soon… </strong>In the mean time please download the fresh version of the plugin with the latest improvements:</p>
<p align="center"><a title="Download Powershell console for Cheat Sheet 1" href="http://www.najmanowicz.com/blog_bin/Cognifide.EPiserverControls.PowerShell.CheatSheet1.zip" target="_blank">[Download &amp; Enjoy]</a><br /><strong>Get it while it’s hot – still includes 3 bonus script collections!</strong></p>
<h2>How to install?</h2>
<p>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.</p>
<p>All feedback appreciated!</p>
<p><span style="color: #ff0000"><strong>Disclaimer – Responsibility</strong>: <em>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. </em></span></p>
<p><span style="color: #ff0000"><strong>Disclaimer – Security</strong>: <em>While the tool requires a membership in either the “WebAdmin” or “Administrators” group, to execute, protect it with a &lt;location&gt; entry in web.config, to add another layer of security especially if your administration UI is exposed to the public. Manage your group memberships &amp; rights responsibly!</em></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cognifide.com/blogs/episerver/powershell-for-episerver-cheat-sheet-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EPiServer Admin Mode PowerShell scripts</title>
		<link>http://www.cognifide.com/blogs/episerver/episerver-admin-mode-powershell-scripts/</link>
		<comments>http://www.cognifide.com/blogs/episerver/episerver-admin-mode-powershell-scripts/#comments</comments>
		<pubDate>Sat, 07 May 2011 12:51:02 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[episerver]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://4.10</guid>
		<description><![CDATA[The PowerShell plugin gets an update once again to support Admin mode script collections in addition to the context scripts. How to write an Admin mode script collections? &#60;ContextScriptCollection&#62; &#60;Title&#62;Statistics Scripts&#60;/Title&#62; &#60;Description&#62;This script collection ... &#60;/Description&#62; &#60;Area&#62;Administration&#60;/Area&#62; &#60;Scripts&#62; &#60;ContextScript&#62; &#60;Title&#62;Restart Application&#60;/Title&#62; &#60;Description&#62;The script restarts this instance of EPiServer...&#60;/Warning&#62; &#60;Script&#62;Restart-Application&#60;/Script&#62; &#60;Icon&#62;/App_Themes/Default/Images/Tools/Refresh.gif&#60;/Icon&#62; &#60;Groups&#62; &#60;/Groups&#62; &#60;/ContextScript&#62; &#60;/Scripts&#62; &#60;/ContextScriptCollection&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>The PowerShell plugin gets an update once again to support Admin mode script collections in addition to the context scripts.</p>
<h2>How to write an Admin mode script collections?</h2>
<pre>&lt;ContextScriptCollection&gt;
  &lt;Title&gt;Statistics Scripts&lt;/Title&gt;
  &lt;Description&gt;This script collection ... &lt;/Description&gt;
  <strong>&lt;Area&gt;Administration&lt;/Area&gt;</strong>

  &lt;Scripts&gt;
    &lt;ContextScript&gt;
      &lt;Title&gt;Restart Application&lt;/Title&gt;
      &lt;Description&gt;The script restarts this instance of EPiServer...&lt;/Warning&gt;
      &lt;Script&gt;Restart-Application&lt;/Script&gt;

      &lt;Icon&gt;/App_Themes/Default/Images/Tools/Refresh.gif&lt;/Icon&gt;
      &lt;Groups&gt;
      &lt;/Groups&gt;
    &lt;/ContextScript&gt;
  &lt;/Scripts&gt;

&lt;/ContextScriptCollection&gt;</pre>
<h2>Where can I access that?</h2>
<p align="center"><a href="http://blog.najmanowicz.com/wp-content/uploads/2011/05/image.png"><img style="border-right-width: 0px;margin: 3px 3px 0px 7px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="image" src="http://blog.najmanowicz.com/wp-content/uploads/2011/05/image_thumb.png" width="244" height="200" /></a></p>
<p><span></span></p>
<p align="center">
<p><span id="more-107"></span></p>
<h2 align="left">Additional changes in this version</h2>
<p>Scripts now have access to 3 starting predefined variables:</p>
<table border="1" cellspacing="1" cellpadding="5" width="5">
<tbody>
<tr>
<td valign="top" width="34%">
<p><b>Name</b></p>
</td>
<td valign="top" width="65%">
<p>Sample value</p>
</td>
</tr>
<tr>
<td valign="top" width="34%">
<p>$AppPath</p>
</td>
<td valign="top" width="65%">
<p>C:EPiServerSitesExampleEPiServerSite3</p>
</td>
</tr>
<tr>
<td valign="top" width="34%">
<p>$AppVPath</p>
</td>
<td valign="top" width="65%">
<p>/ (app virtual folder)</p>
</td>
</tr>
<tr>
<td valign="top" width="34%">
<p>$tempPath</p>
</td>
<td valign="top" width="65%">
<p>C:WindowsTEMP</p>
</td>
</tr>
<tr>
<td valign="top" width="34%">
<p>$tmpPath</p>
</td>
<td valign="top" width="65%">
<p>C:WindowsTEMP</p>
</td>
</tr>
</tbody>
</table>
<p>and 1 more PowerShell Function have been defined: <strong>Reset-Application</strong>.</p>
<p align="center"><a title="Download Powershell console for Cheat Sheet 1" href="http://www.najmanowicz.com/blog_bin/Cognifide.EPiserverControls.PowerShell.AdminMode.zip" target="_blank">[Download &amp; Enjoy]</a></p>
<p align="center"><strong>Includes 3 bonus script collections!</strong></p>
<h2>How to install?</h2>
<p>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.</p>
<p>All feedback appreciated!</p>
<p><font color="#ff0000"><strong>Disclaimer – Responsibility</strong>: <em>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. </em></font></p>
<p><font color="#ff0000"><strong>Disclaimer – Security</strong>: <em>While the tool requires a membership in either the “WebAdmin” or “Administrators” group, to execute, protect it with a &lt;location&gt; entry in web.config, to add another layer of security especially if your administration UI is exposed to the public. Manage your group memberships &amp; rights responsibly!</em></font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cognifide.com/blogs/episerver/episerver-admin-mode-powershell-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Context PowerShell Scripts in EPiServer</title>
		<link>http://www.cognifide.com/blogs/episerver/context-powershell-scripts-in-episerver/</link>
		<comments>http://www.cognifide.com/blogs/episerver/context-powershell-scripts-in-episerver/#comments</comments>
		<pubDate>Wed, 04 May 2011 12:57:48 +0000</pubDate>
		<dc:creator>Adam Najmanowicz</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://4.13</guid>
		<description><![CDATA[Ok, so I’ve got my shot of endorphins writing about PowerShell last week (damn, it’s nice to be able to code again!), and I got pretty determined on making it usable and achieving all the goals I’ve initially envisioned. and in the process build a usable tool and a library of scripts that people can [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so I’ve got my shot of endorphins writing about PowerShell last week (damn, it’s nice to be able to code again!), and I got pretty determined on making it usable and achieving all the goals I’ve initially envisioned. and in the process build a usable tool and a library of scripts that people can use either directly or to modify to meet their needs.</p>
<p>The goal for this week: <strong>Context Scripts</strong></p>
<p>Context scripts are the first step to break the scripting out of the admin realm and into the editor’s space. Those scripts will still be written by admins and developers but the goal is for them to be usable by the authors. The goal for those scripts can be as trivial as e.g. syndicating all the great functionality little plugins like this <a title="Unpublish button by Ted" href="http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/2009/2/Adding-a-custom-plugin-button-to-unpublish-a-page-in-EPiServer/" rel="nofollow" target="_blank">Unpublish button by Ted</a> in one place and then mix and match them to your liking.</p>
<p>Some of the important bits:</p>
<ul>
<li>Context scripts are something that is visible to users on “Scripts” page. </li>
<li>Scripts can be exposed to everyone or just the groups of your liking… you define it in the script. </li>
<li>Scripts are grouped in collections that are defined in *.psepi files that you drop into your application folder </li>
</ul>
<h2>How do I define a script collection?</h2>
<p><span></span></p>
<h2></h2>
<p>Each script collection is defined within a single .psepi file. A sample script can look as follows:</p>
<p><span id="more-108"></span></p>
<pre>

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;ContextScriptCollection&gt;
  &lt;Title&gt;Statistics Scripts&lt;/Title&gt;

  &lt;Description&gt;This is a sample script collection allows for calculating some interesting stats for the branch you're in.&lt;/Description&gt;
  &lt;Scripts&gt;
    &lt;ContextScript&gt;
      &lt;Title&gt;Author Statistics&lt;/Title&gt;
      &lt;Description&gt;Click to Learn more about how many pages under this one were created by which authors.&lt;/Description&gt;

      &lt;Script&gt;get-childitem -recurse | Group-Object  ChangedBy | Sort count -descending | format-table -property count, name&lt;/Script&gt;
        &lt;Icon&gt;/App_Themes/Default/Images/Tools/AddUser.gif&lt;/Icon&gt;
        &lt;Warning&gt;This script only calculates statistics so it does not really need a warning but let’s show it anyway. Do you want to calculate them?&lt;/Warning&gt;
      &lt;Groups&gt;

        &lt;string&gt;WebAdmins&lt;/string&gt;
        &lt;string&gt;Administrators&lt;/string&gt;
      &lt;/Groups&gt;
    &lt;/ContextScript&gt;
  &lt;/Scripts&gt;

&lt;/ContextScriptCollection&gt;

</pre>
<p>What does each of the tags mean?</p>
<ul>
<li><strong>Title</strong> &amp; <strong>Description</strong> on <strong>ContextScriptCollection</strong> are going to appear at the top of the list for the collection – pretty self explanatory. </li>
<li><strong>Scripts</strong> is a container for <strong>ContentScript</strong> tags, it can contain any number of them. </li>
<li>Each <strong>ContentScript</strong> tag represent a single script, where <strong>Title</strong> is what will appear on the button that the user will click to execute the script. </li>
<li><strong>Script</strong> tag contains the script body – this is where you put the code that will get executed </li>
<li><strong>Icon</strong> is a URL to an image that will appear on the button left of the button title. </li>
<li><strong>Warning</strong> tag contains a question that will get presented to the user before the script will be executed. This tag can be omitted in which case no warning will be shown. A rule of thumb would be that you should show a warning before a long running scripts and scripts that modify content so a user can back away from a mistakenly pressed button. </li>
<li><strong>Groups</strong> tag contains the list of groups that will have the right to see the script. if that tag is empty, all users will see the script. If you define at least one group, you need to define all groups that you want to see the button. </li>
</ul>
<h2>What does “Context” in Context scripts mean?</h2>
<p>Basically since we cannot show the editor a console and let them navigate to the page (for various reasons, like complexity and potential destructive powers <img style="border-bottom-style: none;border-left-style: none;border-top-style: none;border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blog.najmanowicz.com/wp-content/uploads/2011/05/wlEmoticon-smile.png" />) The scripting environment will put your script “in the current page” before releasing control to your script, which in essence is equivalent to typing before your script.</p>
<pre>cd MyCMSDrive:
cd /path/to/my/page/</pre>
<p>That means that your script can get access to the page the user is currently on simply by using the “get-item .” command or enumerate its children by use of “get-childitem” etc. in the case of the script above it calculates author statistics for the branch it is executed in. Pretty cool, huh? <img src='http://www.cognifide.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Additionally I’ve realized in the process of duplicating/extending the functionality of <a title="Unpublish button by Ted" href="http://labs.episerver.com/en/Blogs/Ted-Nyberg/Dates/2009/2/Adding-a-custom-plugin-button-to-unpublish-a-page-in-EPiServer/" rel="nofollow" target="_blank">Ted’s plugin</a> that modifying standard EPiServer properties is not necessarily very intuitive, hence I’ve created a few macros to help me with the scripting exercises. While I still need to call “CreateWritableClone” I’ve simplified the saving of a page by defining the “<strong>Save-Page</strong>” function and created a <strong>Get-CurrentPage</strong> as an alias to “get-item .”. So a following script unpublishes the current page:</p>
<pre><b>Get-CurrentPage</b> | foreach-object {
$writable = $_.CreateWritableClone();
$writable.StopPublish = [DateTime]::Now;
Save-Page($writable);
}</pre>
<p>While this one unpublishes all of its children</p>
<pre><b>get-childitem -recurse</b> | foreach-object {
$writable = $_.CreateWritableClone();
$writable.StopPublish = [DateTime]::Now;
Save-Page($writable);
}</pre>
<p>Notice the subtle difference in the script and the major difference in the effect? Pure PowerShell magic!</p>
<p>To be honest I was amused to find out that during the whole process I’ve still not broken the CMS 5 compatibility, but I don’t expect it to stay this way in the future.</p>
<p align="center"><a href="http://blog.najmanowicz.com/wp-content/uploads/2011/05/EPiServer5ContextScript.png"><img style="border-right-width: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="EPiServer5ContextScript" src="http://blog.najmanowicz.com/wp-content/uploads/2011/05/EPiServer5ContextScript_thumb.png" width="244" height="201" /></a>&#160;&#160;&#160; <a href="http://blog.najmanowicz.com/wp-content/uploads/2011/05/EPiServer6ContextScript.png"><img style="border-right-width: 0px;margin: 3px 3px 0px 7px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="EPiServer6ContextScript" src="http://blog.najmanowicz.com/wp-content/uploads/2011/05/EPiServer6ContextScript_thumb.png" width="244" height="204" /></a></p>
<p>Also at this moment you can add and remove the *.psepi files at any moment as the files are parsed on every request, if that proves to be a problem in the future, caching will get introduced. At this moment I don’t find it a problem as it only happens in edit mode.</p>
<p align="center"><a title="Download Powershell console with context script support" href="http://www.najmanowicz.com/blog_bin/Cognifide.EPiserverControls.PowerShell.Context.zip" target="_blank">[Download &amp; Enjoy]</a></p>
<p>Includes 2 bonus script collections!</p>
<h2>How to install?</h2>
<p>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.</p>
<p>All constructive feedback appreciated!</p>
<p><font color="#ff0000"><strong>Disclaimer – Responsibility</strong>: <em>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. </em></font></p>
<p><font color="#ff0000"><strong>Disclaimer – Security</strong>: <em>While the tool requires a membership in either the “WebAdmin” or “Administrators” group, to execute, protect it with a &lt;location&gt; entry in web.config, to add another layer of security especially if your administration UI is exposed to the public. Manage your group memberships &amp; rights responsibly!</em></font></p>
<p>pre.ps1<br />
{<br />
        padding: 4px;<br />
        margin: 4px 0 4px 0;<br />
        overflow: auto;<br />
        background-color: #012456;<br />
        border: thin solid #aaa;<br />
        color:#fff;<br />
        font-weight:900;<br />
        width: 95%;<br />
        border: solid #000 1px;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cognifide.com/blogs/episerver/context-powershell-scripts-in-episerver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
