Read computer name from folder and create new if unique

lupinlicious 131 Reputation points
2022-01-04T09:43:36.823+00:00

Dear all,

I might be asking for too much now, but I have not the slightest clue on how to proceed or neither am I able to find relative information.

What I'd like to achieve from the script (attached as .txt) would be to read from the folder "C:\DeploymentShare$\Computers" and only create new computer names if there is no match.
If the computer name is unique it will create the computer name as a simple text file.

So for instance, what should happen:

  1. From WindowsPE a PowerShell script will run asking for creating a computer name.
  2. A user enters a computer name like: F16ABCD.
  3. The script recognize that the computer name F16ABCD already exists in "C:\DeploymentShare$\Computers" and will not proceed until the user change the name.
  4. Same user enters a new computer name like: F16ABCE
  5. The script looks on "C:\DeploymentShare$\Computers" and does not find F16ABCE and create an entry "C:\DeploymentShare$\Computers\F16ABCE.txt" and proceeds with the installation.

Is there anyone here that would be so kind and help me with this and I'd be truly grateful for this

162078-set-osdcomputername.txt

Thaaanks

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,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2022-01-05T20:21:34.767+00:00

    How about something like this (minus all the irrelevant forms coding)?

    function Set-OSDComputerName {
        $ErrorProvider.Clear()
        if ($TBComputerName.Text.Length -eq 0) {
            $ErrorProvider.SetError($GBComputerName, "Please enter a computer name")
        }
        else {
            if ($TBComputerName.Text.Length -gt 7) {
                $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 6 characters")
            }
            else {
                if ($TBComputerName.Text -notmatch '^[F][0-9]{2,2}[a-zA-Z]{4,4}$') {
                    $ErrorProvider.SetError($GBComputerName, "Computer name must be named with <F> and <numbers> and <four random letters>")
                }
    
                else {                       
                    $OSDComputerName = $TBComputerName.Text -replace "[\[\]:;|=+*?<>\/,]+", ''
    
                    if (Test-Path "C:\SomeDirectory\$OSDComputerName") {
                        $ErrorProvider.SetError($GBComputerName, "Computer name already exists. Choose another name")
                    }
                    else {
                        New-Item -Path "C:\SomeDirectory\$OSDComputerName" -ItemType File
                    }
                    $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
                    $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)"
                    $Form.Close()
                }
            }
        }
    }
    

2 additional answers

Sort by: Most helpful
  1. Limitless Technology 39,351 Reputation points
    2022-01-05T13:58:09.013+00:00

    Hi @lupinlicious

    Thank you for your question and for getting in touch. My name is Samuel and I would be more than happy to help you with your query.

    This type of problem has already been documented in our database, you can follow more details about it in the article below:

    https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-read-computer-names-from-a-text-file-and-then-verify-whether-or-not- those-computers-still-exist/

    If the answer is helpful, please vote positively and accept the answer.

    Regards,
    Samuel


  2. AlexZhu-MSFT 5,551 Reputation points Microsoft Vendor
    2022-01-06T00:52:29.84+00:00

    Hi,

    Have Changed the script slightly and done a quich lab test. Here's the script (different part, all the remaining part keeps unchanged) and hope it helps.

    Note: (1) it seems UNC path is required for PXE client; (2) make sure the boot image supports Powershell.

     function Set-OSDComputerName {  
         $ErrorProvider.Clear()  
         if ($TBComputerName.Text.Length -eq 0) {  
             $ErrorProvider.SetError($GBComputerName, "Please enter a computer name")  
         }  
         else {  
             $OSDComputerName = $TBComputerName.Text -replace "[\[\]:;|=+*?<>\/,]+", ''  
             $temp_file = $temp_path + $OSDComputerName + ".txt"  
                    
             if ($OSDComputerName.Length -gt 7) {  
                 $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 7 characters")  
             }  
             elseif ($OSDComputerName -notmatch '^[F][0-9]{2,2}[a-zA-Z]{4,4}$') {  
                 $ErrorProvider.SetError($GBComputerName, "Computer name must be named with <F> and <2-digit numbers> and <four letters>")  
             }  
             elseif (test-path $temp_file) {  
    			$ErrorProvider.SetError($GBComputerName, "Computer name already exist, please enter a new one")  
             }  
             else {  
                 write-host $OSDComputerName  
                 "anything" | out-file -filepath $temp_file  
    #            $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment   
    #            $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)"  
                 $Form.Close()  
               }  
         }  
     }  
      
      
    #$command = "net use m: \\10.1.1.2\d\alex\ /user:<domain\user> <password>"  
    #write-host $command  
    #invoke-expression $command  
      
    #$temp_path = "m:\"  
    $temp_path = "\\10.1.1.2\d\alex\"  
    		  
      
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")   
      
    $Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider  
      
      
    $Form = New-Object System.Windows.Forms.Form      
    $Form.Size = New-Object System.Drawing.Size(300,300)    
    

    162619-osd-hostname.png

    Alex

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html