PowerShell

The Answer to Your PowerShell Question: Working with Objects

question-mark-aspect-hero

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.

Sponsored Content

Maximize Value from Microsoft Defender

In this ebook, you’ll learn why Red Canary’s platform and expertise bring you the highest possible value from your Microsoft Defender for Endpoint investment, deployment, or migration.

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)
A VBScript solution (Image Credit: Jeff Hicks)

Clearly that is not what we want in PowerShell, although it would be easy to powershell-ize the VBScript, which I’ve seen some people do. Let’s start with an equivalent set of PowerShell variables.

$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)
An object result (Image Credit: Jeff Hicks)

I suppose I should point out that because $Amount is a number, it doesn’t have any properties. So I created one called value, which uses the object itself ($_).

While Select-Object technically is creating a custom object behind the scenes, you can create the object yourself, which might be a little easier to follow. Again, I start with some initial values:

$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)
PSCustomObject result (Image Credit: Jeff Hicks)

Now that I know to present the data the way I want it, I can build a simple function to make it re-usable.

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)
Testing the function (Image Credit: Jeff Hicks)

I now have a re-usable tool that writes an object to the pipeline that contains the necessary information.

One thing that always comes up when I show things like this is someone will ask about formatting the decimal places. OK. Add that to your function. Here’s a revised version that uses the Round() method from the [Math] class that rounds the specified number of decimal places. I set a default value of 2.

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)
Using the revised function (Image Credit: Jeff Hicks)

This should be enough to get you started and realizing that PowerShell is the answer, especially when you stop thinking about text output. Of course, there are some alternative techniques you can use but I think I’ve run out of time for today so check back later for the wrap-up to this mini-series.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

 
Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: