【问题标题】:PowerShell v5 class method - problemsPowerShell v5 类方法 - 问题
【发布时间】:2015-08-11 14:17:33
【问题描述】:

在 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

【问题讨论】:

    标签: class powershell methods powershell-5.0


    【解决方案1】:

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

    总而言之,您希望使用与默认 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 发布的视频。

    【讨论】:

      【解决方案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
      

      【讨论】:

      • 设法更进一步,使用 $this.PROP --- 听了以下播客,Jeffrey Snover 设法回答 [powershell.org/wp/2014/12/17/…
      • 不得不稍微修改一下你的代码,它因为不使用 $this.prop 而对我大喊大叫 --- 类可以编译,但我不能用参数重载构造函数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2022-01-26
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多