Quantcast
Channel: Tech Blog is Tech » primer
Viewing all articles
Browse latest Browse all 2

PowerShell Object Tutorial

$
0
0

Windows PowershellCreating objects in PowerShell is… strange, to put it lightly–down right awful if you want to be realistic. But even if the syntax is terrible, objects are still nice to use. Here’s a quick primer on how to create and use an object in PowerShell.

Creating a New Object

First let’s create an object using New-Object.

PS > $myObj = New-Object Object

Add a Property

Add a new property to the object by using Add-Member and specifying the member type NoteProperty.

PS > Add-Member -MemberType NoteProperty -InputObject $myObj -Name MyName -Value "Sean"
PS > $myObj.MyName
Sean

Add a Method

Add a new method to the object by using Add-Member and specifying the member type ScriptMethod. The value should be a script block, so put your code in curly braces.

When calling the method, don’t forget to use parentheses. When calling cmdlets in PowerShell you don’t use parentheses, so it’s easy to forget to do so here.

PS > Add-Member -MemberType ScriptMethod -InputObject $myObj -Name PrintName -Value { Write-Host "My name is" $this.MyName }
PS > $myObj.PrintName()
My name is Sean

Add a Method that Accepts Arguments

You can use a param statement to allow your method to accept arguments.

Don’t forget to follow each statement with a semicolon since we’re defining our method on a single line.

When passing the arguments you’ll need to comma-delimit them; again, this is in contrast to the space-delimited arguments you pass to cmdlets.

PS > Add-Member -MemberType ScriptMethod -InputObject $myObj -Name PrintNameAgeAndInterest -Value { param($age, $interest); Write-Host "My name is" $this.MyName "and I am $age years old. I like $interest!" }
PS > $myObj.PrintNameAgeAndInterest(23, 'programming')
My name is Sean and I am 23 years old. I like programming!

Creating Large Methods

You don’t have to create methods all on one line if you don’t want to. If you’re building a script–and you probably are–make sufficient use of white space to make your code easier to read. I personally use the ` (backtick) to continue my methods on the next line for readability. Here’s an example.

Add-Member -MemberType ScriptMethod -InputObject $myObj -Name Print1ThroughX -Value `
{
	param($x)
	for($i = 1; $i -le $x; $i++)
	{
		Write-Host $i
	}
}

And look, it works!

PS > $myObj.Print1ThroughX(10)
1
2
3
4
5
6
7
8
9
10

Overwriting Methods

What if you define a method, but you make a mistake?

PS > Add-Member -MemberType ScriptMethod -InputObject $myObj -Name OutputName -Value { Write-Hst "My name is" $this.MyName }
PS > $myObj.OutputName()
Exception calling "OutputName" with "0" argument(s): "The term 'Write-Hst' is not recognized as the name of a cmdlet,
 function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that t
he path is correct and try again."
At line:1 char:18
+ $myObj.OutputName <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ScriptMethodRuntimeException

Oh no! I misspelled Write-Host. No problem! Just use the -Force attribute to overwrite the method with the corrected version.

PS > Add-Member -Force -MemberType ScriptMethod -InputObject $myObj -Name OutputName -Value { Write-Host "My name is" $this.MyName }
PS > $myObj.OutputName()
My name is Sean

Have fun using objects in PowerShell!


Viewing all articles
Browse latest Browse all 2

Trending Articles