samples/SampleScript5.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#requires -version 5.1
#requires -RunAsAdministrator

#This function can be used for analysis and testing. It
#Isn't something that will run without error or make any sense.
#The use of aliases is intentional for testing purposes

Function Get-Result {
    <#
.Synopsis
Sample function
.Description
This is a sample
.Parameter Count
How many numbers do you want?
#>

    [cmdletbinding()]
    [alias('grx')]
    Param (
        [Parameter(Position=0)]
        [ValidateRange(1,100)]
        [int]$Count = 1,
        [ValidateNotNullOrEmpty]
        [string]$Name = "Foo"
        )

    Begin {
        Write-Host "this is a sample script that doesn't do anything but write a random number" -ForegroundColor Yellow
        #this is an undefined alias
        w -msg "Processing $Name"
        $a = [System.DateTime]::Now
        write-host $a
        $os = gcim Win32_OperatingSystem
        Write-Verbose "Running in $($PSVersionTable.PSVersion) on $($os.caption)"
        $f = Join-Path $([system.environment]::GetEnvironmentVariable("TEMP") "r.txt"
    }
    Process {
        #get numbers
        1..$count | ForEach {
            Get-Random -Minimum 1 -Maximum 1000
        } | tee -FilePath $f
    }
    End {
        Write-Host "Ending script" -ForegroundColor yellow
        $b = [System.DateTime]::now
        $c = New-TimeSpan $a $b
        write-verbose "Runtime: $c"
        notepad.exe $f
        #run a clean batch file
        c:\scripts\cleanup.bat
    }
} #close function