United Kingdom | Using AI to Standardise Scripts

Chris Threlfall - 22.07.202520250722

United Kingdom | Using AI to Standardise Scripts

Join our community of 1,000+ IT professionals, and receive tech tips and updates once a week.

Using AI to Standardise Scripts

United Kingdom | Using AI to Standardise Scripts

Introduction

As consultants, we often inherit script libraries from colleagues past and present, customers, and often, the internet. From time to time this results in diluted knowledge of how the script functions, and without that knowledge, making improvements to the scripting could be daunting and time-consuming. Leveraging AI, particularly tools like Copilot, can significantly streamline this process. This article explores the benefits of using AI to standardise PowerShell scripts and offers additional recommendations to further enhance this approach.

Current Approach

I have been using Copilot to standardise some PowerShell scripts with a focus on removing hard-coded items such as directory addresses, output directories, usernames, and tenant IDs. The goal is to ensure all scripts: 

  • Look for related files in the working directory
  • Output to the same directory
  • Prompt for user information, such as admin account login, during script execution
  • Import or install all relevant modules as part of the script
  • Establish necessary connections (e.g., Connect-MicrosoftTeams)
  • Output log files to the directory to maintain a paper trail
  • Update notes in the script to accurately describe each part
  • Create README files for all scripts to facilitate ease of use by other consultants

Benefits of This Approach

  • Consistency: By removing hard-coded values and standardising the structure, scripts become more uniform, making them easier to understand and maintain
  • Flexibility: Scripts that dynamically locate files and prompt for user input can be reused in different environments without modification
  • Collaboration: Detailed notes and README files make it easier for other consultants to understand and use the scripts, fostering better teamwork
  • Error Reduction: Automating the import of modules and establishing connections reduces the risk of missing dependencies or incorrect configurations
  • Accountability: Outputting log files provides a clear record of actions taken by the script, which is essential for troubleshooting and auditing

Basic Script Uplift Example

Here is a basic example of how a script designed to do a specific task “on the fly” could be uplifted to meet better operating standards and become part of a shared toolset: 

Original Script:

United Kingdom | Using AI to Standardise Scripts

Example AI Prompt

Here is an example of a prompt that you could use to uplift the original script 

  • Update this script so that it imports and connects to the correct PowerShell Modules without using any hard-coded usernames or passwords in the script, it will be used in multiple tenants, prompt for modern auth login using the correct “Connect-” command, also remove any hard coded file locations and have the script import the CSV file from the working directory of the script. Also, export a CSV with the new UPNs of the users, keep a log file of everything the script is doing and export a txt log file into the working directory, a success/fail for each user should be shown on-screen as well, Add error handling for any failures and add an explanation for any failures such as user not having a license or account not found. Finally, create a Read Me description of the script and to use it and how it functions.” 

Updated Script

Here we can see that Copilot has annotated the script, removed the hard-coded file path, and added all of the requested functions: 

United Kingdom | Using AI to Standardise Scripts

*AI-generated scripts should be fully tested in a development environment before running in a production environment.

Readme File

Copilot also generated the following Read Me file, this will allow the script to easily be passed to colleagues and enable them to have an understanding of its functions and requirements.

PowerShell Script for Microsoft 365 Tenant-to-Tenant UPN and Email Address Update 

Description 

This PowerShell script is designed to update the User Principal Name (UPN) and email address for users in a Microsoft 365 tenant. It prompts for modern authentication login, imports user data from a CSV file located in the working directory, and updates the UPN and email address for each user. The script logs all actions and errors, providing a detailed log file and a CSV file with the updated UPNs. 

How to Use

Prerequisites 

  • Ensure you have the ExchangeOnlineManagement module installed
  • Ensure you have the necessary permissions to update user properties in the Microsoft 365 tenant
  • Prepare a CSV file named UPNChanges.csv in the working directory with the following columns:
  • OLDUPN: The current UPN of the user
  • NEWUPN: The new UPN to be assigned to the user
  • NEWEMAILADDRESS: The new email address to be assigned to the user

Steps to Execute the Script

  • Open PowerShell: Launch PowerShell with administrative privileges
  • Navigate to the Working Directory: Change the directory to the location where the script and UPNChanges.csv file are stored
    • cd pathtoyourworkingdirectory
  • Run the Script: Execute the script by typing its name
    • .YourScriptName.ps1
  • Authentication: The script will prompt you to enter your User Principal Name (UPN) for modern authentication. Follow the prompts to authenticate
  • Processing: The script will read the UPNChanges.csv file and process each entry, updating the UPN and email address for each use
  • Logging: The script will log all actions and errors to a file named UPNChangeLog.txt in the working directory
  • Output: The script will create a CSV file named UpdatedUPNs.csv in the working directory, containing the updated UPNs and email addresses

Error Handling

  • The script includes error handling to provide explanations for any failures, such as the user not having a license or account not found
  • Success and failure messages for each user will be displayed on the screen

Example CSV Format

 

OLDUPN NEWUPN NEWEMAILADDRESS 
user1@oldtenant.com user1@newtenant.com user1@newtenant.com 
user2@oldtenant.com user2@newtenant.com user2@newtenant.com 

Log File

The log file UPNChangeLog.txt will contain detailed information about each action performed by the script, including success and error messages.

Recommendations for Further Enhancement

  • Automated Testing: Implement automated testing for your scripts using tools like Pester (Quick Start | Pester) This ensures that any changes or updates to the scripts do not introduce new errors
  • Version Control: Use a version control system like Git (What Is Git Version Control? – GeeksforGeeks) to track changes to your scripts. This allows you to revert to previous versions if needed and collaborate more effectively with others
  • Code Review: Incorporate a code review process where peers can review and suggest improvements to your scripts. This can help catch potential issues and improve the overall quality of the code
  • Security Best Practices: Ensure that your scripts follow security best practices, such as avoiding the use of plain-text passwords and implementing proper error handling

Conclusion

Using AI to standardise PowerShell scripts offers numerous benefits, including improved consistency, flexibility, collaboration, error reduction, and accountability. By incorporating additional practices such as automated testing, version control, code review, documentation generation, and security best practices, you can further enhance the effectiveness and reliability of your scripts. Embracing these strategies will not only streamline your workflow but also ensure that your scripts are robust, secure, and easy to use. If you need guidance or support in optimising your script processes, don’t hesitate to contact us we’re here to help.

Hungry for more?

If you’re waiting for a sign, this is it.

We’re a certified amazing place to work, with an incredible team and fascinating projects – and we’re ready for you to join us! Go through our simple application process. Once you’re done, we will be in touch shortly!

Who is Insentra?

Imagine a business which exists to help IT Partners & Vendors grow and thrive.

Insentra is a 100% channel business. This means we provide a range of Advisory, Professional and Managed IT services exclusively for and through our Partners.

Our #PartnerObsessed business model achieves powerful results for our Partners and their Clients with our crew’s deep expertise and specialised knowledge.

We love what we do and are driven by a relentless determination to deliver exceptional service excellence.

United Kingdom | Using AI to Standardise Scripts

Insentra ISO 27001:2013 Certification

SYDNEY, WEDNESDAY 20TH APRIL 2022 – We are proud to announce that Insentra has achieved the  ISO 27001 Certification.