如何将电脑序列号转换为字符串
首先我想知道我是否可以
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过使用 Out-String,您可以转换 gwmi win32_bios | 的输出。选择序列号为字符串并将其存储在
$sn
中。因此,$sn
现在将具有以下内容:因此,
$sn.length
显示整个字符串的长度。如果只想将其更改为序列号:如您所见,我的序列号(我删除了原始序列号)只有7个字符宽。但是,
$sn.length
显示 9。输出后可能有几个隐藏字符。我可以在控制台输出后看到一个空行。说到真正的重点,这个空格是由
Out-String
添加的。所以,你甚至不需要那个。你可以这样做:$sn
仍然是一个字符串。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:So,
$sn.length
is showing you the length of this entire string. If you want to change it only to the serial number: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:$sn
is still a string.看起来这两个 wmi 属性在我的机器上给出了相同的结果。我猜他们是从同一个地方出发的。
就 GetType 的结果而言,我得到:
这意味着 $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:
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.
如果您对 gwmi win32_bios | 的输出执行 get-member 操作选择serialnumber,您将看到它实际上具有以下属性,就像.NET 中的任何对象一样。
如果您需要序列号,则需要执行以下操作:
这样您就可以选择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.If you want the serial number, you need to do the following:
That way you're selecting the contents of the serialnumber property of the serialnumber object.
或者你可以这样做:
Or you can just do this: