Cuma Eğlencesi: ChatGPT ile PowerShell Komut Dosyası Oluşturma
En yeni yapay zeka ChatGPT'yi denedim. Aptalca sorular sordum.
Gerçekten önemli soruların yanı sıra.
Ancak en etkileyici bulduğum şey PowerShell kodunu yazmak için ChatGPT'yi kullanmak. Yapay zekaya şu talimatı verdim: “Ekranın ortasında alternatif renklerde “Merhaba Dünya” görüntüleyecek bir PowerShell işlevi yazın. Mesajın yanıp sönmesini sağlayacak bir parametre ekleyin.”. Birkaç saniye sonra bu kodu oluşturdu.
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
}
}
Bu süreci, hâlâ PowerShell öğreniyor olabilecek bir arkadaşınızla komut dosyası yazmak olarak düşünmeniz gerekir. Yapay zekanın takdirine göre, işlev için uygun bir fiil kullanıldı. Ekranı temizlemenin gerekli olacağını anlayacak kadar akıllıydı. Ve betik dili, yeni başlayan birinden beklediğim dildir. Ancak bu, göz kırpma seçeneğimi bir anahtara, hatta bir boole değerine bile dönüştürmedi. Ancak bunu değiştirmek yeterince kolaydır. Gözümü kırpmadan çalıştıracağım.
Tam olarak aklımdaki şey değildi. Yanıp sönmeyi eklemek de umduğum şey değildi.
ChatGPT'den işlevi revize etmesini istedim.
ANSI çıkış dizilerini veya $PSStyle'ı kullanacak şekilde işlevi gözden geçirin. Her karakter farklı renkte olmalıdır.
Sonuç çok daha iyi değildi ve koşmayı başaramadı. İşte bir alıntı.
# 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]Ancak kodu bir başlangıç noktası olarak kullanabilir ve PowerShell hakkında bildiklerimi kullanarak revize edebilirim. Demek istediğim, CodePilot gibi bir şey bile olsa yapay zeka kullanmanın çok faydalı olabileceği, ancak size PowerShell'i öğretmeyeceği ve etkili ve doğru kod yazmaktan hâlâ sizin sorumlu olduğunuzdur.
Bununla birlikte, ChatGPT'den benim için oluşturmasını istediğim iki işlevi daha size bırakayım. Konsol penceresinde bir fraktal görüntüleyecek bir fonksiyon istedim. Bir fonksiyon bir Julia kümesi yaratır.
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
}Orijinal ekran kodunu tersine çevirdim.
İtiraf etmeliyim ki matematik kısmını asla çözemezdim ve bu Mandelbrot fonksiyonu için kesinlikle doğrudur.
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
}Bu işlevleri alıp daha da geliştirmenize izin vereceğim.
Yapay zeka ile kodlamayı oldukça büyüleyici buluyorum. Yakında herhangi bir zamanda değiştirilme konusunda kesinlikle endişelenmiyorum. Ancak yapay zeka, kodlamanın zorlu işlerinin bir kısmını üstlenebilir ve hatta daha önce düşünmediğiniz bir yaklaşım bile sunabilir. Yapay zekayı ilham kaynağınız olarak düşünün.
Yapay zeka komut dosyası yazma veya VSCode'da CodePilot kullanma deneyimleriniz nelerdi?





