简体   繁体   中英

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

I seriously apologize for the horrible title of this post...but I'm new to OOP and PowerShell, so I have absolutely no idea how to spell out what I'm asking for. After finding out I could use PowerShell to connect to API's and things like that, I've been working on it non-stop. I'm now working on a script where I need to create a class. I've done this successfully and I'm able to build methods and use them. As of now, all of my objects and methods only have a single 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've written a script that uses my TV's API. So currently I have commands like this:

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

But I'd like to be able to do this instead:

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

Is this possible?

And yes, I realize there are probably other languages that might be better suited. But I'm just playing around and using this as a transitioning language into actual C# development. For me it's easier to throw a bunch of scripts and parameters into a file and run the file than trying to set up a C# project, compile and test.

Here is the current code for the class:

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
    }
}

Define a new class for the Power functions and inject the IPAddress and Auth token into a new instance of that class in the constructor of the main class:

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)
    }
}

You could create classes for Power and Input and store them as properties in your TV-class. Remember to pass a reference to the parent object (TV) so they can access the IPAddress and AuthToken -values.

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"

Demo:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM