如何在 PowerShell 中创建自定义类型以供脚本使用?

我希望能够在我的一些 PowerShell 脚本中定义和使用自定义类型。例如,假设我需要一个具有以下结构的对象:

Contact
{
string First
string Last
string Phone
}

我该如何创建这个函数,以便我可以像下面这样使用它:

function PrintContact
{
param( [Contact]$contact )
"Customer Name is " + $contact.First + " " + $contact.Last
"Customer Phone is " + $contact.Phone
}

这样的事情是可能的,甚至在 PowerShell 中推荐吗?

99391 次浏览

There is the concept of PSObject and Add-Member that you could use.

$contact = New-Object PSObject


$contact | Add-Member -memberType NoteProperty -name "First" -value "John"
$contact | Add-Member -memberType NoteProperty -name "Last" -value "Doe"
$contact | Add-Member -memberType NoteProperty -name "Phone" -value "123-4567"

This outputs like:

[8] » $contact


First                                       Last                                       Phone
-----                                       ----                                       -----
John                                        Doe                                        123-4567

The other alternative (that I'm aware of) is to define a type in C#/VB.NET and load that assembly into PowerShell for use directly.

This behavior is definitely encouraged because it allows other scripts or sections of your script work with an actual object.

Creating custom types can be done in PowerShell.
Kirk Munro actually has two great posts that detail the process thoroughly.

The book Windows PowerShell In Action by Manning also has a code sample for creating a domain specific language to create custom types. The book is excellent all around, so I really recommend it.

If you are just looking for a quick way to do the above, you could create a function to create the custom object like

function New-Person()
{
param ($FirstName, $LastName, $Phone)


$person = new-object PSObject


$person | add-member -type NoteProperty -Name First -Value $FirstName
$person | add-member -type NoteProperty -Name Last -Value $LastName
$person | add-member -type NoteProperty -Name Phone -Value $Phone


return $person
}

This is the shortcut method:

$myPerson = "" | Select-Object First,Last,Phone

Prior to PowerShell 3

PowerShell's Extensible Type System didn't originally let you create concrete types you can test against the way you did in your parameter. If you don't need that test, you're fine with any of the other methods mentioned above.

If you want an actual type that you can cast to or type-check with, as in your example script ... it cannot be done without writing it in C# or VB.net and compiling. In PowerShell 2, you can use the "Add-Type" command to do it quite simmple:

add-type @"
public struct contact {
public string First;
public string Last;
public string Phone;
}
"@

Historical Note: In PowerShell 1 it was even harder. You had to manually use CodeDom, there is a very old function new-struct script on PoshCode.org which will help. Your example becomes:

New-Struct Contact @{
First=[string];
Last=[string];
Phone=[string];
}

Using Add-Type or New-Struct will let you actually test the class in your param([Contact]$contact) and make new ones using $contact = new-object Contact and so on...

In PowerShell 3

If you don't need a "real" class that you can cast to, you don't have to use the Add-Member way that Steven and others have demonstrated above.

Since PowerShell 2 you could use the -Property parameter for New-Object:

$Contact = New-Object PSObject -Property @{ First=""; Last=""; Phone="" }

And in PowerShell 3, we got the ability to use the PSCustomObject accelerator to add a TypeName:

[PSCustomObject]@{
PSTypeName = "Contact"
First = $First
Last = $Last
Phone = $Phone
}

You're still only getting a single object, so you should make a New-Contact function to make sure that every object comes out the same, but you can now easily verify a parameter "is" one of those type by decorating a parameter with the PSTypeName attribute:

function PrintContact
{
param( [PSTypeName("Contact")]$contact )
"Customer Name is " + $contact.First + " " + $contact.Last
"Customer Phone is " + $contact.Phone
}

In PowerShell 5

In PowerShell 5 everything changes, and we finally got class and enum as language keywords for defining types (there's no struct but that's ok):

class Contact
{
# Optionally, add attributes to prevent invalid values
[ValidateNotNullOrEmpty()][string]$First
[ValidateNotNullOrEmpty()][string]$Last
[ValidateNotNullOrEmpty()][string]$Phone


# optionally, have a constructor to
# force properties to be set:
Contact($First, $Last, $Phone) {
$this.First = $First
$this.Last = $Last
$this.Phone = $Phone
}
}

We also got a new way to create objects without using New-Object: [Contact]::new() -- in fact, if you kept your class simple and don't define a constructor, you can create objects by casting a hashtable (although without a constructor, there would be no way to enforce that all properties must be set):

class Contact
{
# Optionally, add attributes to prevent invalid values
[ValidateNotNullOrEmpty()][string]$First
[ValidateNotNullOrEmpty()][string]$Last
[ValidateNotNullOrEmpty()][string]$Phone
}


$C = [Contact]@{
First = "Joel"
Last = "Bennett"
}

Steven Murawski's answer is great, however I like the shorter (or rather just the neater select-object instead of using add-member syntax):

function New-Person() {
param ($FirstName, $LastName, $Phone)


$person = new-object PSObject | select-object First, Last, Phone


$person.First = $FirstName
$person.Last = $LastName
$person.Phone = $Phone


return $person
}

Here is the hard path to create custom types and store them in a collection.

$Collection = @()


$Object = New-Object -TypeName PSObject
$Object.PsObject.TypeNames.Add('MyCustomType.Contact.Detail')
Add-Member -InputObject $Object -memberType NoteProperty -name "First" -value "John"
Add-Member -InputObject $Object -memberType NoteProperty -name "Last" -value "Doe"
Add-Member -InputObject $Object -memberType NoteProperty -name "Phone" -value "123-4567"
$Collection += $Object


$Object = New-Object -TypeName PSObject
$Object.PsObject.TypeNames.Add('MyCustomType.Contact.Detail')
Add-Member -InputObject $Object -memberType NoteProperty -name "First" -value "Jeanne"
Add-Member -InputObject $Object -memberType NoteProperty -name "Last" -value "Doe"
Add-Member -InputObject $Object -memberType NoteProperty -name "Phone" -value "765-4321"
$Collection += $Object


Write-Ouput -InputObject $Collection

Surprised no one mentioned this simple option (vs 3 or later) for creating custom objects:

[PSCustomObject]@{
First = $First
Last = $Last
Phone = $Phone
}

The type will be PSCustomObject, not an actual custom type though. But it is probably the easiest way to create a custom object.

Here's one more option, which uses a similar idea to the PSTypeName solution mentioned by Jaykul (and thus also requires PSv3 or above).

Example

  1. Create a TypeName.Types.ps1xml file defining your type. E.g. Person.Types.ps1xml:
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>StackOverflow.Example.Person</Name>
<Members>
<ScriptMethod>
<Name>Initialize</Name>
<Script>
Param (
[Parameter(Mandatory = $true)]
[string]$GivenName
,
[Parameter(Mandatory = $true)]
[string]$Surname
)
$this | Add-Member -MemberType 'NoteProperty' -Name 'GivenName' -Value $GivenName
$this | Add-Member -MemberType 'NoteProperty' -Name 'Surname' -Value $Surname
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>SetGivenName</Name>
<Script>
Param (
[Parameter(Mandatory = $true)]
[string]$GivenName
)
$this | Add-Member -MemberType 'NoteProperty' -Name 'GivenName' -Value $GivenName -Force
</Script>
</ScriptMethod>
<ScriptProperty>
<Name>FullName</Name>
<GetScriptBlock>'{0} {1}' -f $this.GivenName, $this.Surname</GetScriptBlock>
</ScriptProperty>
<!-- include properties under here if we don't want them to be visible by default
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
</Members>
</MemberSet>
-->
</Members>
</Type>
</Types>
  1. Import your type: Update-TypeData -AppendPath .\Person.Types.ps1xml
  2. Create an object of your custom type: $p = [PSCustomType]@{PSTypeName='StackOverflow.Example.Person'}
  3. Initialise your type using the script method you defined in the XML: $p.Initialize('Anne', 'Droid')
  4. Look at it; you'll see all properties defined: $p | Format-Table -AutoSize
  5. Type calling a mutator to update a property's value: $p.SetGivenName('Dan')
  6. Look at it again to see the updated value: $p | Format-Table -AutoSize

Explanation

  • The PS1XML file allows you to define custom properties on types.
  • It is not restricted to .net types as the documentation implies; so you can put what you like in '/Types/Type/Name' any object created with a matching 'PSTypeName' will inherit the members defined for this type.
  • Members added through PS1XML or Add-Member are restricted to NoteProperty, AliasProperty, ScriptProperty, CodeProperty, ScriptMethod, and CodeMethod (or PropertySet/MemberSet; though those are subject to the same restrictions). All of these properties are read only.
  • By defining a ScriptMethod we can cheat the above restriction. E.g. We can define a method (e.g. Initialize) which creates new properties, setting their values for us; thus ensuring our object has all the properties we need for our other scripts to work.
  • We can use this same trick to allow the properties to be updatable (albeit via method rather than direct assignment), as shown in the example's SetGivenName.

This approach isn't ideal for all scenarios; but is useful for adding class-like behaviors to custom types / can be used in conjunction with other methods mentioned in the other answers. E.g. in the real world I'd probably only define the FullName property in the PS1XML, then use a function to create the object with the required values, like so:

More Info

Take a look at the documentation, or the OOTB type file Get-Content $PSHome\types.ps1xml for inspiration.

# have something like this defined in my script so we only try to import the definition once.
# the surrounding if statement may be useful if we're dot sourcing the script in an existing
# session / running in ISE / something like that
if (!(Get-TypeData 'StackOverflow.Example.Person')) {
Update-TypeData '.\Person.Types.ps1xml'
}


# have a function to create my objects with all required parameters
# creating them from the hash table means they're PROPERties; i.e. updatable without calling a
# setter method (note: recall I said above that in this scenario I'd remove their definition
# from the PS1XML)
function New-SOPerson {
[CmdletBinding()]
[OutputType('StackOverflow.Example.Person')]
Param (
[Parameter(Mandatory)]
[string]$GivenName
,
[Parameter(Mandatory)]
[string]$Surname
)
([PSCustomObject][Ordered]@{
PSTypeName = 'StackOverflow.Example.Person'
GivenName = $GivenName
Surname = $Surname
})
}


# then use my new function to generate the new object
$p = New-SOPerson -GivenName 'Simon' -Surname 'Borg'


# and thanks to the type magic... FullName exists :)
Write-Information "$($p.FullName) was created successfully!" -InformationAction Continue