Powershell 5中的类需要帮助创建$ foo.bar.run()

时间:2018-03-18 17:09:24

标签: powershell oop powershell-v5.0

我为这篇文章的可怕标题道歉...但我是OOP和PowerShell的新手,所以我完全不知道如何拼出我要求的内容。在发现我可以使用PowerShell连接到API之类的东西之后,我一直在不间断地工作。我现在正在编写一个我需要创建一个类的脚本。我已经成功完成了这项工作,我可以构建方法并使用它们。截至目前,我的所有对象和方法只有一个级别....所以如果我在一个对象上调用一个方法,它总是看起来像$ foo.run()

但我想创建像$ foo.bar.run()

这样的东西

我写了一个使用我的电视API的脚本。所以目前我有这样的命令:

$tv.TurnOn()
$tv.TurnOff()
$tv.GetPowerStatus()
$tv.ListInputs()
$tv.GetCurrentInput()
$tv.SetInput($name)

但我希望能够做到这一点:

$tv.Power.On()
$tv.Power.Off()
$tv.Power.Status()
$tv.Input.List()
$tv.Input.Current()
$tv.Input.Set($name)

这可能吗?

是的,我意识到可能还有其他语言可能更适合。但我只是在玩这个并将其用作实际C#开发的过渡语言。对我来说,将一堆脚本和参数放入文件并运行文件比尝试设置C#项目,编译和测试更容易。

以下是该课程的当前代码:

Class VizioTV {
    [String]$IPAddress
    [String]$AuthToken

    [void]TurnOn() {
        Set-Power -action "on" -IPAddress $this.IPAddress -auth $this.AuthToken
    }

    [void]TurnOff() {
        Set-Power -action "off" -IPAddress $this.IPAddress -auth $this.AuthToken
    }

    [String]GetPowerStatus() {
        Return Get-PowerStatus -IPAddress $this.IPAddress -auth $this.AuthToken
    }
}

2 个答案:

答案 0 :(得分:2)

Power函数定义一个新类,并将IPAddress和Auth标记注入主类构造函数中该类的新实例:

class VizioTVPower {
    hidden [String]$IPAddress
    hidden [String]$AuthToken

    VizioTVPower([string]$IPAddress, [string]$AuthToken) {
        $this.IPAddress = $IPAddress
        $this.AuthToken = $AuthToken
    }

    [void]TurnOn() {
        Set-Power -action "on" -IPAddress $this.IPAddress -auth $this.AuthToken
    }

    [void]TurnOff() {
        Set-Power -action "off" -IPAddress $this.IPAddress -auth $this.AuthToken
    }

    [String]GetPowerStatus() {
        Return Get-PowerStatus -IPAddress $this.IPAddress -auth $this.AuthToken
    }
}


class VizioTV {
    [String]$IPAddress
    [String]$AuthToken

    [VizioTVPower]$Power

    VizioTV([string]$IPAddress, [string]$AuthToken) {
        $this.IPAddress = $IPAddress
        $this.AuthToken = $AuthToken

        $this.Power = [VizioTVPower]::new($this.IPAddress, $this.AuthToken)
    }
}

答案 1 :(得分:2)

您可以为package com.example.saif.dammi; /** * Created by Saif on 3/18/2018. */ public class UserInfo { String Birthday; String id; // public UserInfo(String birthday, String id) { // Birthday = birthday; // this.id = id; // } public String getBirthday() { return Birthday; } public void setBirthday(String birthday) { Birthday = birthday; } public String getId() { return id; } public void setId(String id) { this.id = id; } } Power创建课程,并将其存储为电视课程中的属性。请记住传递对父对象(TV)的引用,以便他们可以访问InputIPAddress - 值。

AuthToken

演示:

class VizioTVInput {
    [VizioTV]$TV

    VizioTVInput([VizioTV]$TV) {
        #Keep reference to parent
        $this.TV = $TV
    }

    [string[]]List() {
        return "Something"
    }

    [string]Current() {
        return "Something"
    }

    [void]Set([string]$name) {
        #Do something with $name
    }

}

class VizioTVPower {
    [VizioTV]$TV

    VizioTVPower([VizioTV]$TV) {
        #Keep reference to parent
        $this.TV = $TV
    }

    [void]On() {
        #Remove Write-Host, just used for demo
        Write-Host Set-Power -action "on" -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
    }

    [void]Off() {
        Set-Power -action "off" -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
    }

    [String]Status() {
        return Get-PowerStatus -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
    }

}

Class VizioTV {
    [String]$IPAddress
    [String]$AuthToken

    [VizioTVInput]$Input = [VizioTVInput]::new($this)
    [VizioTVPower]$Power = [VizioTVPower]::new($this)

    #Made it mandatory to input IP and AuthToken. Remove constructor if not needed
    VizioTV([string]$IPAddress,[string]$AuthToken) {
        $this.IPAddress = $IPAddress
        $this.AuthToken = $AuthToken
    }

}

$TV = New-Object VizioTV -ArgumentList "127.0.0.1", "AuthKey123"