[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本
作者:精品下载站 日期:2024-12-14 08:07:41 浏览:11 分类:玩电脑
周五乐趣:使用 ChatGPT 编写 PowerShell 脚本
我一直在尝试最新的人工智能,ChatGPT。我问过它一些愚蠢的问题。
以及真正重要的问题。
但我发现最令人着迷的是使用 ChatGPT 编写 PowerShell 代码。我向 AI 发出了这样的指令:“编写一个 PowerShell 函数,该函数将在屏幕中央以交替颜色显示“Hello,World”。包含一个参数以使消息闪烁。”。几秒钟后,它生成了这段代码。
function Show-HelloWorld {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet("On", "Off")]
[string]$Blink
)
# Clear the screen
Clear-Host
# Set the text colors to alternate between red and green
$colors = @("Red", "Green")
$colorIndex = 0
# Set the cursor position to the center of the screen
$x = (Get-Host).UI.RawUI.WindowSize.Width / 2
$y = (Get-Host).UI.RawUI.WindowSize.Height / 2
# Set the cursor position and display the message
Write-Host "Hello, World" -ForegroundColor $colors[$colorIndex] -NoNewline
$host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates $x, $y
# Toggle the text color and blink the message if the Blink parameter is set to "On"
while ($Blink -eq "On") {
Start-Sleep -Milliseconds 500
$colorIndex = ($colorIndex + 1) % 2
Write-Host "`b" -NoNewline
Write-Host "Hello, World" -ForegroundColor $colors[$colorIndex] -NoNewline
}
}
您需要将此过程视为与可能仍在学习 PowerShell 的朋友一起编写脚本。值得称赞的是,人工智能为该功能使用了适当的动词。它足够聪明,意识到需要清除屏幕。脚本语言正是我对刚入门的人所期望的。尽管如此,它并没有使我的眨眼选项成为一个开关,甚至没有成为一个布尔值。但这很容易改变。我会一眨不眨地运行它。
不完全是我的想法。添加眨眼也不是我所希望的。
我要求ChatGPT修改该功能。
修改函数以使用 ANSI 转义序列或 $PSStyle。每个字符应该是不同的颜色。
结果也好不到哪儿去,跑不了。这是摘录。
# Set the text colors to alternate between red and green
$colors = @("31", "32")
$colorIndex = 0
# Set the cursor position to the center of the screen
$x = (Get-Host).UI.RawUI.WindowSize.Width / 2
$y = (Get-Host).UI.RawUI.WindowSize.Height / 2
# Set the cursor position and display the message
Write-Host "H" -NoNewline
Write-Host "e" -NoNewline -ForegroundColor $colors[$colorIndex]
$colorIndex = ($colorIndex + 1) % 2
Write-Host "l" -NoNewline -ForegroundColor $colors[$colorIndex]
$colorIndex = ($colorIndex + 1) % 2
Write-Host "l" -NoNewline -ForegroundColor $colors[$colorIndex]
$colorIndex = ($colorIndex + 1) % 2
Write-Host "o" -NoNewline -ForegroundColor $colors[$colorIndex]
$colorIndex = ($colorIndex + 1) % 2
Write-Host ", " -NoNewline -ForegroundColor $colors[$colorIndex]
但我可以使用该代码作为起点,并根据我对 PowerShell 的了解对其进行修改。我要表达的观点是,使用 AI,即使是像 CodePilot 这样的工具,也可以提供很大的帮助,但它不会教你 PowerShell,而且你仍然负责编写有效且正确的代码。
也就是说,让我向您介绍另外两个我要求 ChatGPT 为我创建的函数。我想要一个在控制台窗口中显示分形的函数。一个函数创建一个 Julia 集。
function Show-JuliaSet {
[cmdletbinding()]
param (
[double]$cReal = -0.8,
[double]$cImag = 0.156,
[int]$width = 80,
[int]$height = 24,
[int]$maxIterations = 256
)
# Create the Julia set function
function julia {
param (
[double]$cReal,
[double]$cImag,
[int]$width,
[int]$height,
[int]$maxIterations
)
# Calculate the pixel values for the Julia set
$pixels = for ($y = 0; $y -lt $height; $y++) {
for ($x = 0; $x -lt $width; $x++) {
# Map the pixel coordinates to the complex plane
$zReal = [math]::Round((($x / $width) * 3.5) - 2.5, 2)
$zImag = [math]::Round((($y / $height) * 2) - 1, 2)
# Iterate the Julia set equation
$iteration = 0
while (($zReal * $zReal + $zImag * $zImag) -lt 4) {
$tempReal = $zReal * $zReal - $zImag * $zImag + $cReal
$zImag = 2 * $zReal * $zImag + $cImag
$zReal = $tempReal
$iteration++
if ($iteration -ge $maxIterations) {
break
}
}
# Return the number of iterations as a pixel value
$iteration
}
}
# Print the Julia set to the console
for ($y = 0; $y -lt $height; $y++) {
for ($x = 0; $x -lt $width; $x++) {
$pixel = $pixels[$y * $width + $x]
if ($pixel -eq $maxIterations) {
Write-Host "." -NoNewline
} else {
Write-Host " " -NoNewline
}
}
Write-Host ""
}
}
# Call the Julia set function
julia -cReal $cReal -cImag $cImag -width $width -height $height -maxIterations $maxIterations
}
我颠倒了原来的显示代码。
我必须承认,我永远无法弄清楚数学部分,这在 Mandelbrot 函数中绝对是正确的。
function Show-MandelbrotSet {
[cmdletbinding()]
param (
[int]$width = 80,
[int]$height = 24,
[int]$maxIterations = 256
)
# Create the Mandelbrot set function
function mandelbrot {
param (
[int]$width,
[int]$height,
[int]$maxIterations
)
# Calculate the pixel values for the Mandelbrot set
$pixels = for ($y = 0; $y -lt $height; $y++) {
for ($x = 0; $x -lt $width; $x++) {
# Map the pixel coordinates to the complex plane
$cReal = [math]::Round((($x / $width) * 3.5) - 2.5, 2)
$cImag = [math]::Round((($y / $height) * 2) - 1, 2)
$zReal = 0
$zImag = 0
# Iterate the Mandelbrot set equation
$iteration = 0
while (($zReal * $zReal + $zImag * $zImag) -lt 4) {
$tempReal = $zReal * $zReal - $zImag * $zImag + $cReal
$zImag = 2 * $zReal * $zImag + $cImag
$zReal = $tempReal
$iteration++
if ($iteration -ge $maxIterations) {
break
}
}
# Return the number of iterations as a pixel value
$iteration
}
}
# Print the Mandelbrot set to the console
for ($y = 0; $y -lt $height; $y++) {
for ($x = 0; $x -lt $width; $x++) {
$pixel = $pixels[$y * $width + $x]
if ($pixel -eq $maxIterations) {
Write-Host " " -NoNewline
} else {
Write-Host "." -NoNewline
}
}
Write-Host ""
}
}
# Call the Mandelbrot set function
mandelbrot -width $width -height $height -maxIterations $maxIterations
}
我会让你利用这些功能并进一步开发它们。
我发现用人工智能编码非常有趣。我绝对不担心很快就会被取代。但人工智能可以减轻编码中的一些繁重工作,甚至可能提供一种你从未考虑过的方法。将人工智能视为灵感的缪斯。
您在 VSCode 中编写 AI 脚本或使用 CodePilot 的经历如何?
- 上一篇:[玩转系统] WPF 倒计时器
- 下一篇:[玩转系统] 最佳实践成就完美
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag