Comparing data from a CSV file to data queried from WMI

Hey Scripting Guy! I am working on a script for our district to automatically name computers based on serial numbers. We have a CSV database that pairs each computer to a name via its serial number;

Name,Serial Number

HS-LAB-01,xxxyyyzzz
HS-LAB-02,aaabbbccc

etc etc...

I'm using this in conjunction with sysprep for imaging. This way a newly imaged computer will automatically be named, joined to the domain, and placed in the correct OU. The problem is that I cannot get it to compare information from the csv file (the serial number) to the query result from Get-WMIObject then compare that with a matching computer name. Here's what I have:

$serialnum = (Get-WmiObject win32_BIOS|select SerialNumber)

To parse the SN of the machine. This works.

$newname = (Import-CSV '\\server\share\csvdatabase.csv' | Where-Object 'Serial' -eq $serialnum | Select 'Name')

Rename-Computer -Newname $newname -DomainCredential $username

^ This doesn't work

*($username omitted)

I can enter $serialnum into PS and it will return the machine SN just fine, which means it works. However, I just draw a blank for $newname every time. Thoughts?

July 2nd, 2013 1:58pm

Hi,

In your Where-Object, you're using 'Serial' as the property name, not "Serial Number" as shown in your example CSV snippet. Try this:

$newname = (Import-CSV '\\server\share\csvdatabase.csv' | Where-Object "Serial Number" -eq $serialnum | Select 'Name')
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 2:04pm

Ack, mistyped that one! That's what I meant to say. 
July 2nd, 2013 2:09pm

Ack, mistyped that one! That's what I meant to say. 

As in, that's what you're already using? The code I posted above does populate $newname for me in my (albeit limited) testing.

EDIT: What version of PowerShell are you ru

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 2:12pm

It's what I'm already using. I get a 'Cannot validate argument on parameter' error. 

I'm using PS3.0
July 2nd, 2013 2:23pm

That's odd then. Here's my input and output:

.\test.csv contents:

"Name","Serial Number"
"one","one123"
"two","two123"
"three","three123"
"four","four123"

Command run:
$newname = Import-Csv .\test.csv | Where-Object "Serial Number" -eq "one123" | Select Name

Output:

$newname

Name               
----            
one                                                                                                                                    

Name only output:

$newname.Name

one



Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 2:30pm

It's very odd. This should work as it sits. The only thing that I can tell is maybe $serialnum isn't being imported into the -equal field properly

Between the Get-WMIObject and $newname lines I do have a Net use line to connect to the share with a $username and $password (hidden network share). That shouldn't make a difference, though...

July 2nd, 2013 2:41pm

Whoops, totally missed this.

$newname = (Import-CSV '\\server\share\csvdatabase.csv' | Where-Object "Serial Number" -eq $serialnum.SerialNumber | Select 'Name')

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 2:50pm

Wow, that did it! =D Thank you!

What exactly does that .SerialNumber do?

July 2nd, 2013 2:57pm

You're welcome, I'm glad I could help out.

The .SerialNumber property holds the actual data that you want from the $serialnum object. The easiest way to see what properties you have available for an object is to pipe the object to the Get-Member cmdlet.

In this example, here's the output:

PS C:\Scripts\PowerShell Scripts\Misc Testing\7-2-2013> $serialnum | gm


   TypeName: Selected.System.Management.ManagementObject

Name         MemberType   Definition                        
----         ----------   ----------                        
Equals       Method       bool Equals(System.Object obj)    
GetHashCode  Method       int GetHashCode()                 
GetType      Method       type GetType()                    
ToString     Method       string ToString()                 
SerialNumber NoteProperty System.String SerialNumber=MyPCName

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 3:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics