
close
close
In a previous article, we began exploring possible answers to a PowerShell question. If you missed that article, take a moment to get caught up, otherwise this article might be a bit confusing.
The last option for the original problem that I want to explore is to use PowerShell’s Add-Member cmdlet. This is similar to using Select-Object, except it updates the original object. So let’s start again with initial values.
advertisment
$amount = 123.45 $interest = .12
If you pipe $amount to Get-Member, you’ll see that there are no properties. I’ll add one for the value itself.
$amount | Add-Member -MemberType ScriptProperty -Name Value -Value {$this}
I need to use a ScriptProperty, because I must have PowerShell evaluate the object. In this context, this is reflected by $this, as opposed to $_. I can create a static note property for the rate.
$amount | Add-Member -MemberType NoteProperty -Name Percent -Value ($interest *100)
And finally scriptproperties to calculate new values:
$amount | Add-Member -MemberType ScriptProperty -Name Increase -value {$this * ($this.Percent/100)} $amount | Add-Member -MemberType ScriptProperty -Name NewValue -value {$this + $this.Increase}
These new properties are now reflected with Get-Member.
Viewing new properties (Image Credit: Jeff Hicks)
advertisment
$amount | select *
Values from added properties (Image Credit: Jeff Hicks)
Updating the object (Image Credit: Jeff Hicks)
New object discards new properties (Image Credit: Jeff Hicks)
Function Update-Value { [cmdletbinding()] Param([double]$Amount,[double]$Percent) #convert percent back into the decimal format $i = $percent/100 Write-Verbose "Updating $amount with $i" <# PowerShell doesn't seem to like trying to modify an object in use as a parameter so we'll make a copy of it #> $result = $amount $result | Add-Member -MemberType ScriptProperty -Name Value -Value {$this} -force $result | Add-Member -MemberType NoteProperty -Name Percent -Value ($i *100) -force $result | Add-Member -MemberType ScriptProperty -Name Increase -value {$this * ($this.Percent/100)} -Force $result | Add-Member -MemberType ScriptProperty -Name NewValue -value {$this + $this.Increase} -force #write the new object to the pipeline $result }
The functions in this article are very simple with no help, parameter validation, or error handling, primarily because these are more demonstrations than production level tools. But this works.
Testing the Update-Value function (Image Credit: Jeff Hicks)
Viewing type names (Image Credit: Jeff Hicks)
$a.psobject.TypeNames.Insert(0,"My.IncreaseValue")
The type name can be anything you want as long as it is unique. Now look at typenames for $a
Viewing the new type name (Image Credit: Jeff Hicks)
Display the object with the new type name (Image Credit: Jeff Hicks)
Function Update-Value { [cmdletbinding()] Param([double]$Amount,[double]$Percent) #convert percent back into the decimal format $i = $percent/100 Write-Verbose "Updating $amount with $i" <# PowerShell doesn't seem to like trying to modify an object in use as a parameter so we'll make a copy of it #> $result = $amount $result | Add-Member -MemberType ScriptProperty -Name Value -Value {$this} -force $result | Add-Member -MemberType NoteProperty -Name Percent -Value ($i *100) -force $result | Add-Member -MemberType ScriptProperty -Name Increase -value {$this * ($this.Percent/100)} -Force $result | Add-Member -MemberType ScriptProperty -Name NewValue -value {$this + $this.Increase} -force #update typename $result.psobject.typenames.insert(0,"My.IncreaseValue") #write the new object to the pipeline $result }
Testing the revised function (Image Credit: Jeff Hicks)
advertisment
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