PowerShell  Web Part  - Template Demos

Click on any of the templates below to see the full source for each template. Then just copy and paste the template into the source editor of the PowerShellWebPart running on your server.

PowerShellWebPart: Command Prompt

PowerShell Command Prompt

This example uses PowerShell's Invoke-Expression cmdlet to build a PowerShell command prompt inside SharePoint.

View Source
Template: PowerShellWebPart: Command Prompt
<div style="background-color:DarkBlue; color:white; height:400px; width:650px">
<form method="GET" action="" enctype="multipart/form-data">
  PS>  <input type=text name="query" size="40" value="<%=$request['query']%>"  />
  <input type=submit value="Execute" />
</form>

<% 
  if ($request['query'] -ne $null -and $request['query'] -ne "") {
%>

<pre>
<% invoke-expression $request['query'] %>
</pre>
<%
}
%>
</div>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: EventLog

Application EventLog

This example uses the Get-EventLog cmdlet to retrieve the newest 50 event messages from the Application Event Log. Each event is piped to a ForEach-Object block that outputs selected information in a table.

View Source
Template: PowerShellWebPart: EventLog
<table border=1 width="98%">
<tr>
            <th style="background-color:LightGrey">Index</th>                               
            <th style="background-color:LightGrey">Time Generated</th>                         
            <th style="background-color:LightGrey">Entry Time</th>                             
            <th style="background-color:LightGrey">Source</th>                                 
            <th style="background-color:LightGrey">Instance ID</th>                            
            <th style="background-color:LightGrey">Message</th>      
</tr>     

<%
Get-EventLog -logName Application -Newest 50 | %{
%>

<tr>
<td><%= $_.Index %></td>
<td><%= $_.TimeGenerated %></td>
<td><%= $_.EntryType %></td>
<td><%= $_.Source %></td>
<td><%= $_.InstanceID %></td>
<td><%= $_.Message %></td>
</tr>

<%
}
%>
</table>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: Get-Process

Process Viewer

Display information about each process running on the sytem.

View Source
Template: PowerShellWebPart: Get-Process
<table border=1 width="98%">
<tr>
<th style="background-color:LightGrey">Name</th>
<th style="background-color:LightGrey">Id</th>
<th style="background-color:LightGrey">Handles</th>
<th style="background-color:LightGrey">Working Set</th>
</tr>     

<%
gps | %{
%>

<tr>
<td><%= $_.ProcessName %></td>
<td><%= $_.Id %></td>
<td><%= $_.Handles %></td>
<td><%= $_.WS %></td>
</tr>

<%
}
%>

</table>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: Recent Documents

Recent Documents

Displays a list of the five most recent documents in Shared Documents. This example shows usage of PowerShellWebPart's $spcontext object (Microsoft.SharePoint.SPContext).

View Source
Template: PowerShellWebPart: Recent Documents
<table width="95%" cellspacing="2px">
<%
   $docsCount = 0;
   $docList = $spcontext.Web.Lists['Shared Documents'];
   $docList.Items
   | sort @{ expression={$_['Modified']} } -descending
   | select -first 5 | %{
%>
<tr>
  <td><a href="<%=$_.URL%>"><%=$_['Name']%></a></td>
  <td><%=$_['Modified']%></td>
</tr>
<% } %>
</table>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: High Priority Tasks

High Priority Tasks

This example uses PowerShellWebPart's $spcontext object (Microsoft.SharePoint.SPContext) to retrieve and display the highest priority items from the Tasks list.

View Source
Template: PowerShellWebPart: High Priority Tasks
<style type='text/css'>
  .tasklist {
    font-family: Trebuchet MS,Arial,helvetica;
  }
  .task {
    margin-bottom: 0.7em;
  }
  .tasktitle, .tasktitle_imp {
    font-size: 1.5em;
    font-weight: bold;
    margin-bottom: 2px;
  }
  .tasktitle {
    color: #2040A0;
  }
  .tasktitle_imp {
    color: #A02020;
  }
  .assignedto {
    font-size: 0.9em;
    color: black;
  }
  .duedate {
    font-size: 0.9em;
    color: #606060;
  }
</style>

<%
function get-assigned($task) {
   $assigned = $task['Assigned To'];
   if ( [string]::IsNullOrEmpty($assigned) ) {
      return ""
   } else {
      return $assigned.SubString($assigned.IndexOf('#')+1)
   }
}

function get-shortdate($date) {
  if ( $date -ne $null ) {
    return ([datetime]$date).ToShortDateString()
  }
}

function get-taskimp($task) {
  if ( $task['Priority'].Contains('1') ) {
    return 'tasktitle_imp'
  } else {
    return 'tasktitle'
  }
}
%>

<div class='tasklist'>
<%
   $taskList = $spcontext.Web.Lists['Tasks'];
   $taskList.Items | sort @{ expression={$_['Priority']} } | select -first 5 | %{
%>
  <div class='task'>
      <div class='<%=get-taskimp $task%>'><%=$task['Title']%></div>
      <span class='assignedto'><%=get-assigned($task)%></span>&nbsp;&nbsp;
      <span class='duedate'><%=get-shortdate($task['Due Date'])%></span>
   </div>
<% } %>
</div>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: Twitter Search

Twitter Search

This example uses PowerShell to retrieve a list of search results from Twitter and displays select information about each tweet in an HTML table. The template shows the use of the $request object to retrieve submitted form variables.

View Source
Template: PowerShellWebPart: Twitter Search
<form method="POST" action="" enctype="multipart/form-data">
Query:  <input type=text name="query" size="40" value="<%=$request['query']%>"  />
<input type=submit value="Search" />
</form>

<!-- This powershell function gets the latest tweets that match the search query -->
<%
function Get-Tweets { 
 $query = $request['query'];
 if ($query -eq $null) { $query = "PowerShell" };
 $WebClient=new-object System.Net.WebClient;
 $results=[xml]($webClient.DownloadString("http://search.twitter.com/search.rss?q=$query"));
 $results.rss.channel.item;
} 

$tweets = Get-Tweets;
%>

<table>
<%

  $tweets | %{
%>
  <tr>
  <td><a href="<%=$_.link%>"><img src="<%= $_.image_link %>" border="0"></a></td>
  <td>
    <table>
      <tr style="background-color:LightGrey">
      <td><a href="<%=$_.link%>"><%= $_.author%></a></td>
      </tr>
      <tr>
      <td colspan="2"><%= $_.description%></td>
      </tr>
      <tr>
      <td style="background-color:WhiteSmoke"><%= $_.pubDate%></td>
      </tr>
    </table>
  </td>
  </tr>
<%
  } #end foreach

%>
</table>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->
PowerShellWebPart: SPSite

SPSite Browser

Displays information about each Site on a SharePoint Server.

View Source
Template: PowerShellWebPart: SPSite
<table border=1 width="98%" style="margin-left:10px; line-height:1.2em; empty-cells:show">
<tr>
<th style="background-color:LightGrey">Title</th>
<th style="background-color:LightGrey">Desc</th>
<th style="background-color:LightGrey">Author</th>
<th style="background-color:LightGrey">Date Created</th>
<th style="background-color:LightGrey">Date LastModified</th>
<th style="background-color:LightGrey">URL</th>
</tr>    

<%
$sp=new-object Microsoft.SharePoint.SPSite('http://' + $request.servervariables["SERVER_NAME"] + ":" + $request.servervariables["SERVER_PORT"]);
$sp.AllWebs | %{
%>

<tr>
<td><%= $_.Title %></td>
<td><%= $_.Description%></td>
<td><%= $_.Author %></td>
<td><%= $_.Created %></td>
<td><%= $_.LastItemModifiedDate %></td>
<td><%= $_.Url%></td>
</tr>

<%
}
%>

</table>
<!--- generated by PowerShellToys PowerShellWebPart - www.powershelltoys.com --->