-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-Fortune.ps1
73 lines (66 loc) · 2.13 KB
/
Get-Fortune.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function Get-Fortune {
<#
.SYNOPSIS
Display a short quote
.DESCRIPTION
Display a short quote from a file which defaults to: 'c:\scripts\wisdom.txt') but can be changed with parameter -Path.
.NOTES
# Sample wisdom.txt file with 3 entries. Each 'fortune' is delimited by a line consisting of just the pct sign
# The last fortune in the file should NOT be terminated with a pct sign
%
This too will pass.
- Attar
%
Don't think, just do.
- Horace
%
Time is money.
- Benjamin Franklin
.OUTPUTS
[string]
.PARAMETER Path
A path to a filename containing the fortunes. Defaults to: (Get-Module -Name PoshFunctions).Path + '\Resources\Wisdom.txt'
Aliased to 'FileName' and 'Fortune'
.PARAMETER Delimiter
Indicates delimiter between the individual fortunes. Defaults to "`n%`n" (newline percent newline)
.NOTES
When this function reads in the file it will replace CRLF ("`r`n") with LF ("`n") so as to simplify the Delimiter parameter
.LINK
Get-Content
Get-Random
Split-Path
#>
#region Parameter
[CmdletBinding(ConfirmImpact='None')]
[alias('Fortune')] #FunctionAlias
[OutputType('string')]
Param(
[Alias('FileName', 'Fortune')]
[string] $Path = $script:FortuneFile,
[string] $Delimiter = "`n%`n",
[switch] $Speak
)
#endregion Parameter
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
Write-Verbose -Message "Using fortune file [$Path]"
}
process {
if (Test-Path -Path $Path) {
Write-Verbose -Message "Using [$Path] for fortune file"
Write-Verbose -Message "Delimiter [$Delimiter]"
$Fortune = (Get-Content -Raw -Path $Path -ReadCount 0) -replace "`r`n", "`n" -split $Delimiter | Get-Random
if ($Speak) {
$Fortune
$Fortune | Invoke-Speak
} else {
$Fortune
}
} else {
Write-Error -Message "ERROR: File [$Path] does not exist."
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}