Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class PowerShell v5类方法-问题_Class_Powershell_Methods_Powershell 5.0 - Fatal编程技术网

Class PowerShell v5类方法-问题

Class PowerShell v5类方法-问题,class,powershell,methods,powershell-5.0,Class,Powershell,Methods,Powershell 5.0,在PowerShell v5中使用新的类函数,如果我们可以将方法放入类中,我正在尝试了解一下 我试过下面的游戏,玩了一会儿,但是运气不好 类服务器{ [字符串]$computerName=“192.168.0.200” [bool]$ping=(测试连接-ComputerName$ComputerName).count } $1=[服务器]::new() $1.computerName=“诸如此类” 我尝试过通过设置属性手动输入计算机名,但我认为您在创建对象时需要它 $1=[server]::

在PowerShell v5中使用新的类函数,如果我们可以将方法放入类中,我正在尝试了解一下

我试过下面的游戏,玩了一会儿,但是运气不好

类服务器{
[字符串]$computerName=“192.168.0.200”
[bool]$ping=(测试连接-ComputerName$ComputerName).count
}
$1=[服务器]::new()
$1.computerName=“诸如此类”
我尝试过通过设置属性手动输入计算机名,但我认为您在创建对象时需要它

$1=[server]::新建($computerName=“192.168.0.200”)
我得到的例外是

[ERROR]调用带有“0”参数的“.ctor”时发生异常:“无法验证参数“ComputerName”上的参数。该参数为null或空。请提供一个非null或空的参数,然后重试
再次执行[错误]命令。“
[错误]位于D:\Google Drive\Projects\VSPowerShell\DiscoveryFramework\DiscoveryFramework\DiscoveryFramework\class.ps1:12字符:1
[错误]+$1=[服务器]::new()
[错误]+~~~~~~~~~~~~~~~~~~~~
[错误]+类别信息:未指定:(:)[],MethodInvocationException
[错误]+FullyQualifiedErrorId:参数BindingValidationException
[错误]
[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
服务器
[DBG]:PS C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\IDE>$1.gettype()
服务器
来自$error的完整异常链接处于打开状态


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

PS路径:\>类服务器{
[字符串]$computerName=“192.168.0.200”
[bool]$ping=(测试连接-ComputerName$this.ComputerName).count
}
PS路径:\>
PS路径:\>$var=[server]::new()
PS路径:\>$var
计算机名ping
------------  ----
192.168.0.200正确

我还一直在玩v5中的新类功能,从我自己的代码中发现,我相信您可能需要“实例化”服务器类,通过这样做,您可以提供默认值或计算值,如下所示:

class Server {

    [string]$computerName
    [bool]$ping

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

}

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

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

作为总结,您希望使用不同于默认值的ip地址初始化服务器(并允许触发$ping的默认值)

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

现在,无需向对象传递参数即可创建对象:

[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地址(或服务器名称)(注意,不提供属性名称。)

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


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

尝试进一步了解,使用$this.PROP--听了下面的播客,Jeffrey Snover成功回答了问题[必须稍微修改一下代码,它因为我没有使用$this.prop而对我大喊大叫---类可以编译,但我不能用参数重载构造函数。
[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
[5] PS G:\> $2 = [Server]::new("192.168.0.100")
[6] PS G:\> $2

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