PowerShell/Scripts/write-code.ps1

67 lines
2.0 KiB
PowerShell
Raw Normal View History

2023-02-17 13:22:50 +01:00
<#PSScriptInfo
.VERSION 1.0
.GUID 3bb10ee7-38c1-41b9-88ea-16899164fc19
.AUTHOR Markus Fleschutz
.COPYRIGHT CC0
#>
<#
2022-11-09 14:45:11 +01:00
.SYNOPSIS
Writes code
.DESCRIPTION
2022-11-09 15:54:06 +01:00
This PowerShell script generates and writes PowerShell code on the console (for fun).
2022-11-09 16:16:15 +01:00
.PARAMETER color
Specifies the color to use ("green" by default)
2022-11-09 15:54:06 +01:00
.PARAMETER speed
2022-11-09 16:16:15 +01:00
Specifies the speed in milliseconds per code line (500 by default)
2022-11-09 14:45:11 +01:00
.EXAMPLE
PS> ./write-code
.LINK
https://github.com/fleschutz/PowerShell
#>
2022-11-09 16:16:15 +01:00
param([string]$color = "green", [int]$speed = 500) # milliseconds
2022-11-09 15:54:06 +01:00
2022-11-09 14:45:11 +01:00
function GetRandomCodeLine {
$Generator = New-Object System.Random
2022-11-09 15:54:06 +01:00
$Num = [int]$Generator.next(0, 24)
2022-11-09 14:45:11 +01:00
switch($Num) {
2022-11-09 15:54:06 +01:00
0 { return " `$count = 0" }
1 { return " `$count++" }
2 { return " exit 0 # success" }
3 { return " `$Files = Get-ChildItem C:" }
4 { return " Start-Sleep 1" }
5 { return " `$Generator = New-Object System-Random" }
6 { return "} else {" }
2022-11-09 14:45:11 +01:00
7 { return "} catch {" }
2022-11-09 15:54:06 +01:00
8 { return "} elseif (`$count -eq 0) {" }
9 { return " Write-Host `"Hello World`" " }
10 { return " while (`$true) {" }
2022-11-09 14:45:11 +01:00
11 { return "# next part:" }
2022-11-09 15:54:06 +01:00
12 { return " exit 1 # failed" }
13 { return " return 1" }
14 { return " return 0" }
15 { return " Write-Progress `"Working...`" " }
16 { return " [bool]`$KeepAlive = `$true" }
17 { return "# Copyright © 2022 write-code.ps1. All Rights Reserved." }
18 { return " for ([int]`$i = 0; `$i -lt 100; `$i++) {" }
19 { return " `$StopWatch = [system.diagnostics.stopwatch]::startNew()" }
20 { return " [int]`$Elapsed = `$StopWatch.Elapsed.TotalSeconds" }
21 { return " if (`$count -eq 0) { `$count = Read-Host `"Enter number of iterations`" " }
22 { return " } finally {" }
23 { return " throw `"Can't open file`" " }
2022-11-09 14:45:11 +01:00
}
}
try {
2022-11-09 16:16:15 +01:00
Write-Host -foreground $color "try {"
2022-11-09 14:45:11 +01:00
while ($true) {
2022-11-09 16:16:15 +01:00
Write-Host -foreground $color "$(GetRandomCodeLine)"
Start-Sleep -milliseconds $speed
2022-11-09 14:45:11 +01:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2023-02-17 13:22:50 +01:00
}