Get Computername from SerialNumber

Welcome Forums General PowerShell Q&A Get Computername from SerialNumber

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 4 days ago.

  • Author
    Posts
  • #112973

    Participant
    Points: 0
    Rank: Member

    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

     

  • #112978

    Participant
    Points: 3
    Rank: Member

    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
    
    
    
    
    		
  • #113012

    Participant
    Points: 0
    Rank: Member

    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.

  • #113027

    Participant
    Points: 20
    Rank: Member

    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.

    • #113053

      Participant
      Points: 0
      Rank: Member

      Thank you!

      it works fine with Get-CimInstance!

      That's an easy fix 🙂 Thanks

  • #113057

    Participant
    Points: 20
    Rank: Member

    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.

    https://blogs.technet.microsoft.com/heyscriptingguy/2016/02/08/should-i-use-cim-or-wmi-with-windows-powershell/

You must be logged in to reply to this topic.