SRansom
SRansom

Reputation: 357

Formatting a powershell table, removing entries

I'm new to powershell and I've run into a seemingly simple problem I just can't find the solution to. I'm writing a script that pulls the serial number off of the current computer, it then queries a database and performs functions on the computer based on what the database says to do for that particular serial number. Now here's the problem, when I use the following code

$currSerial = Get-WMIObject -Class "Win32_BIOS" | select SerialNumber

I receive output in what I'm pretty sure is a table, so when I output $currSerial I get this:

SerialNumber                                                                                                                                                
------------                                                                                                                                                
AGR0Agh99481028

All I want is to save the text "AGR0Agh99481028" to a string so that I can query the server with that number. right now my query is looking like this:

$Query = "SELECT name FROM table WHERE serial = '$currSerial'"

output parses to this:

SELECT name FROM table WHERE serial = '@{SerialNumber=D1N0AS249667028     }'

I read about the get-item cmd and it seems like I'd already have to know what the serial was to search for it. Is there some cmdlet that I can use to make this easier? Thanks for the help.

Upvotes: 0

Views: 396

Answers (3)

Lars Truijens
Lars Truijens

Reputation: 43595

Or even shorter

$currSerial = (Get-WMIObject -Class "Win32_BIOS").SerialNumber

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

The output of select is an object with just the specified properties. To get the serial number string change

Get-WMIObject -Class "Win32_BIOS" | select SerialNumber

into

(Get-WMIObject -Class "Win32_BIOS" | select SerialNumber).SerialNumber

Upvotes: 3

rtf
rtf

Reputation: 734

All you need is a flag:

$currSerial = Get-WMIObject -Class "Win32_BIOS" | select -ExpandProperty SerialNumber

-ExpandProperty will turn it in to a plain-old string.

Upvotes: 4

Related Questions