Tags

, , , , , , , , , , , , , ,

Welcome back to the ChatGPT 101 series! In the last post, I introduced readers to OpenAI’s language model called ChatGPT, which uses machine learning algorithms to generate human-like text. The post highlights the potential applications of ChatGPT in code generation and translation, including generating code based on a prompt and translating code between programming languages. In this post, we’ll focus on using ChatGPT for code generation.

Code Generation with ChatGPT:

ChatGPT can generate code in a variety of programming languages, including Python, JavaScript, and PowerShell. Let’s take a look at an example of using ChatGPT to generate a PowerShell script to connect to a remote server and troubleshoot a TCP/IP routing issue, all using natural language.

Prompt:
Create a PowerShell script that will connect to a remote server named [TARGET-1] and then diagnose and troubleshoot any TCP/IP routing issues that may exist between [TARGET-1] and another server named [TARGET-2].

Response:

$targetServer = "TARGET-1"
$username = "domain\username"
$password = "password"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $securePassword)

Enter-PSSession -ComputerName $targetServer -Credential $cred

$targetServerIP = Resolve-DnsName -Name $targetServer | Select-Object -ExpandProperty IPAddress
$target2ServerIP = Resolve-DnsName -Name "TARGET-2" | Select-Object -ExpandProperty IPAddress

Test-NetConnection -ComputerName $target2ServerIP -TraceRoute -InformationLevel Detailed

ChatGPT is smart enough to include in the script a prompt for the credentials needed to connect to [TARGET-1], then establishes a PowerShell session with that remote server. It then uses the Resolve-DnsName cmdlet to resolve the IP addresses of both [TARGET-1] and [TARGET-2]. Finally, it runs the Test-NetConnection cmdlet with the -TraceRoute parameter to diagnose any TCP/IP routing issues between [TARGET-1] and [TARGET-2].

Conclusion:

We’ve seen how ChatGPT’s language model can be leveraged to generate complex and useful PowerShell scripts, such as the one we created to connect to a remote server and troubleshoot a TCP/IP routing issue. What’s impressive is not just the syntax and logic of the code itself, but also the ability of the model to understand the context of the problem and prompt for missing information, like the credentials required for remote access. This shows the power of natural language processing and machine learning, and how it can augment the work of system administrators, enabling them to focus on higher-level tasks and delegate some of the more mundane and time-consuming ones to AI-powered tools like ChatGPT.

So if you’re a sysadmin looking for ways to streamline your workflow and automate repetitive tasks, don’t hesitate to give ChatGPT a try. You may be surprised by the quality and efficiency of the code it generates, and the intelligence it demonstrates in understanding your needs and requirements.

In the next post, we will cover how to use ChatGPT for code translation, such as rewriting PowerBuilder code into a .NET app. Stay tuned!