Avoid UserNames Becoming Bad Words with a Little AI and PowerShell

Avoid UserNames Becoming Bad Words with a Little AI and PowerShell

Maybe your business has been really good. So good, in fact, that now your acquiring other businesses around the world, and you're facing support for more languages. Welcome to having to join meetings at crazy hours due to all those new time zones you never knew about.

And suppose your organization has relied on a process or policy for creating user account names for years, and all has been fine, until one day you get a message from HR that your process created a username that is considered offensive in the user's native language.

Uh oh? What now?

What now - is you pick up the phone and call AI for some help. Well, not really. But in this example, we'll glue together PowerShell and OpenAI using the PSAI PowerShell module, written by Microsoft MVP Doug Finke . If you're not familiar with this module, but you use PowerShell fairly often, you may have used his ImportExcel module (if you haven't - you should!). Anyhow, let's continue.

I won't cover all the details about setting up PSAI, but in short: you'll need an OpenAI API account, an API token and an environment variable named $env:OpenAIKey to store the API token.

By the way, this example will work on any OS platform that supports PowerShell and PSAI module (Windows, MacOS, and Linux, basically).

We'll start with a function that creates a username from inputs for first and last name. The pattern is first-initial + last name, such as "John Smith" would return "JSmith".

function New-UserName {
    param( $FirstName, $LastName)
    $FirstName.Trim().Substring(0,1) + $LastName.Trim()
}        

If you test that with "John" "Smith" or "Jane" "Smith" it will return "JSmith". There's obviously no input validation so you could break this pretty fast, but it's just for demonstration. Easy enough.

PS:> New-UserName "Julie" "Smith"
PS:> JSmith        

Simple stuff. But you can easily think of combinations that would generate offensive or embarrassing phrases just in English. Maybe, like me, you've left a few hanging around in your test environments and realized it while showing a demo on a big screen to a large group of people. Ah, yeah, good times. Anyhow, I would provide more examples but I need to stay employed too.

That was using just one language (English), but you can scale that concern out to every other language in your organization and you get the idea.

Let's expand that function a bit to leverage PSAI features so we can ask OpenAI how the results are before we throw them into an identity platform (AD, Entra, Okta, etc.).

We'll add a -FormatPattern parameter to let you adjust the pattern when needed. Not really necessary, but I got bored, so there it is.

I'll wrap all this stuff inside a try/catch block so if it explodes or implodes, we'll just hand over the exception message through the mail slot at the end.

Then we'll import the PSAI module and create a new agent, with custom instructions and a custom prompt.

function New-UserName {
	[CmdletBinding()]
	param (
		[parameter(Mandatory)][string]$FirstName,
		[parameter(Mandatory)][string]$LastName,
		[parameter()][string]$FormatPattern = "F,Ln" # or "Ln,F", "Fn.Ln"
	)
	try {
		Import-Module PSAI
		switch ($FormatPattern) {
			"Ff,Ln" {
                               # first 2 letters of first name + last name
				$UserName = $FirstName.Substring(0, 2).Trim() + $LastName.Trim()
			}
			"Fff,Ln" {
                               # first 3 letters of first name + last name
				$UserName = $FirstName.Substring(0, 3).Trim() + $LastName.Trim()
			}
			"Ln,F" {
                               # last name + first letter of first name
				$UserName = $LastName.Trim() + $FirstName.Substring(0, 1).Trim()
			}
			"Fn.Ln" {
                               # first name + "." + last name
				$UserName = $FirstName.Trim() + "." + $LastName.Trim()
			}
			default {
                               # first letter of first name + last name
				$UserName = $FirstName.Substring(0, 1) + $LastName.Trim()
			}
		}

		$prompt = "Determine if $UserName is offensive or embarrassing, and if so, in what language?"

		$instructions = "date: $(get-date) - check for offensive or embarrassing phrase - output JSON with keys for UserName, Language (English, Spanish, etc.), Is_Offensive (boolean), and Reason (Sexual, Racial, Gender, Nationality, etc.), Do not include comments."

		$agent = New-Agent -Instructions $instructions
		$agent | Get-AgentResponse -Prompt $prompt | ConvertFrom-Json

	} catch {
		Write-Error $_.Exception.Message
	}
}        

Now you can kick the tires and see how different first + last name patterns work out. You'll find that AI (actually LLM's) aren't 100% consistent, but they are improving every day. So you can expect some patterns to be identified as IsOffensive ($True) when they might not be. I've also noticed that it will sometimes replace the UserName output value with "User" when IsOffensive is $True, but all of these anomalies may be due to the specific LLM model so try it with different models.

You can comment the ConvertFrom-Json statement if you want the JSON result instead. Otherwise, this should be easy to follow, even for my horrific coding habits.

This is a very simple and crude example, but it should demonstrate some interesting things you can do with AI and PowerShell. Cheers!

要查看或添加评论,请登录

David Stein的更多文章

社区洞察