PowerShell v3 PowerShell org Forums and Some New Ideas

Half a story, and, half a post here. The story stems from the announcement of the new website PowerShell.org. As noted in the About,
This site is designed to be a general-purpose gathering place and portal for Windows PowerShell, providing access to Q&A forums, technical articles and blog posts, and other resources. We’re the spiritual successor to PowerShellCommunity.org… just with a simpler look, a tighter focus on answers and education, and an easier-to-type domain name!
The heart of this community is its discussion and Q&A forums, since most people come here looking for answers to specific PowerShell-related problems. Aside from those, we also try to provide pointers to other best-of-breed resources within the broader community. In other words, we’re not going to try and duplicate effort when someone else is already doing a great job someplace else. For example, you’ll find that many of our blog posts here are actually aggregated from elsewhere, bringing together some of the best PowerShell content we could find into one place, while letting those authors continue to do their own thing.
So far I have, I feel a little self-conscious mentioning it, been uber active in the forums. Translation? Im all over it! I think the admins may ban me at some point! Seriously though, I see the forums as a fantastic place to directly interact with some of the brightest PowerShell folks around for free. I mean, seriously, how can it get any better than that? As a part of my continual learning experience I have really jumped straight into the Advanced Scripting sub forum. If you have followed my blog, it will come as no surprise I have really found it exciting. As with any great learning experience, I am being shown things that are making me stop and just gawk with jaw wide open for sure. Yes, I look like a dumbfounded 6 year old.

At any rate, one of the great discoveries from these early posts is the use of New-Member with the -AsCustomType switch Kirk Munro pointed out in this discussion,
Can advanced functions be added to custom objects?
If you are not familiar with Kirk he goes by the handle poshoholic and happens to be one of the foremost experts on PowerShell out there. He previously worked at Quest on a number of key products, and, now works as an independent consultant. The depth and breadth of his learning/experience is enough to make most anyone take pause. My first pause was came with this script,
$userClass = {
   # Use the param block to define the properties for "constructor". The entire script block is
   # the constructor, defining properties and methods on the objects dynamically.
   [CmdletBinding()]
   param(
      [Parameter(Position=0,Mandatory=$true)]
      [ValidateNotNullOrEmpty()]
      [ValidateScript({$_ -eq $_.Trim()})]
      [System.String]
      $FirstName,

      [Parameter(Position=1,Mandatory=$true)]
      [ValidateNotNullOrEmpty()]
      [ValidateScript({$_ -eq $_.Trim()})]
      [System.String]
      $LastName
   )

   # First turn off default exporting so that you can control what is public and what is private.
   Export-ModuleMember

   # Here we define our constructor parameters as read-only public variables
   Set-Variable -Name FirstName -Option ReadOnly
   Set-Variable -Name LastName -Option ReadOnly
   Export-ModuleMember -VariableFirstName,LastName

   # We also need to calculate a read-only public FullName variable based on those values.
   Set-Variable -Name FullName -Option ReadOnly -Value "$FirstName $LastName"
   Export-ModuleMember -Variable FullName

   # Now lets set up an internal private method to do some work for us
   function UpdateEmailAddress {
      [CmdletBinding()]
      param()
      $FirstName.ToLower().SubString(0,1) + ($LastName -replace [^a-z]).ToLower().SubString(0,[System.Math]::Min($LastName.Length,7)) + @poshoholicstudios.com
   }

   # Now we can create a public EmailAddress variable that is defined by using the private function as part of the "constructor".
   Set-Variable -Name EmailAddress -Option ReadOnly -Value (UpdateEmailAddress)
   Export-ModuleMember -Variable EmailAddress

   # But what if they get married and change their last name? We can use a public method for that
   # since the public property is read-only
   function ChangeLastName {
      [CmdletBinding()]
      [OutputType([System.Void])]
      param(
         [Parameter(Position=0,Mandatory=$true)]
         [ValidateNotNullOrEmpty()]
         [ValidateScript({$_ -eq $_.Trim()})]
         [System.String]
         $NewLastName
      )
      Set-Variable -Scope script -Name LastName -Value $NewLastName -Force
      Set-Variable -Scope script -Name EmailAddress -Value (UpdateEmailAddress) -Force
   }
   Export-ModuleMember -Function ChangeLastName
}

# Create our object
$me = New-Module -AsCustomObject -ScriptBlock $userClass -ArgumentList Kirk,Munro

# Look at the properties it has.
$me

# Look at the public properties and methods it has. Note that this approach automatically
# defines NoteProperty and ScriptMethod members. The key difference here is control over
# visibility of the members and methods, plus being able to create read-only properties.
# Note that the UpdateEmailAddress method is not displayed because it is private.
$me | Get-Member

# Now lets try changing FirstName (thats read-only, remember?). This will not work.
$me.FirstName = Poshoholic

# Heres the email address before we change the last name
$me.EmailAddress

# Now we can change the last name.
$me.ChangeLastName(Poshoholic)

# And heres the email address after we change the last name
$me.EmailAddre

Related Posts by Categories

0 comments:

Post a Comment