PowerShell v5 class 方法 - 问题

PowerShell v5 class method - problems

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

我已经尝试了下面的方法并尝试了一下,但一直没有成功。

class Server {

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

}

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

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

 = [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] +  = [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> 
Server
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> .gettype()
Server

来自 $error 的完全异常 link 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

我也一直在研究 v5 中的新 Class 功能,根据我在自己的代码中发现的内容,我相信您可能需要 "Instantiate" 服务器 class 并通过这样做,您可以提供默认值或计算值,如下所示:

class Server {

    [string]$computerName
    [bool]$ping

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

}

 = [server]::new("MYCOMPUTER")

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

总而言之,您想要使用不同于默认的 IP 地址来初始化您的服务器(并允许触发 $ping 的默认值。)

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

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:\>  = [Server]::new()
[2] PS G:\> 

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



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

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

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

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

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

值得注意的是,class 中有两个构造函数。对此进行测试时,一旦我指定了自己的构造函数,不带参数的默认构造函数就不再有效,因此我包含了一个零参数构造函数,供您在使用所有默认值时使用。

有关这些 class 及其构造函数和方法的更多信息,我建议您查看最近几天发布的视频 Trevor Sullivan