如何将电脑序列号转换为字符串

发布于 2024-11-11 06:16:07 字数 394 浏览 2 评论 0原文

首先我想知道我是否可以

gwmi win32_bios | select serialnumber

gwmi win32_Computersystemproduct | select identifyingnumber

无差别地使用这两个指令。

第二个问题是为什么如果我写

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

$sn.gettype() 返回 system.object

$sn.length 返回我 561,即使我的序列号是由 22 组成的字符。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一页 2024-11-18 06:16:07

通过使用 Out-String,您可以转换 gwmi win32_bios | 的输出。选择序列号为字符串并将其存储在$sn中。因此,$sn 现在将具有以下内容:

PS> $sn

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

因此,$sn.length 显示整个字符串的长度。如果只想将其更改为序列号:

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

如您所见,我的序列号(我删除了原始序列号)只有7个字符宽。但是,$sn.length 显示 9。输出后可能有几个隐藏字符。我可以在控制台输出后看到一个空行。

说到真正的重点,这个空格是由 Out-String 添加的。所以,你甚至不需要那个。你可以这样做:

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

$sn 仍然是一个字符串。

PS> $sn.GetType()

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

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
世态炎凉 2024-11-18 06:16:07

看起来这两个 wmi 属性在我的机器上给出了相同的结果。我猜他们是从同一个地方出发的。

就 GetType 的结果而言,我得到:

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

这意味着 $sn 是一个 String,它派生自 System.Object。

哦,是的……最后一部分。 $sn 不仅仅是序列号。它是 GetType() 函数结果的标题、格式、空格和所有属性。

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.

只是偏爱你 2024-11-18 06:16:07

如果您对 gwmi win32_bios | 的输出执行 get-member 操作选择serialnumber,您将看到它实际上具有以下属性,就像.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

如果您需要序列号,则需要执行以下操作:

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

这样您就可以选择serialnumber 对象的serialnumber 属性的内容。

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.

倚栏听风 2024-11-18 06:16:07

或者你可以这样做:

$sn = (gwmi win32_bios).serialnumber 

Or you can just do this:

$sn = (gwmi win32_bios).serialnumber 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文