Classes in Powershell 5, need help creating $ foo.bar.run ()

I seriously apologize for the awful title of this post ... but I'm new to OOP and PowerShell, so I have absolutely no idea how to spell out what I ask. After learning that I can use PowerShell to connect to the API and the like, I worked on it non-stop. Now I am working on a script where I need to create a class. I have done this successfully, and I can create methods and use them. At the moment, all my objects and methods have only one level .... So if I call a method on an object, it always looks like this: $ foo.run ()

But I want to create something like $ foo.bar.run ()

I wrote a script that uses my TV API. So currently I have commands like this:

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

But I would like to do this instead:

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

Is it possible?

, , , , , . #. , #, .

:

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
    }
}
+4
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)
    }
}
+2

Power Input -. (), IPAddress 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"

:

$TV.Power.On()
#Outputs
Set-Power -action on -IPAddress 127.0.0.1 -auth AuthKey123

$TV.IPAddress = "10.0.0.1"
$TV.Power.On()
#Outputs
Set-Power -action on -IPAddress 10.0.0.1 -auth AuthKey123
+2

Source: https://habr.com/ru/post/1695023/


All Articles