Friday, March 29, 2024
HomePowershellPowerShell Scripting with ChatGPT • The Lonely Administrator

PowerShell Scripting with ChatGPT • The Lonely Administrator


I’ve been attempting out the newest AI, ChatGPT. I’ve requested it foolish questions.

In addition to the actually necessary questions.

do dogs dream?

However what I discover most fascinating is utilizing ChatGPT to jot down PowerShell code. I gave the AI this instruction: “Write a PowerShell operate that may show “Hi there, World” within the heart of the display in alternating colours. Embrace a parameter to have the message blink.”. After a number of seconds, it generated this code.

operate Present-HelloWorld {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateSet("On", "Off")]
        [string]$Blink
    )

    # Clear the display
    Clear-Host

    # Set the textual content colours to alternate between purple and inexperienced
    $colours = @("Crimson", "Inexperienced")
    $colorIndex = 0

    # Set the cursor place to the middle of the display
    $x = (Get-Host).UI.RawUI.WindowSize.Width / 2
    $y = (Get-Host).UI.RawUI.WindowSize.Top / 2

    # Set the cursor place and show the message
    Write-Host "Hi there, World" -ForegroundColor $colours[$colorIndex] -NoNewline
    $host.UI.RawUI.CursorPosition = New-Object System.Administration.Automation.Host.Coordinates $x, $y

    # Toggle the textual content coloration and blink the message if the Blink parameter is about to "On"
    whereas ($Blink -eq "On") {
        Begin-Sleep -Milliseconds 500
        $colorIndex = ($colorIndex + 1) % 2
        Write-Host "`b" -NoNewline
        Write-Host "Hi there, World" -ForegroundColor $colours[$colorIndex] -NoNewline
    }
}

You could consider this course of as scripting with a good friend who would possibly nonetheless be studying PowerShell. To the AI’s credit score, it used a correct verb for the operate. It was good sufficient to appreciate clearing the display could be obligatory. And the scripting language is what I’d anticipate from somebody simply getting began. Though, it didn’t make my blink possibility a change, or perhaps a boolean worth. However that’s straightforward sufficient to vary. I’ll run it with out blinking.

hello world

Not fairly what I had in thoughts. Including blinking additionally wasn’t what I hoped for.

hello world again

I requested ChatGPT to revise the operate.

Revise the operate to make use of ANSI escape sequences or $PSStyle. Every character ought to be a special coloration.

The consequence wasn’t significantly better, and didn’t run. Right here’s an excerpt.

  # Set the textual content colours to alternate between purple and inexperienced
        $colours = @("31", "32")
        $colorIndex = 0

        # Set the cursor place to the middle of the display
        $x = (Get-Host).UI.RawUI.WindowSize.Width / 2
        $y = (Get-Host).UI.RawUI.WindowSize.Top / 2

        # Set the cursor place and show the message
        Write-Host "H" -NoNewline
        Write-Host "e" -NoNewline -ForegroundColor $colours[$colorIndex]
        $colorIndex = ($colorIndex + 1) % 2
        Write-Host "l" -NoNewline -ForegroundColor $colours[$colorIndex]
        $colorIndex = ($colorIndex + 1) % 2
        Write-Host "l" -NoNewline -ForegroundColor $colours[$colorIndex]
        $colorIndex = ($colorIndex + 1) % 2
        Write-Host "o" -NoNewline -ForegroundColor $colours[$colorIndex]
        $colorIndex = ($colorIndex + 1) % 2
        Write-Host ", " -NoNewline -ForegroundColor $colours[$colorIndex]

However I may use the code as a place to begin and revise it with what I learn about PowerShell. The purpose I’m making is that utilizing AI, even one thing like CodePilot, generally is a nice assist, but it surely received’t train you PowerShell, and you’re nonetheless answerable for writing efficient and correct code.

That mentioned, let me go away you with two extra features I requested ChatGPT to create for me. I needed a operate to show a fractal within the console window. One operate creates a Julia set.

operate Present-JuliaSet {
    [cmdletbinding()]
    param (
      [double]$cReal = -0.8,
      [double]$cImag = 0.156,
      [int]$width = 80,
      [int]$peak = 24,
      [int]$maxIterations = 256
    )

    # Create the Julia set operate
    operate julia {
      param (
        [double]$cReal,
        [double]$cImag,
        [int]$width,
        [int]$peak,
        [int]$maxIterations
      )

      # Calculate the pixel values for the Julia set
      $pixels = for ($y = 0; $y -lt $peak; $y++) {
        for ($x = 0; $x -lt $width; $x++) {
          # Map the pixel coordinates to the complicated airplane
          $zReal = [math]::Spherical((($x / $width) * 3.5) - 2.5, 2)
          $zImag = [math]::Spherical((($y / $peak) * 2) - 1, 2)

          # Iterate the Julia set equation
          $iteration = 0
          whereas (($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 variety of iterations as a pixel worth
          $iteration
        }
      }

      # Print the Julia set to the console
      for ($y = 0; $y -lt $peak; $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 ""
      }
    }

   # Name the Julia set operate
  julia -cReal $cReal -cImag $cImag -width $width -height $peak -maxIterations $maxIterations
}

I inverted the unique show code.

julia fractal in a PowerShell console

I’ve to confess, I’d by no means have found out the mathematics half, and that’s undoubtedly true within the Mandelbrot operate.

operate Present-MandelbrotSet {
    [cmdletbinding()]
    param (
      [int]$width = 80,
      [int]$peak = 24,
      [int]$maxIterations = 256
    )

    # Create the Mandelbrot set operate
    operate mandelbrot {
      param (
        [int]$width,
        [int]$peak,
        [int]$maxIterations
      )

      # Calculate the pixel values for the Mandelbrot set
      $pixels = for ($y = 0; $y -lt $peak; $y++) {
        for ($x = 0; $x -lt $width; $x++) {
          # Map the pixel coordinates to the complicated airplane
          $cReal = [math]::Spherical((($x / $width) * 3.5) - 2.5, 2)
          $cImag = [math]::Spherical((($y / $peak) * 2) - 1, 2)
          $zReal = 0
          $zImag = 0

          # Iterate the Mandelbrot set equation
          $iteration = 0
          whereas (($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 variety of iterations as a pixel worth
          $iteration
        }
      }

      # Print the Mandelbrot set to the console
      for ($y = 0; $y -lt $peak; $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 ""
      }
    }

    # Name the Mandelbrot set operate
    mandelbrot -width $width -height $peak -maxIterations $maxIterations
  }
mandelbrot fractal in a powershell console

I’ll allow you to take these features and develop them additional.

I discover coding with an AI fairly fascinating. I undoubtedly don’t fear about being changed any time quickly. However the AI can take a few of the grunt work from coding and will even supply an strategy that you just hadn’t even thought of. Consider the AI as a muse for inspiration.

What have been your experiences scripting an AI or utilizing CodePilot in VSCode?


Learn PowerShell Scripting in a Month of Lunches, 2nd Ed. MEAP


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments