Tuesday, November 17, 2015

PowerShell - Basics by Example



To continue with the Basics by Example. today's version of the post written in PowerShell Enjoy!

You can copy and paste the code below in your favorite IDE/Editor, I use PowerShell ISE, and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

Beware that this is using the class keyword introduced on PowerShell version 5.0. If you're on Windows 10, you must have it already, otherwise you will need to download the Windows Management Framework 5.0 Production Preview . You can check your current version using the following command:






There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
# PowerShell Basics

# Greeting Class  
class Greet {
    # Fields and Auto-Properties (PowerShell doesn't differentiate between fields & properties)
    [String] $message = ""
    [String] $name = ""
    [Int] $loopMessage = 0
    # Set-Property method 
    [void] setMessage([String] $value) {
        $this.message = $this.Capitalize($value)
    }
    [void] setName([String] $value) {
        $this.name = $this.Capitalize($value)
    }
    # Constructor 
    Greet() {
        $this.message = ""
        $this.name = ""
        $this.loopMessage = 1
    }
    # Overloaded Constructor  
    Greet([String] $message, [String] $name, [String] $loopMessage) {
        $this.message = $message
        $this.name = $name
        $this.loopMessage = $loopMessage
    }
    # Method 1
    [String] Capitalize([String] $val) {
        if ($val.Length -eq 0) {  
            return ""
        } else {
            return (Get-Culture).TextInfo.ToTitleCase($val)            
        }
        #Write-Host ("Hello {0}!`n" -f $this.name)
    }    
    # Method 2    
    [void] Salute() {
        # "for" statement (no need of ; if linebreak used) 
        for ($i = 0; $i -lt $this.loopMessage; $i++) {  
            Write-Host ("{0} {1}!" -f $this.message, $this.name)
        }  
    }  
    # Overloaded Method 2.1  
    [void] Salute([String] $message, [String] $name, [String] $loopMessage) {
        $i = 0
        # "while" statement  
        while ($i -lt $loopMessage) {  
            Write-Host ("{0} {1}!" -f $this.Capitalize($message), $this.Capitalize($name))
            $i++
        }       
    }  
    # Overloaded Method 2.2  
    [void] Salute([String] $name) {
        $dtNow = Get-Date
        switch -regex ($dtNow.Hour) {
            "^(10|11|[6-9])" {$this.message = "good morning,";break} 
            "^(1[2-7])" {$this.message = "good afternoon,";break} 
            "^(1[8-9]|2[0-2])" {$this.message = "good evening,";break}             
            "^(23|[0-5])" { $this.message = "good night,";break} 
            default {$this.message ="huh?"}
        }
        Write-Host ("{0} {1}!" -f $this.Capitalize($this.message), $this.Capitalize($name))
    }
}

# Console Program  

# Define object of type Greet and Instantiate. Call Constructor  
$g = [Greet]::new()
# Call Set (Methods)Properties and Property (PS is case-insensitive)
$g.setMessage("hello")
$g.setName("world")
$g.LoopMessage = 5 
# Call Method 2  
$g.Salute()  
# Call Overloaded Method 2.1 and Get Properties  
$g.Salute($g.Message, "powershell", $g.LoopMessage)
# Call Overloaded Method 2.2  
$g.Salute("carlos")
              
# Stop and Exit  
Read-Host "Press any key to exit..."

Greetings Program - Minimal

And the Output is:





No comments:

Post a Comment