5

使用 PowerShell v5 中的新类函数,如果我们可以将方法放入类中,我正在努力解决问题。

我已经尝试了以下方法并玩了一段时间,但没有运气。

class Server {

    [string]$computerName = "192.168.0.200"
    [bool]$ping = (Test-Connection -ComputerName $computerName).count

}

$1 = [server]::new()
$1.computerName = "blah"

我尝试通过设置属性手动输入计算机名称,但后来我假设您在创建对象时需要它

$1 = [server]::new($computerName = "192.168.0.200")

我得到的例外是

[ERROR] Exception calling ".ctor" with "0" argument(s): "Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the 
[ERROR] command again."
[ERROR] At D:\Google Drive\Projects\VSPowerShell\DiscoveryFramework\DiscoveryFramework\DiscoveryFramework\class.ps1:12 char:1
[ERROR] + $1 = [server]::new()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
[ERROR]     + FullyQualifiedErrorId : ParameterBindingValidationException
[ERROR]  
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> 
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> $1
Server
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> $1.gettype()
Server

来自 $error 的完整异常链接位于http://pastebin.com/WtxfYzb5


进一步使用了 $this.prop,但您不能使用自己的参数启动构造函数。

PS path:\> class Server {

    [string]$computerName = "192.168.0.200"
    [bool]$ping = (Test-Connection -ComputerName $this.computerName).count

}

PS path:\> 
PS path:\> $var = [server]::new()

PS path:\> $var

computerName  ping
------------  ----
192.168.0.200 True
4

2 回答 2

4

你需要的是一个构造函数(或多个构造函数),如果你没有在你的类中指定一个构造函数,你得到的唯一构造函数是没有参数的默认构造函数。

总而言之,您希望使用与默认 IP 地址不同的 IP 地址来初始化您的服务器(并允许启动 $ping 的默认值。)

我已经包含了我通常包含在类中的区域,以区分属性、构造函数和方法。

class Server {
    #region class properties
    [string]$computerName = "192.168.0.200"
    [bool]$ping = (Test-Connection -ComputerName $this.computerName).count
    #endregion

    #region class constructors
    Server() {}

    Server([string]$computerName) {
        $this.computerName = $computerName
    }
    #endregion

    #region class methods
    #endregion
}

现在您可以创建一个对象而不向其传递参数:

[1] PS G:\> $1 = [Server]::new()
[2] PS G:\> $1

computerName  ping
------------  ----
192.168.0.200 True



[3] PS G:\> $1.computerName = 'blah'
[4] PS G:\> $1

computerName  ping
------------  ----
blah          True

您现在还可以在创建对象时提供 IP 地址(或服务器名称)(注意,不要提供属性名称。)

[5] PS G:\> $2 = [Server]::new("192.168.0.100")
[6] PS G:\> $2

computerName  ping
------------  ----
192.168.0.100 True

值得注意的是,请注意类中有两个构造函数。在测试这个时,一旦我指定了我自己的一个,不带参数的默认构造函数就不再有效,所以当你想使用所有默认值时,我已经包含了一个零参数构造函数。

有关这些类、它们的构造函数和方法的更多信息,我建议您查看Trevor Sullivan在过去几天发布的视频。

于 2015-05-29T01:20:00.280 回答
2

我也一直在玩 v5 中的新 Class 功能,根据我在自己的代码中发现的内容,我相信您可能需要“实例化”Server 类,通过这样做您可以提供默认值或计算值,像这样的东西:

class Server {

    [string]$computerName
    [bool]$ping

    Server([String]$Computer) {
        $computerName = $Computer
        $ping = (Test-Connection -ComputerName $Computer).count
    }

}

$1 = [server]::new("MYCOMPUTER")
$1
于 2015-05-28T22:11:08.060 回答