
close
close
I’m having fun with this series and hope you are as well. In my previous article, I introduced you to PowerShell classes and I built a class that I can use with my movie data file. If you are just joining us, you really should take a few minutes to read the previous articles (Making Data Dance with PowerShell & Dancing on the Table with PowerShell), plus part one of this article, Data Transformations with PowerShell Classes, Part 1. Otherwise, let’s pick up where we left off.
First, I’m going to use the same CSV file I’ve been using in the last few articles.
$data = Import-CSV C:\scripts\moviedata.csv
Then I’m going to need my class definition, which I explained in the previous article.
advertisment
Class MyUpcoming { #properties [string]$Title [datetime]$ReleaseDate [string]$Comments [int]$OpensIn [string]$Rating [boolean]$NowPlaying = $False #methods [MyUpcoming]Update() { $this.OpensIn = ($this.ReleaseDate - (Get-Date)).TotalDays if ((Get-Date) -ge $this.ReleaseDate ) { $this.NowPlaying = $True } return $this } #constructor MyUpcoming([string]$Title,[datetime]$ReleaseDate,[string]$Rating,[string]$Comments) { $this.Title = $Title $this.ReleaseDate = $ReleaseDate $this.Rating = $Rating $this.Comments = $Comments $this.Update() } } #close class definition
Now I’m ready to start creating instances of the class using my data. To create an instance of my class, I need to invoke the constructor and pass it parameter values from the data. Here’s a quick proof of concept with a few items from $data.
$data[0..2] | foreach { [myupcoming]::new($_.Title,$_.ReleaseDate,$_.Rating,$_.comment) }
$data[3..5] | foreach { New-Object -TypeName MyUpcoming -ArgumentList $_.Title,$_.ReleaseDate,$_.Rating,$_.comment }
Function New-MyUpcoming { [cmdletbinding()] Param( [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [ValidateNotNullorEmpty()] [string]$Title, [Parameter(Mandatory,ValueFromPipelineByPropertyName)] [ValidateNotNullorEmpty()] [datetime]$ReleaseDate, [Parameter(ValueFromPipelineByPropertyName)] [ValidateSet("G","PG","PG-13","R","NC-17","UR","NR")] [string]$Rating = "PG-13", [Parameter(ValueFromPipelineByPropertyName)] [string]$Comments ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose "[PROCESS] Creating instance for $Title" New-Object -TypeName MyUpcoming -ArgumentList $Title,$ReleaseDate,$Rating,$Comments } #process End { Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" } #end }
There are some added benefits with using a function. One, it makes it easier to use in a pipelined expression since you can define parameters to take pipeline input. You can also set default values, make critical values mandatory and use parameter validation. Note that I am using a validation set for the Rating parameter because I know what the accepted values will be. This means I can catch discrepancies when I pull in external data. Let’s test it out.
advertisment
New-MyUpcoming -Title "The Monad Manifesto" -ReleaseDate "12/1/2016" -Comment "starring Tom Hanks as Jeffrey Snover" -Verbose
Update-TypeData -TypeName MyUpcoming -DefaultDisplayPropertySet "Title","ReleaseDate","Rating","Comments" -Force
Now when I look at the objects in $all I get the default properties.
$all | where {-Not $_.NowPlaying -and $_.releaseDate.year -eq 2016} | sort ReleaseDate
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group