Get-PowerShell Andy Schneider's Blog on Windows PowerShell and other random thoughts http://get-powershell.com/ http://www.rssboard.org/rss-specification BlogEngine.NET 3.1.1.0 en-US http://get-powershell.com/opml.axd http://www.dotnetblogengine.net/syndication.axd My name Get-PowerShell 0.000000 0.000000 Using Session State Variables in a Module <p>I just had the opportunity to watch a great Pluralsight course called <a href="http://www.pluralsight.com/courses/powershell-cmdlet-development-csharp">PowerShell Cmdlet Development in C# - The Ins and Outs</a>.  One feature that Nathan went over was how to use session state in a Cmdlet.</p> <p>Here is how session state can help you. His example was a Cmdlet called “Invoke-SQLQuery.” As you can imagine, this cmdlet would likely have several parameters, such as “Query”, “Server”, and “Database.” He had several more as well in his course but they are not relevant for this discussion. </p> <p>As a user of this cmdlet, chances are you are going to be running queries against the same server and database multiple times. It is really annoying to have to type in  values for –server and –database all the time. As an end user of the cmdlet, one way to solve this is using PowerShell’s $PSDefaultParameterValues variable.  But as the author of the cmdlet, we can help the user out as well. In fact, we don’t even need to use C# to get this functionality in our own Script Cmdlets. </p> <p>If you have written advanced functions, you have probably come across the [CmdletBinding] attribute that you can use to decorate your function. Most of the time, this is used to support “ShouldProcess” to give users the –confirm and –whatif options using the automatic variable PSCmdlet. </p> <p>Well, it turns out there is a lot more available to us in the $PSCmdlet variable.   Just looking at intellisense, we can see a bunch of properties and methods in there.</p> <p><a href="http://get-powershell.com/image.axd?picture=image_54.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px" border="0" alt="image" src="http://get-powershell.com/image.axd?picture=image_thumb_54.png" width="345" height="201"></a> </p> <p>Highlighted in the screenshot, you can see SessionState. If we continue to dig into this object, we find that it has a property called PSVariable, which is another object. There are several methods on this object that we can use to set and get values. <p> <p>  <p>This allows us to set a value of a variable for as long as the session is available. When the session is gone, so is the variable and its value. We can write a bit of code so that once you use it the first time and specify the Server, you wouldn’t have to type it in the second time. I also threw in a Try / Catch to ensure that if there is no session state variable and the user doesn’t specify a server, than we let the user know that they need to specify the server parameter. We cannot use the “Mandatory” attribute on the [Parameter()] attribute because we would get prompted every time we want to use the cached session state variable. <script src="https://gist.github.com/AndyPowerShell/e544c2ae75d97da3bace.js"></script> http://get-powershell.com/post/2015/06/08/Using-Session-State-Variables-in-a-Module.aspx http://get-powershell.com/post/2015/06/08/Using-Session-State-Variables-in-a-Module.aspx#comment http://get-powershell.com/post.aspx?id=16c63b99-f131-4eaa-bffc-dcdf9341e196 Mon, 08 Jun 2015 14:03:17 +0000 PowerShell SessionState admin http://get-powershell.com/pingback.axd http://get-powershell.com/post.aspx?id=16c63b99-f131-4eaa-bffc-dcdf9341e196 0 http://get-powershell.com/trackback.axd?id=16c63b99-f131-4eaa-bffc-dcdf9341e196 http://get-powershell.com/post/2015/06/08/Using-Session-State-Variables-in-a-Module.aspx#comment http://get-powershell.com/syndication.axd?post=16c63b99-f131-4eaa-bffc-dcdf9341e196 TabExpansion++ Completers for AD Users and Groups <p>There is a lot of great documentation out there on using <a href="https://github.com/lzybkr/TabExpansionPlusPlus">TabExpansion++.</a>  If you haven’t started using it yet, I suggest you give it a whirl.</p> <p>I wrote a couple quick Auto Completers that you can use as a starting point for AD Users and Groups.</p><script src="https://gist.github.com/AndyPowerShell/b275091e1705f95d171e.js"></script> http://get-powershell.com/post/2015/01/10/tabexpanstabexpansion-completers-for-ad-users-and-groupsion-completers-for-ad-users-and-groups.aspx http://get-powershell.com/post/2015/01/10/tabexpanstabexpansion-completers-for-ad-users-and-groupsion-completers-for-ad-users-and-groups.aspx#comment http://get-powershell.com/post.aspx?id=f031c763-2085-4782-8cd7-d886e4438cc8 Sat, 10 Jan 2015 13:01:00 +0000 Admin http://get-powershell.com/pingback.axd http://get-powershell.com/post.aspx?id=f031c763-2085-4782-8cd7-d886e4438cc8 0 http://get-powershell.com/trackback.axd?id=f031c763-2085-4782-8cd7-d886e4438cc8 http://get-powershell.com/post/2015/01/10/tabexpanstabexpansion-completers-for-ad-users-and-groupsion-completers-for-ad-users-and-groups.aspx#comment http://get-powershell.com/syndication.axd?post=f031c763-2085-4782-8cd7-d886e4438cc8 Adding Aliases to your Functions with an Attribute <pre class="brush: ps;">Function Test-Something { [Alias("ts")] [CmdletBinding(SupportsShouldProcess)] param( [Parameter(ValueFromPipeline)] $What ) Process { if ($pscmdlet.ShouldProcess($What)) { } } # End Process } # En </pre> <p> </p> <p>I picked this tip up on <a href="http://www.youtube.com/watch?v=tKyAVm7bXcQ&list=PLfeA8kIs7Coehjg9cB6foPjBojLHYQGb_&index=15">one of the videos from the PS Summit in Europe</a>.  If I paste this into a PS session, I will get the alias automatically. #learnSomethingNewEveryDay</p> http://get-powershell.com/post/2014/10/16/Adding-Aliases-to-your-Functions-with-an-Attribute.aspx http://get-powershell.com/post/2014/10/16/Adding-Aliases-to-your-Functions-with-an-Attribute.aspx#comment http://get-powershell.com/post.aspx?id=fb318982-0a59-46b6-a688-c5a0d07f940a Thu, 16 Oct 2014 02:31:00 +0000 Admin http://get-powershell.com/pingback.axd http://get-powershell.com/post.aspx?id=fb318982-0a59-46b6-a688-c5a0d07f940a 0 http://get-powershell.com/trackback.axd?id=fb318982-0a59-46b6-a688-c5a0d07f940a http://get-powershell.com/post/2014/10/16/Adding-Aliases-to-your-Functions-with-an-Attribute.aspx#comment http://get-powershell.com/syndication.axd?post=fb318982-0a59-46b6-a688-c5a0d07f940a