开发者

how to convert pc serial number to string

First of all I'd like to know if I can use these two instructions

gwmi win32_bios | select serialnumber

gwmi win32_Computersystemproduct | select identifyingnumber

indifferently.

The second question is why if I write

$sn = gwmi win32_bios | select serialnumber | out-string

$sn.gettype() returns me system.object

and

$sn.length returns me 561 even though my开发者_如何学运维 serial number is made of 22 chars. Thanks.


By using Out-String, you are converting the output of gwmi win32_bios | select serialnumber to a string and storing it in $sn. So, $sn will now have the following content:

PS> $sn

serialnumber
------------
xxxxxxx

So, $sn.length is showing you the length of this entire string. If you want to change it only to the serial number:

PS> $sn = gwmi win32_bios | select -Expand serialnumber | out-string
PS> $sn
xxxxxxx    
PS> $sn.Length
9

As you can see, my serial number (I removed the original) is only 7 characters wide. But, $sn.length shows 9. There are probably a couple hidden chars after the output. I can see a blank line after the output at the console.

Coming to the real point, this space is added by Out-String. So, you don't even need that. you can do:

PS> $sn = gwmi win32_bios | select -Expand serialnumber
PS> $sn
XXXXXX
PS> $sn.Length
7

$sn is still a string.

PS> $sn.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


Looks like those two wmi properties give the same result on my machine. I'm guessing that they pull from the same place.

In terms of the results of GetType, I get this:

IsPublic IsSerial Name                                     BaseType                                                                                                                    
-------- -------- ----                                     --------                                                                                                                    
True     True     String                                   System.Object

Which means that $sn is a String, which is derived from System.Object.

Oh yeah...the last part. $sn is not just the serial number. It's The headers, the formatting, the spaces, and all of the properties of the result of the GetType() function.


if you do a get-member on the the output of gwmi win32_bios | select serialnumber you'll see that it in fact has the following properties, like any object in .NET.

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=N1B85 T10 55757

If you want the serial number, you need to do the following:

$sn = gwmi win32_bios | select serialnumber
$sn.serialnumber

That way you're selecting the contents of the serialnumber property of the serialnumber object.


Or you can just do this:

$sn = (gwmi win32_bios).serialnumber 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜