
close
close
Chance to win $250 in Petri 2023 Audience Survey
Today’s article is yet another guide to get you thinking about working with objects in PowerShell. To illustrate, here’s a question I came across in a PowerShell forum.
How would I write a PowerShell script, which takes an amount, adds 15% for example, and displays the amount, the 15% addition, and the total?
I really don’t have any idea of the need for such a tool, but that’s okay. Instead, let’s use this as a learning opportunity.
In reading this question, I get the sense the user is looking for ways to present text values. Back in the day we would have used a VBScript file like this:
dim Amount,Interest,Increase,Total Amount = 100 Interest = 15 Increase = (Interest/100)*Amount Total = Amount+Increase wscript.echo "Amount = " & Amount wscript.echo "Interest = " & Interest wscript.echo "Increase = " & Increase wscript.echo "Total = " & Total
Running this would give us a simple result:
A VBScript solution (Image Credit: Jeff Hicks)
$amount = 100 $interest= .15
Notice that in this example the variable interest is expressed as a decimal equivalent. I can use Select-Object with custom hashtables to write an object to the pipeline with all of the relevant information.
$amount | select @{Name="Value";Expression={$_}}, @{Name="Interest";Expression={$interest*100}}, @{Name="Increase";Expression = {$_ * $interest}}, @{Name="Total";Expression={ $_ + ($_ * $interest) }}
The Expression scriptblock for each entry performs some type of calculation.
An object result (Image Credit: Jeff Hicks)
$amount = 100 $interest = .15
For the sake of clarity, I’ll create additional variables for the increase and new total.
$increase = $amount * $interest $newAmount = $amount + $increase
Finally, I can create a new object. There are several techniques, but I’ll use the [pscustomobject] to keep it simple.
[pscustomObject]@{ Amount = $amount Percent = $interest * 100 Increase = $Increase Total = $newAmount }
The result is the same:
PSCustomObject result (Image Credit: Jeff Hicks)
Function Invoke-Increase { Param([double]$Amount,[double]$Percent) #convert the percent into the correct value $increase = $amount * ($percent/100) #create an object [pscustomObject]@{ Amount = $amount Percent = $percent Increase = $Increase Total = $amount + $increase } } #end function
I added parameters so I can specify the amount and percent increase, what I had been referring to as “interest.” I cast the variables as the type [double], so that I could pass values like 123.98.
Testing the function (Image Credit: Jeff Hicks)
Function Invoke-Increase { Param([double]$Amount,[double]$Percent,[int]$Decimal = 2) #convert the percent into the correct value $increase = $amount * ($percent/100) #create an object [pscustomObject]@{ Amount = $amount Percent = $percent Increase = [math]::Round($Increase,$Decimal) Total = [math]::Round($amount + $increase,$Decimal) } } #end function
Using the revised function (Image Credit: Jeff Hicks)
More in PowerShell
Most popular on petri