Dup Ver Goto 📝

PowershellBasics1

PT2/lang/powershell/examples does not exist
To
218 lines, 712 words, 5049 chars Page 'PowershellBasics1' does not exist.
# By the time I'd written compare-exmaple-1, the AI copilot had figured out
# I was writing a crib sheet for PowerShell. I was impressed by the AI copilot's
# ability to figure out what I was doing. I was also impressed by the AI copilot's
# ability to generate code that was similar to what I was writing. I was also
# impressed by the AI copilot's ability to generate code that was better than
# what I was writing. I was also impressed by the AI copilot's ability to generate
# code that was more concise than what I was writing. I was also impressed by the
# AI copilot's ability to generate code that was more readable than what I was
# writing. I was also impressed by the AI copilot's ability to generate code that
# Ok ok ok copilot, I'll stop autocompleting here. But VS Code rocks for Powershell.
# It's one of the things for which I won't use vim.

# Printing, Input
function snarf {
    $a = Read-Host "snarf"
    Write-Host "you gave me $a to snarf"
}

# logic comparison
function compare-example-1 {
    $a = 1
    $b = 2
    if ($a -eq $b) {
        Write-Host "a equals b"
    } else {
        Write-Host "a does not equal b"
    }
}

# parse integer
function parse-integer-example-1 {
    $a = Read-Host "give me a number"
    $a = [int]$a
    Write-Host "you gave me $a"
}

# switch
function switch-example-1 {
    $a = Read-Host "give me a number"
    switch ($a) {
        1 { Write-Host "you gave me 1" }
        2 { Write-Host "you gave me 2" }
        default { Write-Host "you gave me something else" }
    }
}

# loops
function while-loop-example-1 {
    $a = 0
    while ($a -lt 5) {
        Write-Host "a is $a"
        $a++
    }
}
function for-loop-example-1 {
    for ($i = 0; $i -lt 5; $i++) {
        Write-Host "i is $i"
    }
}

function do-loop-example-1 {
    $a = 0
    do {
        Write-Host "a is $a"
        $a++
    } while ($a -lt 5)
}

# services
function get-service-example-1 {
    $services = Get-Service
    $numberOfServicesRunning = ($services | Where-Object { $_.Status -eq "Running" }).Count
    foreach ($service in $services) {
        Write-Host "service name: $($service.Name)"
        Write-Host "service status: $($service.Status)"
    }
    Write-Host "number of services running: $numberOfServicesRunning"
}
get-service-example-1

# math
function math-example-1 {
    $a = 1
    $b = 2
    $sum = $a + $b
    $product = $a * $b
    $quotient = $a / $b
    $remainder = $a % $b
    $difference = $a - $b
    Write-Host "sum,product,quotient,remainder,difference is $sum, $product, $quotient, $remainder, $difference" 
}

# arrays
function array-example-1 {
    $a = @("a", "b", "c")
    $a += "d"
    $a += "e"
    $a += "f"
    foreach ($element in $a) {
        Write-Host "element is $element"
    }
}

# hash tables
function hash-table-example-1 {
    $a = @{}
    $a["a"] = 1
    $a["b"] = 2
    $a["c"] = 3
    foreach ($key in $a.Keys) {
        Write-Host "key is $key"
        Write-Host "value is $($a[$key])"
    }
}

# functions
function function-example-1 {
    function add {
        param($a, $b)
        return $a + $b
    }
    $sum = add 1 2
    Write-Host "sum is $sum"
}

# classes
function class-example-1 {
    class Person {
        [string] $name
        [int] $age
        Person([string] $name, [int] $age) {
            $this.name = $name
            $this.age = $age
        }
        [string] ToString() {
            return "name: $($this.name), age: $($this.age)"
        }
    }
    $person = [Person]::new("john", 30)
    Write-Host "person is $person"
}

# objects
function object-example-1 {
    $person = New-Object PSObject
    $person | Add-Member -MemberType NoteProperty -Name "name" -Value "john"
    $person | Add-Member -MemberType NoteProperty -Name "age" -Value 30
    Write-Host "person is $person"
}

# files
function file-example-1 {
    $path = "C:\Users\john\Documents\file.txt"
    $content = Get-Content $path
    Write-Host "content is $content"
}

# json
function json-example-1 {
    $json = @"
{
    "name": "john",
    "age": 30
}
"@
    $object = ConvertFrom-Json $json
    Write-Host "object is $object"
}

# web
function web-example-1 {
    $url = "https://jsonplaceholder.typicode.com/todos/1"
    $response = Invoke-RestMethod -Uri $url
    Write-Host "response is $response"
}

# regex
function regex-example-1 {
    $text = "hello world"
    $pattern = "hello"
    $isMatch = $text -match $pattern
    Write-Host "isMatch is $isMatch"
}

# error handling
function Handle-ErrorExample-1 {
    try {
        throw "error"
    } catch {
        Write-Host "error is $_"
    }
}

# main
function main {
    snarf
    compare-example-1
    parse-integer-example-1
    switch-example-1
    while-loop-example-1
    for-loop-example-1
    do-loop-example-1
    get-service-example-1
    math-example-1
    array-example-1
    hash-table-example-1
    function-example-1
    class-example-1
    object-example-1
    file-example-1
    json-example-1
    web-example-1
    regex-example-1
    Handle-ErrorExample-1
}