当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本

作者:精品下载站 日期:2024-12-14 08:07:41 浏览:11 分类:玩电脑

周五乐趣:使用 ChatGPT 编写 PowerShell 脚本


我一直在尝试最新的人工智能,ChatGPT。我问过它一些愚蠢的问题。

[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本

以及真正重要的问题。

[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本

但我发现最令人着迷的是使用 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 编写 PowerShell 脚本

不完全是我的想法。添加眨眼也不是我所希望的。

[玩转系统] 周五乐趣:使用 ChatGPT 编写 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
}

我颠倒了原来的显示代码。

[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本

我必须承认,我永远无法弄清楚数学部分,这在 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
  }

[玩转系统] 周五乐趣:使用 ChatGPT 编写 PowerShell 脚本

我会让你利用这些功能并进一步开发它们。

我发现用人工智能编码非常有趣。我绝对不担心很快就会被取代。但人工智能可以减轻编码中的一些繁重工作,甚至可能提供一种你从未考虑过的方法。将人工智能视为灵感的缪斯。

您在 VSCode 中编写 AI 脚本或使用 CodePilot 的经历如何?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯