Saturday, May 24, 2014

PowerShell Objects - New-Module Cmdlet Pt. 2

If you remember Last we talked we had an Object with 4 Public NoteProperties, 1 Public ScriptMethod, and 1 private member. Now if we look at our ScriptMethod it returns a string unless -AsBool is passed, this can cause issues, like hindering ISE's ability to display Intellisense for the method / function because we are unable to decorate our function with the [OutputType] attribute.

To correct this and make the code much more standardized i want to expose the IsPublished variable since it contains the boolean value indicating if the BlogPost has been published. Also notice that the value of the IsPublished value is being controlled by the object and that is important because we don't want someone or something else to come and set the value to false after we have successfully published the article to the web. So we need to set this Variable to be Read-Only, There are a couple of different ways to do this in PowerShell. First way is use the Set-Variable Cmdlet like so.

Lets run this code again and update the object that $BlogPostInfo contains a reference to and pipe it to Get-Member.

IsPublished is now listed as a Public NoteProperty and you can see we have retrieved its value, but what about setting it.

OK we see it's current value is set to $false and we receive and error when we try to set it's value with a simple assignment statement. So we are good to go, right, maybe lets see. first modify Get-PublishingStatus function so it has and empty param block and add the [OtutputType([System.String])] attribute between the Param block and the [CmdletBinding(PositionalBinding)] attribute, this lets ISE and other PS Editors and Tools what to expect a call to the function to return. Modify the Process block, remove the check for $AsBool and its surrounding context, leaving just a check to $this.IsPublished and returning the appropriate string value. The $this variable is a powershell automatic variable that references the current object, it is only available inside object body or a scriptblock. now we add a function definition for Publish-BlogPost that returns a bool on successful publishing, it also sets the IsPublished NoteProperty to $true since we are publishing the Post. Add the new function to the list of functions exported by the object.

Now we rerun the code to update our $BlogPostInfo variable with a reference to our newly modified object.

OK as we can see IsPublished is $false then we call Publish-BlogPost() which appears to finish successfully but when we check IsPublished  again it is still $false, after looking a the $Error variable we see that an error happened when trying to update IsPublished Value it failed because it is ReadOnly which means that the value assigned to it cannot change after assignment, so that is not what we want. So like I said there are a couple of ways to get the desired behavior and interestingly enough they both involve using the same Cmdlet Add-Member. Keeping the code we have now and changing the Set-Variable call to a Add-Member call.

Running and Inspecting this code we find it works. IsPublished is now updated when the Publish-BlogPost method is called.

Awesome we have a ReadOnly NoteProperty, well no we don't because we did not update the member IsPublished we overwrote it's definition and replaced it with a new version that did not include the ReadOnly option so now the variable is free to be updated by any object, process, etc. Also another problem is that anytime we want to change its value we have to overwrite its implementation in memory and when overwriting (overriding) the member when need to define it exactly the same each time or run into issues of scoping and accessibility. Remember i said there was another way using add-member and it is to change the formal definition of the IsPublished member. I removed the current variable altogether. I pipe the object to Add-Member and add a script property that contains only a get scriptblock that returns $False and the modified the Publish-BlogPost Script Method to Call add-member and add the same script property but this tme it returns $True.

Next Time will will talk of further uses of the New-Module Cmdlet pertaining to Object creation.