PowerShell v5类方法 - 问题

Luk*_*ith 5 methods powershell class powershell-5.0

在PowerShell v5中使用新的类函数,如果我们可以将方法放入类中,我正试图解决这个问题.

我已经尝试了以下并且玩了一下,但没有运气.

class Server {

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

}

$1 = [server]::new()
$1.computerName = "blah"
Run Code Online (Sandbox Code Playgroud)

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

$1 = [server]::new($computerName = "192.168.0.200")
Run Code Online (Sandbox Code Playgroud)

我得到的例外情况是

[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
Run Code Online (Sandbox Code Playgroud)

来自$ 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
Run Code Online (Sandbox Code Playgroud)

Win*_*dos 4

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

总而言之,您希望使用与默认值不同的 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
}
Run Code Online (Sandbox Code Playgroud)

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

[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
Run Code Online (Sandbox Code Playgroud)

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

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

computerName  ping
------------  ----
192.168.0.100 True
Run Code Online (Sandbox Code Playgroud)

值得注意的是,请注意类中有两个构造函数。测试此功能时,一旦我指定了自己的参数,不带参数的默认构造函数就不再有效,因此当您想要使用所有默认值时,我包含了一个零参数构造函数。

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