Merge pull request #150 from JonasKloseBW/2412.3-Set-Computer-Name-through-SerialNumber-List

2412.3 set computer name through serial number list
This commit is contained in:
rbalsleyMSFT
2025-02-27 10:04:08 -08:00
committed by GitHub
@@ -222,6 +222,7 @@ if (Test-Path -Path $PPKGFolder){
$UnattendFolder = $USBDrive + "unattend\" $UnattendFolder = $USBDrive + "unattend\"
$UnattendFilePath = $UnattendFolder + "unattend.xml" $UnattendFilePath = $UnattendFolder + "unattend.xml"
$UnattendPrefixPath = $UnattendFolder + "prefixes.txt" $UnattendPrefixPath = $UnattendFolder + "prefixes.txt"
$UnattendComputerNamePath = $UnattendFolder + "SerialComputerNames.csv"
If (Test-Path -Path $UnattendFilePath){ If (Test-Path -Path $UnattendFilePath){
$UnattendFile = Get-ChildItem -Path $UnattendFilePath $UnattendFile = Get-ChildItem -Path $UnattendFilePath
If ($UnattendFile){ If ($UnattendFile){
@@ -234,6 +235,12 @@ If (Test-Path -Path $UnattendPrefixPath){
$UnattendPrefix = $true $UnattendPrefix = $true
} }
} }
If (Test-Path -Path $UnattendComputerNamePath){
$UnattendComputerNameFile = Get-ChildItem -Path $UnattendComputerNamePath
If ($UnattendComputerNameFile){
$UnattendComputerName = $true
}
}
#Ask for device name if unattend exists #Ask for device name if unattend exists
if ($Unattend -and $UnattendPrefix){ if ($Unattend -and $UnattendPrefix){
@@ -278,6 +285,24 @@ if ($Unattend -and $UnattendPrefix){
$computername = Set-Computername($computername) $computername = Set-Computername($computername)
Writelog "Computer name set to $computername" Writelog "Computer name set to $computername"
} }
elseif($Unattend -and $UnattendComputerName){
Writelog 'Unattend file found with SerialComputerNames.csv. Getting name for current computer.'
$SerialComputerNames = Import-Csv -Path $UnattendComputerNameFile.FullName -Delimiter ","
$SerialNumber = (Get-CimInstance -Class Win32_Bios).SerialNumber
$SCName = $SerialComputerNames | Where-Object { $_.SerialNumber -eq $SerialNumber }
If ($SCName) {
[string]$computername = $SCName.ComputerName
$computername = Set-Computername($computername)
Writelog "Computer name set to $computername"
} else {
Writelog 'No matching serial number found in SerialComputerNames.csv. Setting random computer name to complete setup.'
[string]$computername = ("FFU-" + (-join ((48..57) + (65..90) + (97..122) | Get-Random -Count 11 | ForEach-Object { [char]$_ })))
$computername = Set-Computername($computername)
Writelog "Computer name set to $computername"
}
}
elseif($Unattend) { elseif($Unattend) {
Writelog 'Unattend file found with no prefixes.txt, asking for name' Writelog 'Unattend file found with no prefixes.txt, asking for name'
[string]$computername = Read-Host 'Enter device name' [string]$computername = Read-Host 'Enter device name'