blog dedicated to automation of everything from builds, testing, deployments, and releases. Posts focusing mainly on PowerShell and related technologies but also including Application Lifestyle Management and anything code, script, computer related. I love tech and need to share my knowledge with the world.
Thursday, May 29, 2014
Credential IO Script
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.
Tuesday, May 20, 2014
PowerShell Object Creation – New-Module Pt. 1
I have seen quite a few posts on creating custom objects in PowerShell, comparing it to techniques used in languages such as JavaScript but most use some variation of defining an object literal using Select-Object, New-Object, or a HashTable that is cast to PSCustomObject which are all fine and great ways of creating a one off Property Bag. These posts seem skip the most powerful way to define objects in PowerShell and that is to use the New-Module Cmdlet. The New-Module Cmdlet has a switch –AsCustomObject and a parameter –ScriptBlock that allow you to define a custom object. Inside the ScriptBlock every Variable defined is a property and you control whether it’s public or private by using the Export-ModuleMember Cmdlet if you export a variable it is public otherwise it is private. The most powerful part comes in the form that your functions defined inside the module ScriptBlock become the methods and the parameters define the methods arguments for the object and again the visibility is controlled by the Export-ModuleMember Cmdlet. The Functions can be Advanced Functions with Parameter Validation and Comment Based Help. Lets look at some code.
Running this code will provide us with a local variable $BlogPostInfo that we can inspect.
We can see that the variable has the PSCustomObject type and are variable are now NoteProperties and our function is now a ScriptMethod. Also notice that our IsPublished variable is not included in the list because it is private, we did not export it. Next time we will cover some advanced topics such as defining ScriptProperties and method overriding.
$Until_Then = Good-Times | Get-Command
Tuesday, March 29, 2011
New Project: PoshLogger
New on Codeplex @ http://poshlogger.codeplex my project to create a good, easy, unified logger for powerShell scripts, and PowerShell applications. I started with a script that Joel Bennet posted on poshcode.org and next thing you know I had a simple logger that I have been using in my internal projects on a day to day basis. If your looking for a logger jump in and help create a really great Logging Experience in PowerShell. My thanks to Joel for the Idea and thanks to the log4net team for the object model.
Import-Module PoshLogger
$Mylogger = Get-Logger -Name 'Test 123' -ConsoleLog -EventLog -RollingFileLog -Folder ".\Logs"
Set-DefaultLogger -Logger $MyLogger
Push-LogContext -Name "My Context"
Write-Log -Message "Logging This To Many Various Target" -LogLevel 'INFO'
Pop-Location
Silicon Valley Code Camp 2011: PowerShell Types and Objects
I will be speaking once again at SCCC on Powershell. here is the actual Session overview.
PowerShell Types and Objects, Objects and Types. Oh My!
Level: Intermediate | Room: Unknown
A look at the PowerShell Type System (ETS). PowerShell exposes a few primative Types for use but has a powerful dynamic engine called the Extensible Type System. We will discuss Primatives, Type Accelerators, PSObjects, .NET Types, WMI, ASDI, and the difference between a dynamic type and dynamic objects
Monday, March 14, 2011
PowerShell Plus v4.0 Released! Oh Yeah!
Advanced PowerShell development environment
Learn PowerShell fast, code PowerShell faster
- Learn PowerShell fast using the Interactive Learning Center
- Execute PowerShell quickly and accurately with the powerful interactive console
- Take advantage of the PowerShell remoting features directly from the interactive console
- Access hundreds of pre-loaded scripts in the personal QuickClick™ library
- Download thousands of community scripts from PowerShell.com, TechNet and PoshCode – directly from the PowerShell Plus console and editor
- Debug PowerShell 10x faster with the advanced script editor and easily share your scripts with the PowerShell community Code
Saturday, February 19, 2011
Microsoft Agile Guidance: Visual Studio 2010
Agile Principles and Values, by Jeff Sutherland
Agile development is a term that was derived from the Agile Manifesto, which was written in 2001 by a group that included the creators of Scrum, Extreme Programming (XP), Dynamic Systems Development Method (DSDM), and Crystal; a representative of feature-driven development; and several other thought leaders in the software industry. The Agile Manifesto established a common set of overarching values and principles for all of the individual agile methodologies at the time. It details four core values for enabling high-performing teams.
- Individuals and their interactions
- Delivering working software
- Customer collaboration
- Responding to change
These values are not just something the creators of the Agile Manifesto intended to give lip service to and then forget. They are working values. Each individual agile methodology approaches these values in a slightly different way, but all of these methodologies have specific processes and practices that foster one or more of these values.
- respect for the worth of every person
- truth in every communication
- transparency of all data, actions, and decisions
- trust that each person will support the team
- commitment to the team and to the team’s goals
- Process improvement depends on the team to generate a list of impediments or problems in the organization, to face them squarely, and then to systematically eliminate them in priority order.
- Innovation occurs only with the free interchange of conflicting ideas, a phenomenon that was studied and documented by Takeuchi and Nonaka, the godfathers of Scrum.
- Aligning the team toward a common goal requires the team to surface and resolve conflicting agendas.
- Commitment to work together happens only when people agree on common goals and then struggle to improve both personally and as a team.
For more see Microsoft Visual Studio ...