Chatgpt is going to take over the world! ….maybe…

This weekend I hopped aboard the beta test group train for ChatGPT and put it through its paces for Azure scripting. I am an advocate for automation, in most cases automation can be a plausible solution for mundane repeatable tasks that that most people find boring, to free us up to move on to more interesting things.

I used powershell as a basis for testing what ChatGPT can do with scripting info gathering for Azure. I thought i would first test reporting on VM’s. ChatGPT responded with the correct output and this would be fine for use as a basic script. This would be fine to use in an environment for today, but not a good long-term solution as AzureRM modules will be outdated as of February 29, 2024.

This time around i asked specifically for Chat GPT to use AZ modules instead and surprisingly it was able to re-write the script to use AZ modules and also provided some instructions to replace the subscription ID. This would be a great solution for a single subscription.

Now I was on a mission to ‘up the ante’ and try to stump it, this time increasing the difficulty to include all subscriptions. I was a bit perplexed that ChatGPT stated that it was using Azure CLI (AZ) modules; however, the script it wrote did not use Azure CLI and wrote powershell syntax as I specified.

Ok, Now I’ve been far to easy on this thing, it’s time for the ultimate test and turn the difficulty up to ’11’. This time around, I decided to ask ChatGPT for something in the realm of deployments. So I decided to ask it to write a powershell script to deploy an automation account in eastus and create a runbook script powershell script to get a list of all azure VMs in all subscriptions using the az modules and a runbook script in python to migrate the automation account to migrate a VM from eastus to westUS and clean up old machines. Much to my suprise, it worked! The script was too big to screenshot, so I’m posting it as a code block.

# Login to Azure account
Connect-AzAccount

# Define variables
$resourceGroupName = "AutomationRG"
$automationAccountName = "AutomationAccount"
$getVMsRunbookName = "GetAllVMsRunbook"
$migrateVMRunbookName = "MigrateVMRunbook"

# Create resource group
New-AzResourceGroup -Name $resourceGroupName -Location "East US"

# Create Automation Account
New-AzAutomationAccount -ResourceGroupName $resourceGroupName -Name $automationAccountName -Location "East US"

# Get all Azure subscriptions
$subscriptions = Get-AzSubscription

# Create a runbook script to get all VMs
$getVMsRunbookScript = @"
# Loop through each subscription
foreach ($subscription in $subscriptions) {
    # Select Azure subscription
    Set-AzContext -SubscriptionId $subscription.Id

    # Get all Azure Virtual Machines for this subscription
    $vms = Get-AzVM

    # Loop through each Virtual Machine and print its name
    foreach ($vm in $vms) {
        Write-Output "$($subscription.Name): $($vm.Name)"
    }
}
"@

# Create a runbook to get all VMs
New-AzAutomationRunbook -Name $getVMsRunbookName -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName -Type "PowerShell" -Content $getVMsRunbookScript

# Create a runbook script to migrate a VM
$migrateVMRunbookScript = @"
import os
import azure.mgmt.compute.models

def main():
    subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
    source_region = "eastus"
    destination_region = "westus"
    resource_group_name = "myResourceGroup"
    vm_name = "myVM"

    print("Migrating virtual machine '{}' from '{}' to '{}'".format(vm_name, source_region, destination_region))

    # Use the Azure CLI to create a new managed disk in the destination region
    os.system("az disk create --resource-group {} --name {}-os-disk --source {} --sku Standard_LRS --location {}".format(resource_group_name, vm_name, vm_name + "-os-disk", destination_region))

    # Use the Azure CLI to create a new virtual machine in the destination region using the managed disk
    os.system("az vm create --resource-group {} --name {} --image {} --os-disk-name {}-os-disk --use-unmanaged-disk --location {} --admin-username azureuser --generate-ssh-keys".format(resource_group_name, vm_name, vm_name + "-os-disk", vm_name, destination

We as human beings have a big moral dilemma ahead of us. We can either learn to live in harmony with AI, shun it, or start now to set boundary lines where AI is an acceptable use case and when it is not. I think that AI is going to be a commonly used practice in the tech industry regardless. We always try to find ways to do things quicker/faster/cheaper. The more you try to stop it, the more it will be demanded and argued to be used. Tech professionals have spent years of their life learning how to code and shape scripts to leverage repeatable business processes together to build the complex tech world that we have today…now it’s upon us that AI can merely do that with a simple query. We’ll have to ask ourselves if we’re “ok” with that fact and be able to pivot and shift with it or run faster than it.

I’m reminded of the story of John Henry vs. Steam Machine. John Henry was able to beat the machine, because the machine had limitations at 9 feet, John Henry was able to drive the drill to 14 feet….but sadly, later died of exhaustion trying to keep up with the machine. Eventually, machines became the societal norm as a replacement for manual labor intensive jobs. Somehow human beings always manage to find a way to make themselves useful in the world. I believe there will always be a need for humans to work, it just perhaps look a little different than what we’re used to. The world will turn regardless, we can either learn to get along with it, or get run over by it.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *