2023-07-29 10:34:04 +02:00
|
|
|
*write-code.ps1*
|
|
|
|
================
|
2022-11-17 19:46:02 +01:00
|
|
|
|
2023-08-06 11:42:46 +02:00
|
|
|
This PowerShell script generates and writes PowerShell code on the console (no AI, just for fun).
|
2022-11-17 19:46:02 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-17 19:46:02 +01:00
|
|
|
```powershell
|
2023-07-29 10:15:44 +02:00
|
|
|
PS> ./write-code.ps1 [[-color] <String>] [[-speed] <Int32>] [<CommonParameters>]
|
2022-11-17 19:46:02 +01:00
|
|
|
|
|
|
|
-color <String>
|
2023-08-06 11:42:46 +02:00
|
|
|
Specifies the text color to use ("green" by default)
|
2022-11-17 19:46:02 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value green
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-speed <Int32>
|
|
|
|
Specifies the speed in milliseconds per code line (500 by default)
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value 500
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2022-11-17 19:46:02 +01:00
|
|
|
```powershell
|
2023-08-06 11:42:46 +02:00
|
|
|
PS> ./write-code.ps1
|
|
|
|
try {
|
|
|
|
...
|
2022-11-17 19:46:02 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2022-11-17 19:46:02 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Writes code
|
|
|
|
.DESCRIPTION
|
2023-08-06 11:42:46 +02:00
|
|
|
This PowerShell script generates and writes PowerShell code on the console (no AI, just for fun).
|
2022-11-17 20:02:26 +01:00
|
|
|
.PARAMETER color
|
2023-08-06 11:42:46 +02:00
|
|
|
Specifies the text color to use ("green" by default)
|
2022-11-17 20:02:26 +01:00
|
|
|
.PARAMETER speed
|
|
|
|
Specifies the speed in milliseconds per code line (500 by default)
|
|
|
|
.EXAMPLE
|
2023-08-06 11:42:46 +02:00
|
|
|
PS> ./write-code.ps1
|
|
|
|
try {
|
|
|
|
...
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$color = "green", [int]$speed = 500) # milliseconds
|
|
|
|
|
2023-08-06 11:42:46 +02:00
|
|
|
[string]$global:Tabs = ""
|
|
|
|
|
2022-11-17 20:02:26 +01:00
|
|
|
function GetRandomCodeLine {
|
|
|
|
$Generator = New-Object System.Random
|
2023-08-06 11:42:46 +02:00
|
|
|
$Num = [int]$Generator.next(0, 31)
|
2022-11-17 20:02:26 +01:00
|
|
|
switch($Num) {
|
2023-08-06 11:42:46 +02:00
|
|
|
0 { return $Tabs + "`$count = 0" }
|
|
|
|
1 { return $Tabs + "`$count++" }
|
|
|
|
2 { return $Tabs + "exit 0 # success" }
|
|
|
|
3 { return $Tabs + "`$Files = Get-ChildItem C:" }
|
|
|
|
4 { return $Tabs + "Start-Sleep -seconds 1" }
|
|
|
|
5 { return $Tabs + "`$Generator = New-Object System-Random" }
|
|
|
|
6 { $global:Tabs = " "; return "} else {" }
|
|
|
|
7 { $global:Tabs = " "; return "} catch {" }
|
|
|
|
8 { $global:Tabs = " "; return "} elseif (`$count -eq 0) {" }
|
|
|
|
9 { return $Tabs + "Write-Host `"Hello World`" " }
|
|
|
|
10 { $global:Tabs = " "; return "while (`$true) {" }
|
|
|
|
11 { return $Tabs + "# next part:" }
|
|
|
|
12 { return $Tabs + "exit 1 # failed" }
|
|
|
|
13 { return $Tabs + "return 1" }
|
|
|
|
14 { return $Tabs + "return 0" }
|
|
|
|
15 { return $Tabs + "Write-Progress `"Working...`" " }
|
|
|
|
16 { return $Tabs + "[bool]`$KeepAlive = `$true" }
|
|
|
|
17 { return $Tabs + "# Copyright © 2023 write-code.ps1. All Rights Reserved." }
|
|
|
|
18 { $global:Tabs = " "; return "for ([int]`$i = 0; `$i -lt 42; `$i++) {" }
|
|
|
|
19 { return $Tabs + "`$StopWatch = [system.diagnostics.stopwatch]::startNew()" }
|
|
|
|
20 { return $Tabs + "[int]`$Elapsed = `$StopWatch.Elapsed.TotalSeconds" }
|
|
|
|
21 { $global:Tabs = " "; return "if (`$count -eq 42) {" }
|
|
|
|
22 { $global:Tabs = " "; return "} finally {" }
|
|
|
|
23 { return $Tabs + "throw `"Can't open file`" " }
|
|
|
|
24 { return $Tabs + "Start-Sleep -milliseconds 42" }
|
|
|
|
25 { return $Tabs + "`$Choice = Read-Host `"Red or blue pill?`"" }
|
|
|
|
26 { return $Tabs + "[int]`$Answer = 42" }
|
|
|
|
27 { $global:Tabs = ""; return "}" }
|
|
|
|
28 { $global:Tabs = " "; return "try {" }
|
|
|
|
29 { $global:Tabs = " "; return "foreach(`$Row in `$Table) {" }
|
|
|
|
30 { $global:Tabs = " "; return "foreach(`$File in `$Files) {" }
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
while ($true) {
|
|
|
|
Write-Host -foreground $color "$(GetRandomCodeLine)"
|
|
|
|
Start-Sleep -milliseconds $speed
|
|
|
|
}
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-08-06 21:36:33 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of write-code.ps1 as of 08/06/2023 21:36:18)*
|