20

我使用 Powershell 的自定义对象命令来保存数据点。自定义对象只创建一个对象并为其分配一个变量。Powershell 可以更进一步,创建可以用来制作对象的新类吗?

在下面的示例中,我存储了三段数据:服务器名称、时间戳和服务器上发生事件后的分钟数。

在学习Powershell的时候,我把这些都放到了一个二维数组中:

$record = @("Server","Timestamp","Minutes")
for ($j = 0; $j -lt 10; $j++){
    $record += @("Server1","$(get-date)",$j)
    sleep 60
    }
$record | export-csv -path c:\record.csv -no type information

export-csv 不能很好地处理数组,所以我开始使用自定义对象:

$record = @()
for ($j = 0; $j -lt 10; $j++){
    $r = New-Object -TypeName PSObject
    $r | Add-Member -MemberType NoteProperty -Name Server -Value ""
    $r | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
    $r | Add-Member -MemberType NoteProperty -Name Minutes -Value ""
    $r.server = "Server1"
    $r.timestamp = "$(get-date)"
    $r.minutes = "$j"
    $record += $r
    sleep 60
    }
$record | export-csv -path c:\record.csv -no type information

这是正确的导出,并且处理对象属性比处理二维数组中的列更容易。

但是如果我想创建几个不在数组中的自定义对象,我必须一遍又一遍地编写自定义对象代码。

$server1 = New-Object -TypeName PSObject
$server1 | Add-Member -MemberType NoteProperty -Name Server -Value ""
$server1 | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
$server2 = New-Object -TypeName PSObject
$server2 | Add-Member -MemberType NoteProperty -Name Server -Value ""
$server2 | Add-Member -MemberType NoteProperty -Name Timesteamp -Value ""
#ad nauseum

如果 Powershell除了自定义对象之外还可以设计自定义怎么办?像OO编程语言一样吗?就像是:

class record {
    -MemberType NoteProperty -Name Server -Value ""
    -MemberType NoteProperty -Name Timestamp -Value ""
    -MemberType NoteProperty -Name Minutes -Value ""
    }
$server1 = new-object -TypeName record
$server2 = new-object -TypeName record
$server3 = new-object -TypeName record

这在Powershell中可能吗?

4

4 回答 4

38

您可以在 PowerShell 中定义类。

Add-Type -Language CSharp @"
public class Record{
    public System.DateTime TimeStamp;
    public string Server;
    public int Minutes;
}
"@;
$MyRecord = new-object Record;
$MyRecord.Server = "myserver";
$MyRecord.Timestamp = Get-Date;
$MyRecord.Minutes = 15;
于 2013-09-09T19:46:39.213 回答
19

可以将函数用作自定义对象的虚假构造函数。您永远不必复制您的代码,并且您可以使用标志从函数调用中直接设置您的属性。这是一个例子:

Function New-Constructor
{
    param
    (
        [string]$Name,
        [DateTime]$TimeStamp = (Get-Date)
    )

    $server = New-Object -TypeName PSObject
    $server | Add-Member -MemberType NoteProperty -Name Server -Value $Name
    $server | Add-Member -MemberType NoteProperty -Name TimeStamp -Value $TimeStamp

    # Calling "server" below outputs it, acting as a "return" value
    $server
}

还有一些示例输出:

PS C:\> New-Constructor -Name "MyServer"

Server                                                      TimeStamp
------                                                      ---------
MyServer                                                    9/9/2013 3:27:47 PM


PS C:\> $myServer = New-Constructor -Name "MyServer"
PS C:\> $myServer

Server                                                      TimeStamp
------                                                      ---------
MyServer                                                    9/9/2013 3:27:57 PM


PS C:\> $newServer = New-Constructor -Name "NS" -TimeStamp (Get-Date).AddDays(-1)
PS C:\> $newServer

Server                                                      TimeStamp
------                                                      ---------
NS                                                          9/8/2013 3:33:00 PM

您可以使用超出此问题范围的功能做很多事情。相反,请查看about_functions_advanced

于 2013-09-09T19:30:35.557 回答
1

另外的选择。

特性

您可以将属性消息的“$null”值替换为具有初始值。Prop 对象是键(属性)和值(初始值)的哈希表。

$messageClass = New-Object -TypeName PSObject -Prop @{ message = $null; }

方法

$messageClass | Add-Member -MemberType ScriptMethod -Name "ShowMessage" -Value {

    Try
    {
        Write-Host $this.message    
    }
    Catch
    {
        Throw $_.Exception
    }
}

构造函数

下面的代码描述了一个构造函数。多态性是使用 [Parameter(Mandatory=$false)] 来断言或不提供指定参数来实现的。

function MessageClass {
    param([Parameter(Mandatory=$true)]
          [String]$mandatoryMessage,
          [Parameter(Mandatory=$false)]
          [String]$optionalMessage)

    $messageObj = $messageClass.psobject.copy()

    if ($optionalMessage)
    {
        $messageObj.message = "$mandatoryMessage $optionalMessage!"
    }
    else
    {
        $messageObj.message = "$mandatoryMessage!"
    }

    $messageObj
}

然后可以像这样调用构造函数:

$var1 = 'Hello'
$var2 = 'World'
$example1 = MessageClass -mandatoryMessage $var1
$example2 = MessageClass -mandatoryMessage $var1 -optionalMessage $var2

要显示文本:

$example1.ShowMessage()
$example2.ShowMessage()

结果将是:

你好!

你好世界!

于 2016-06-16T12:55:43.690 回答
0

为了获得最佳性能,我会这样做:

Add-Type -TypeDefinition '
public class recordEntry {
    public string server;
    public System.DateTime timestamp;
    public int minutes;

    public recordEntry(string _server, System.DateTime _timestamp, int _minutes) {
        server = _server;
        timestamp = _timestamp;
        minutes = _minutes;
    }
}'

$record = [System.Collections.ArrayList]@()
$record = foreach ($j in 0..10){
    [recordEntry]::new("Server1", [datetime]::Now, $j)
}
$record | export-csv -path c:\record.csv -NoTypeInformation
于 2020-06-14T10:52:01.563 回答