-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite-AsciiMotd.ps1
60 lines (46 loc) · 2.03 KB
/
Write-AsciiMotd.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
<#
.SYNOPSIS
Write some awesome ASCII art to the console.
.DESCRIPTION
Gets random ASCII art from the asciiart.eu site and prints it out in the console. You can specify the output color using the 'Color' parameter.
.PARAMETER Color
The color to use to write the ASCII art.
.EXAMPLE
.\Write-AsciiMotd.ps1 -Color Green
#>
[CmdletBinding()]
param ([System.ConsoleColor]$Color=[System.ConsoleColor]::Cyan, [string]$Category)
$baseUrl = "https://www.asciiart.eu"
$categories = "animals", "art-and-design", "books", "buildings-and-places", "cartoons", "clothing-and-accessories", "comics", "computers", "electronics", "food-and-drinks", "holiday-and-events", "logos", "miscellaneous", "movies", "music", "mythology", "nature", "people", "plants", "religion", "space", "sports-and-outdoors", "television", "toys", "vehicles", "video-games", "weapons"
$progressPreference = 'silentlyContinue'
function Get-AsciiArt ($category)
{
$result = $null
$categories = "/$category"
do {
$url = "$baseUrl$categories"
$r = Invoke-WebRequest $url
$subcategories = $r.Links | where { $_.Href -like "$categories/*" }
Write-Verbose "Found $($subcategories.Count) subcategories: $($subcategories.Href)"
if ($subcategories.Count -eq 0) {
$preElements = $r.ParsedHtml.getElementsByTagName("PRE") | where { $_.className -notlike "*text-dark*" }
Write-Verbose "Found $($preElements.Count) PRE elements."
$result = $preElements | Get-Random
} else {
$categories = $subcategories.Href | Get-Random
Write-Verbose "Categories set to '$categories'."
}
} while (($result -eq $null) -and ($subcategories.Count -gt 0))
if ($result -ne $null) {
$art = $result.innerText
} else {
$art = ""
}
return $art, $url
}
if ($Category -eq "") {
$Category = $categories | Get-Random
}
$result = Get-AsciiArt $Category
Write-Host "`n$($result[0])" -ForegroundColor $Color
Write-Host "`n from: $($result[1])" -ForegroundColor DarkGray