create an array or text file with bad words

lupinlicious 131 Reputation points
2022-01-08T18:34:23.833+00:00

Hello all, it's me again :)

I just realized that I might need an array or maybe having a text file with bad words, so a user cannot be too creative with their name giving.
It's only the last four letters in the computer name that needs some limitation.

I could create predefined computer names in this location: C:\DeploymentShare$\Computers and add for an example F21DAMN
But if someone creates the computer name F20DAMN, then MDT will continue to install.

The script I'm using: 163330-osdcomputername.txt

I found the following code and only modified the file location and added some bad words in a text file: 163386-file-with-bad-words.txt

Then I added the function to this line, but it does not work, probably all wrong and too good to be true:
$temp_file = $temp_path + $OSDComputerName + ".txt" + $text

I also got some errors: 163298-errors.txt

...and once again I must ask for some help on how to achieve this

Thank you!

Microsoft Deployment Toolkit
Microsoft Deployment Toolkit
A collection of Microsoft tools and documentation for automating desktop and server deployment. Previously known as Microsoft Solution Accelerator for Business Desktop Deployment (BDD).
828 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,441 Reputation points MVP
    2022-01-09T09:35:23.74+00:00

    Hi @lupinlicious ,

    I am not sure what is the content of variable $TBComputerName. Is the content the computer name? Or is the computer name in $TBComputerName.text ...
    Maybe you can check this and post the result here.

    Please try this:

    function Set-OSDComputerName {  
          
        $ErrorProvider.Clear()  
        $lastFour = $TBComputerName.text.substring($TBComputerName.text.length -4,4) # Get the last 4 characters of computer name  
        $badstring = select-string - path C:\deploymentshare$\bad_words.txt -pattern $lastfour  
               if ($badstring)  {  
                 $ErrorProvider.SetError($GBComputerName, "Computer name contains a bad word, not acceptable")  
                 return  
             }  
           }  
      
    function Set-OSDComputerName {  
      
        if (Select-String -Path "C:\DeploymentShare$\bad_words.txt" -Pattern $TBComputerName.text.substring($TBComputerName.text.Length - 4, 4)) {  
            $ErrorProvider.SetError($GBComputerName, "Computer name contains a bad word, not acceptable")  
            return  
        }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,441 Reputation points MVP
    2022-01-08T19:13:28.117+00:00

    Hi @lupinlicious ,

    it's another if for your script. This is just to show how it works.
    Create a txt file with bad words (4 characters). Each word in one line. For instance:

    # content of badwords.txt  
    test  
    fuck  
    abcd  
    

    This is just an example how this will work. You have to modify this to your needs!

    $a = "F12THJK" # contains the computername - no bad word  
    #$a = "F12FUCK" # for testing a second name -> bad word  
    if (Select-String -Path badwords.txt -Pattern $a.substring($a.length - 4, 4)) { # check if the 4 characters string is in the badwords.txt   
        Write-Output "Found a bad string in computer name"} else { # if it's not in list  
            Write-Output "Computer name is ok"}  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 44,776 Reputation points
    2022-01-08T20:36:11.597+00:00

    Speaking as one who was an e-mail administrator and manager for a multi-national company I can tell you that such a list can be quite long. You'll have to deal not only with words found in a dictionary, but also with slang, languages other than English, abbreviations, acronyms, and maybe such things as words found to be offensive (or maybe just insensitive) to certain cultures, religions, races, etc.

    Your list will be considerably shorter because you're limiting the length of the string and the characters that the string may contain. That, and the number of times you have to perform a lookup of a string will be limited, means using an array would probably be satisfactory. If you were doing lookups at much greater rates than a hash would be preferable.

    If you have access to a UNIX machine, have a look in /usr/dict/words, it's a good place to start.

    Just to give you an idea of what you may NOT find in a dictionary is "FOAD" (Fk Off And Die"), "ESAD" (Eat St And Die). What about a user named Richard wanting to name his machine "Dick"? Or a Dutch user with a given name of "Cock"? Get the scope of the problem? :-)