• Coding with AI pt. 2

    Author
    Topic
    #2655760

    Hey Y’all,

    I thought I’d conduct another test of using AI to code PowerShell. This time though I fed Chat-GPT an existing working PS script, then asked it to suggest improvements.

    Once again I’ll prefix this by saying I’m using the Free version (3.5).

    Here are the results:

    ----- Junk - already doing it that way -----
    # Avoid unnecessary file operations
    $logData = Get-Content -Path $LogFile
    
    ---- Original code ---
    $LogData = Get-Content -Path "$LogFile"
    =================================================
    
    -----Good----
    # Optimize loops using pipeline operations
    $failedBackup = $logData | Where-Object {$_ -like "*Backup Failed*"}
    $status = if ($failedBackup) { "At least one Backup Failed!" } else { "All Backups Successful!" }
    
    ---- Original code ---
      For($Cntr=0 ; $Cntr -lt ($LogData.count) ; $Cntr++) {
    
        if( $($LogData[$Cntr]) -Like "*Backup Failed*") {
          $BGColor = "Red"
          $Status  = "At least one Backup Failed!"
        }
      
        $TBMsg += "$($LogData[$cntr])`n"
    
        If ($Status -like "*At least*") {
          $TBMsg += "nnFor Details See:`n $LogPath"
        }
      
      } #End For ($Cntr...
      
    ----- Refactored Code -----
      $failedBackup = $logData | 
         Where-Object {$_ -like "*Backup Failed*"}
    
      if ($null -eq $failedBackup) { 
        $Status  = "All Backups Successful!" 
        $BGColor = "Green"  
      } 
      else { 
        $Status   ="At least one Backup Failed!" 
        $BGColor  = "Red"
        $LogData += "nFor Details See:n $LogPath"
      }
    ===============================================
    
    -----Good----
    # Minimize function calls by inlining simple functionality
    $dateTimeStr = "$(Get-Date -Format 'MM/dd/yy HH:mm:ss')"
    ----- Original Code ----
      $(Get-Date).ToString("MM/dd/yy") + " " +
      $(Get-Date).ToString("HH:mm:ss")
    =================================================
    
    ---- Junk - It's a text file no need to filter -----
    # Use -Filter parameter instead of Where-Object
    $IFWCmds = Get-Content -Path $(Join-Path @JPArgs) -Filter "*.txt"
    =====================================================
    
    -----Junk - Didn't work! Incorrect Syntax ----
    # Reduce unnecessary variable assignments
    $elapsed = ($log -match "Elapsed")[0] -split ']' | Select-Object -Last 1 -replace '\s'
    $backup = ($log -match "Source File:")[0] -split ']' | Select-Object -Last 1 -trim
    ---- Original Code -----
      $Elapsed = ($log -match "Elapsed")[0]
      $Elapsed = (($Elapsed).Split(']')[1]).Trim().Split(' ')[1]
      $Backup  = ($log -match "Source File:")[0]
      $Backup  = ($Backup).Split(']')[1].Trim()
      
    ----- Refactored Code using basic idea -----
      $Elapsed = 
       ($log -match "Elapsed")[0].Split(']')[1].Trim().Split(' ')[1]
      $Backup  = 
       ($log -match "Source File:")[0].Split(']')[1].Trim() 
    ===============================================
    
    ----- Junk - already using ForEach ------
    # Use foreach instead of ForEach-Object for arrays
    foreach ($cmd in $IFWCmds) {
        # Your backup execution logic here
    }
    ===============================================
    
    ----- Junk - already using Try/Catch ------
    # Use try/catch for error handling instead of setting global preference
    try {
        $NICs | Enable-NetAdapter 
        "$(Get-DTStr) Network reactivated!" >> $LogFile
    }
    catch {
        "$(Get-DTStr) Error: Network Not reactivated!`n$Error" >> $LogFile
    }
    ==================================================
    
    ----- Junk - the program is designed to turn off the computer after backup -----
    # Avoid unnecessary stopping of the computer
    # Stop-Computer
    

    Comments: Once again AI failed to be consistently accurate. It did make some very good suggestions. However, you still need someone who knows the language and logic to implement them. Also note it’s still hallucinating suggesting changes that are identical to the supplied code.

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!
    Computer Specs

    2 users thanked author for this post.
    Reply To: Coding with AI pt. 2

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: