<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Phil Fazio's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/phfazio/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/phfazio/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:48:48 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>WPF and PowerShell Series: Host Applications and PowerShell API</title>
      <link>https://docs.microsoft.com/archive/blogs/phfazio/wpf-and-powershell-series-host-applications-and-powershell-api</link>
      <pubDate>Fri, 01 Feb 2013 12:45:00 GMT</pubDate>
      <dc:creator><![CDATA[Phil Fazio]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/phfazio/2013/02/01/wpf-and-powershell-series-host-applications-and-powershell-api/</guid>
      <description><![CDATA[A great deal has been written about using PowerShell together with WPF, but typically this has meant...]]></description>
      <content:encoded><![CDATA[<p>A great deal has been written about using PowerShell together with WPF, but typically this has meant generating the latter from within the former.&nbsp; Likewise,&nbsp;even though PowerShell 2.0 introduced some incredibly useful APIs to invoke PowerShell commands from within your applications, most examples&nbsp;found online to date&nbsp;are centered around console-based applications.&nbsp;</p>
<p>Much less has been written about using PowerShell interactively from within a WPF application &ndash; a powerful approach that opens up a wealth of possibilities in your development process.&nbsp; This series of blog posts will introduce some key concepts required to develop such an application.</p>
<h1>Part&nbsp;1: Host Applications and PowerShell API</h1>
<p>If you have run a PowerShell script or written a script in the PowerShell ISE (Integrated Scripting Environment), you have already encountered host applications.&nbsp; According to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ee706610(v=vs.85).aspx">MSDN</a>, a host application &ldquo;is any application that loads the Windows PowerShell engine into its process and uses it to perform operations. This could be a graphical user interface (GUI) that runs Windows PowerShell commands to perform an administrative task, or it could be a console application that provides an interactive session for the user.&rdquo;</p>
<p>As you might now suspect, two host applications are provided with your PowerShell installation: the default console-based <strong>PowerShell.exe </strong>and the graphical <strong>PowerShell Integrated Scripting Environment (ISE)</strong>.&nbsp; Whereas&nbsp;PowerShell.exe is your traditional shell environment (for those familiar with <span style="font-family: terminal,monaco;">bash</span> or the Command Prompt), the&nbsp;ISE allows you edit, run and debug scripts in a more IDE-like environment (e.g., Visual Studio, Eclipse).&nbsp; The ISE included with PowerShell version 3.0, in particular, adds such familiar features as <a title="Intellisense" href="https://blogs.msdn.com/b/powershell/archive/2012/06/13/intellisense-in-windows-powershell-ise-3-0.aspx" target="_blank">Intellisense</a>&nbsp;to make script authoring easier than ever.&nbsp;</p>
<table border="0">
<tbody>
<tr>
<td><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/58/00/3857.PowerShellexe.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-58-00/3857.PowerShellexe.png"><img style="width: 200px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/58/00/3857.PowerShellexe.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-58-00/3857.PowerShellexe.png" alt="" border="0" /></a></td>
<td>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/58/00/5722.PowerShellISE.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-58-00/5722.PowerShellISE.png"><img style="width: 200px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/58/00/5722.PowerShellISE.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-58-00/5722.PowerShellISE.png" alt="" border="0" /></a></td>
</tr>
<tr>
<td><strong>Figure 1. </strong>PowerShell.exe</td>
<td>&nbsp;<strong>Figure 2.</strong> PowerShell Integrated Scripting Environment (ISE)</td>
</tr>
</tbody>
</table>
<p>Beginning with PowerShell 2.0, you and I are now able to write our own <strong>custom host</strong> interface,&nbsp;using APIs&nbsp;that let you integrate the PS engine into your own applications.</p>
<p>Implementing a custom host is not for everyone; after all, there are already two mature and fully-featured programs for the vast majority of your scripting needs.&nbsp;&nbsp;But in cases where you wish to extend your application to run PowerShell&nbsp;cmdlets&nbsp;without launching a separate process, or where you wish to&nbsp;add interactive PowerShell power (no pun intended) to your application without needing the overhead of the ISE, this can be a very attractive option.</p>
<h2>The Basics</h2>
<p>Despite our eventual goal of a WPF graphical interface, I will start our journey&nbsp;with the oft-presented console application.&nbsp; In order to use the PowerShell runtime in your project, you must have access to the <strong>System.Management.Automation</strong> namespace.&nbsp; If you are using Visual Studio,&nbsp;make sure to add a reference to the DLL in your project.&nbsp;</p>
<p><span style="background-color: #ffff99;">NOTE:&nbsp;For Windows 7 and Windows 8 users, this DLL is located in <span style="font-family: courier new,courier;">X:\Windows\assembly\GAC_MSIL\System.Management.Automation</span>&nbsp;, where X is the drive on which Windows is installed.</span></p>
<pre style="background: #1e1e1e; color: gainsboro; font-family: Consolas;"><span style="color: #569cd6;">using</span>&nbsp;<span style="color: white;">System</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Management</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Automation</span>;
</pre>
<p>One of the simplest applications using the PowerShell engine does the following:</p>
<ul>
<li>Initialize the PowerShell&nbsp;pipeline&nbsp;in the default host</li>
<li>Execute&nbsp;a predetermined command</li>
<li>Display its output to the console</li>
</ul>
<p>As you might have noticed, I added another new&nbsp;term in my above description: pipeline.&nbsp;The terminology can be overwhelming, so let me summarize briefly before continuing: a <strong>pipeline </strong>holds commands or script&nbsp;to be run by the PowerShell <strong>engine</strong> via a <strong>host application</strong>.</p>
<pre style="background: #1e1e1e; color: gainsboro; font-family: Consolas;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #569cd6;">class</span>&nbsp;<span style="color: #4ec9b0;">Program</span>
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #569cd6;">static</span>&nbsp;<span style="color: #569cd6;">void</span>&nbsp;<span style="color: white;">Main</span>(<span style="color: #569cd6;">string</span>[]&nbsp;<span style="color: white;">args</span>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #569cd6;">var</span>&nbsp;<span style="color: white;">pipeline</span>&nbsp;<span style="color: #b4b4b4;">=</span>&nbsp;<span style="color: #4ec9b0;">PowerShell</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Create</span>();
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: white;">pipeline</span><span style="color: #b4b4b4;">.</span><span style="color: white;">AddCommand</span>(<span style="color: #d69d85;">"Get-Command"</span>);
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #569cd6;">foreach</span>&nbsp;(<span style="color: #4ec9b0;">PSObject</span>&nbsp;<span style="color: white;">result</span>&nbsp;<span style="color: #569cd6;">in</span>&nbsp;<span style="color: white;">pipeline</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Invoke</span>())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #4ec9b0;">Console</span><span style="color: #b4b4b4;">.</span><span style="color: white;">WriteLine</span>(<span style="color: #d69d85;">"{0,-15}{1,-20}"</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: white;">result</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Members</span>[<span style="color: #d69d85;">"CommandType"</span>]<span style="color: #b4b4b4;">.</span><span style="color: white;">Value</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: white;">result</span><span style="color: #b4b4b4;">.</span><span style="color: white;">Members</span>[<span style="color: #d69d85;">"Name"</span>]<span style="color: #b4b4b4;">.</span><span style="color: white;">Value</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}</pre>
<p>Using the <span style="font-family: courier new,courier;">PowerShell.Create()</span>&nbsp;method in the simple&nbsp;application&nbsp;creates a new&nbsp;pipeline that eventually holds our command.&nbsp; When we invoke (i.e., execute) the pipeline's contents with <span style="font-family: courier new,courier;">Invoke()</span>, the console application waits for the pipeline to complete before returning an iterable list of <span style="font-family: courier new,courier;">PSObject</span> objects.&nbsp; These are powerful objects which allow you to access results much as you would in the traditional PowerShell.exe application:</p>
<table border="1" cellpadding="5" align="center">
<tbody>
<tr>
<td><strong>PowerShell.exe</strong></td>
<td><strong>PowerShell API (C#)</strong></td>
</tr>
<tr>
<td><span style="font-family: courier new,courier;">$object.PropertyName</span></td>
<td><span style="font-family: courier new,courier;">object.Members["PropertyName"].Value</span></td>
</tr>
</tbody>
</table>
<p>This program may be extremely limited in function, but by successfully running it, you are officially on the path toward our eventual WPF PowerShell snazzy graphical host!</p>
<p>In <strong>Part 2 </strong>of this series, we will take two further key steps needed to take our application graphical: learn how to <strong>invoke a pipeline asynchronously</strong>, a must for any modern GUI application, and learn about <strong>runspaces</strong>.&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Hello (World)</title>
      <link>https://docs.microsoft.com/archive/blogs/phfazio/hello-world</link>
      <pubDate>Thu, 31 Jan 2013 20:48:00 GMT</pubDate>
      <dc:creator><![CDATA[Phil Fazio]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/phfazio/2013/01/31/hello-world/</guid>
      <description><![CDATA[Welcome, to those that have stumbled across this blog one way or another.
As an associate...]]></description>
      <content:encoded><![CDATA[<p class="scroll">Welcome, to those that have stumbled across this blog one way or another.</p>
<p class="scroll">As an associate consultant working for Microsoft Consulting Services, I have the privilege of working with a wide range of technologies and products on a daily basis. That makes it exceptionally difficult to describe what this blog is and what it may be in the future &ndash; consider it an evolving concept, one that I have the time and wherewithal to maintain.</p>
<p class="scroll">Being grateful for the helpful advice I seek and find every day, it only feels right that I try to share some of the tips, tricks and stories that I feel others may find useful. Rarely, if ever, will I consider my ideas to be the &ldquo;right way&rdquo; to accomplish a task, but my hope is that they may illuminate your own path.</p>
<p class="scroll">The opinions expressed here are my own and do not represent Microsoft, and any content posted here comes "AS IS" &ndash; no guarantees, no warranties. That said, please enjoy this little corner of the Internet and feel free to leave your comments and feedback!</p>
<p>Phil</p>]]></content:encoded>
    </item>
  </channel>
</rss>