Generate Scripted Actions with ChatGPT
This topic is a sample of how to generate scripted actions with ChatGPT.
Note: Alternatively, you can download our prompt here. You can then copy/paste the prompts directly in ChatGPT or Bard.
Tip: You must be as specific as possible about what you want the script to accomplish. In addition, if you want the script to run as an Azure Runbook scripted action, you must include that in the prompts.
Step 1: Find an existing scripted action and use it as a sample.
In Nerdio Manager, navigate to Scripted Actions > Windows scripts.
Locate the scripted action you wish to work with. For example, select Optimize Microsoft Edge for WVD from the scripted action gallery.
In ChatGPT:
Input: Here is a sample scripted action from Nerdio Manager for Enterprise <body of the script>. I will provide you with series of prompts to fine tune this script.
Note: This is the body of the script that you selected above from the scripted action gallery.
Output: Great! Now that I have the script, I'm ready to assist you with fine-tuning it according to your prompts. Please go ahead and provide the series of prompts you mentioned.
Step 2: You provide the table from above as context.
Input: Here is a table describing the different global variables available in Scripted Actions: <table body as text> . I am not sure if we are going to need it but keep it in mind.
Output: Understood. These global variables can be used in the script to make it more dynamic and adaptable to different environments. Based on the script you've provided, it seems it doesn't need any of these variables as it's setting Microsoft Edge policies locally on the machine it's run on.
Step 3: The fun begins.
Input: Please add additional optimizations for the performance of Edge.
Output:
Step 4: Every scripted action must start with a comment line that includes a description of its functionality.
Input: Taking the changes that you have done to the scripted actions, please update its description.
Output:
Step 5: The next line can optionally include tags for organization of the scripted action.
For example: #tags: Nerdio, Preview.
Input: Please update the tags to include the new changes.
Output:
Step 6: Add some logging.
Input:
Using the variables syntax here "<# Variables: {
"VNetName": {
"Description": "VNet in which to create the temp VM. Can be same as AVD host vnet. Must be able to access the fslogix fileshare.",
"IsRequired": false
#>" please add a line to the script that logs that variable to Windows event logs. The variable is mandatory and will take a description.
Output:
Step 7: ChatGPT decided to show only the last "ask," so you are clarifying your need for the full code.
Input: Can you show me the full script.
Output:
Note: See Appendix A for the full source.
Step 8: You need to update the variable name.
For example, the current variable name in the example is VNetName.
Input: Please change VNetName to LogString.
Output:
Step 9: Ask ChatGPT to explain the script.
Tip: Once you have a Scripted Action that you are happy with, it is a best practice to ask ChatGPT to explain the script. This can expose bugs, logic errors, and typos.
Input: Please explain the script and identify potential errors or areas for improvement.
Output: In our case, ChatGPT generates an accurate explanation and identifies 7 areas for improvement.
Note: For the sake of brevity, the detailed feedback is not provided here. We now move straight to testing.
Step 10: Take the generated PowerShell script and put it in your test host pool.
Conclusion
Scripted Action can be used to automate tasks, deploy software, configure settings, and troubleshoot problems. With this article we leveraged ChatGPT to streamline the process of SA creation. We encourage everyone to continue to explore generating Scripted Action with ChatGPT and other AI technologies.
Appendix A
Here is the full source of the scripted action.
#description: (PREVIEW) Configures policy settings for Microsoft Edge to optimize performance in AVD
#tags: Nerdio, Preview, Edge, PerformanceOptimization, SleepingTabs, StartupBoost, DisableHardwareAcceleration, MaxRasterThreads, DisableBackgroundApps
<# Variables:
{
"VNetName": {
"Description": "VNet in which to create the temp VM. Can be same as AVD host vnet. Must be able to access the fslogix fileshare.",
"IsRequired": true
}
#>
<#
Notes:
This script configures policy settings for Microsoft Edge to optimize performance in Azure Virtual Desktop (AVD) environments.
Policies Set:
- Enable Sleeping Tabs ("sleep" inactive browser tabs)
- Enable Startup Boost (preload MS Edge in the background on login)
- Disable Hardware Acceleration (may improve performance on systems with limited resources)
- Set Maximum Raster Threads to 2 (limits the CPU usage of Edge, adjust based on system's CPU resources)
- Disable running background apps when Edge is closed (frees up resources when Edge is not in use)
#>
# Set registry settings
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge /v "SleepingTabsEnabled" /t REG_DWORD /d 1 /f
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge /v "StartupBoostEnabled" /t REG_DWORD /d 1 /f
# Additional performance optimizations
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge /v "HardwareAccelerationModeEnabled" /t REG_DWORD /d 0 /f
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge /v "MaxRasterThreads" /t REG_DWORD /d 2 /f
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge /v "ContinueRunningBackgroundAppsWhenMicrosoftEdgeIsClosed" /t REG_DWORD /d 0 /f
# Log VNetName to the Windows Event Log
$VNetName = Get-Variable -Name "VNetName" -ValueOnly
if (![string]::IsNullOrEmpty($VNetName)) {
Write-EventLog -LogName Application -Source "Nerdio Script" -EntryType Information -EventID 1000 -Message "VNetName is: $VNetName"
}
else {
Write-Host "VNetName variable is not set."
}
Comments (0 comments)