Get Computername from SerialNumber

Hello,

I would like to be able to run a script so that I can find out to which Computer a given Serialnumber belongs to.

unfortunately my script doesnt work. Could you guys give me a little input?

I would like to query all computers in my domain /OU

 

Thank you so much!

 

Function Get-computername {

[cmdletbinding()]

param(

[string]$ComputerName,

[string]$serial

)

Begin {

foreach($computer in $ComputerName){

$sn = Get-WmiObject Win32_Bios -Filter “SerialNumber=‘$serial’” -ComputerName $computer

}

$result = Get-ADComputer -identity $computer

Write-Output “$result”

}

Process { }

End {}

}

 

Get-computername -ComputerName Test1, Test2 -serial SerialTest1


 

How about something like this

$Servers = "Srv1","Srv1","Srv3"

$GetData = {

[PSCustomObject]@{

BiosInfo = $(Get-WmiObject Win32_Bios)

}

}

$Results = Invoke-Command -ComputerName $Servers -ScriptBlock $GetData -ErrorAction SilentlyContinue

$Results | ForEach-Object{

$ComputerName = $_.PSComputerName

$_.BiosInfo |

ForEach-Object{

[PSCustomObject]@{

ComputerName = $ComputerName

SMBIOSBIOSVersion = $_.SMBIOSBIOSVersion

Manufacturer = $_.Manufacturer

Name = $_.Name

SerialNumber = $_.SerialNumber

Version = $_.Version

}

}

} | sort-object | Export-Excel -Path $xlsxFile -WorkSheetname BIOS -AutoSize -AutoFilter -FreezeTopRow -BoldTopRow


            

The issue with your original script is that this block:

$result = Get-ADComputer -identity $computer
Write-Output “$result”
is outside the foreach loop. So the $computer variable is only the last element of the loop. In this case it’s Test2. It will output $result for Test2 no matter if matches the serial number or not, and it will never output the info for Test1. Although you’re probably better off with the Invoke-Command as suggested above.

You don’t need loops here, -ComputerName accepts array of string.

Get-CimInstance -ComputerName $ComputerName -Class Win32_Bios -Filter "SerialNumber='$serial'"

The result will be from the relevant computer and unless you need some AD property of the resultant computer, no need to use Get-ADComputer as well.

Thank you!

it works fine with Get-CimInstance!

That’s an easy fix :slight_smile: Thanks

The solution is not actually to use Get-CimInstance, you could achieve it by Get-WmiObject as well, but is not recommend because of various reasons, PS 6.x doesn’t have any -WMI cmdlets available.