简体   繁体   English

如何索引powershell对象中的某行

[英]How to index a certain line within a powershell object

i'm a powershell beginner attempting to webscrape SerialNumbers using a powershell 2.0 script.我是一个 powershell 初学者,尝试使用 powershell 2.0 脚本抓取 SerialNumbers。 I am unsure on how to index this list(?) in order to return the SerialNo (2390).我不确定如何索引此列表(?)以返回 SerialNo (2390)。 This is the command i have used to locate where the Serial No is stored.这是我用来定位序列号存储位置的命令。

PS C:\WINDOWS\system32> $currentDocument.IHTMLDocument3_getElementsByTagName("table") | Select-Object outerText | Format-List

Below is the result (--- represents censored data)下面是结果(--- 代表删失数据)

outerText : Welcome to ---
            Model ---
            MAC Address---
            Serial No.2390
            Firmware Version---
            System Uptime---

            ---


            Basic Settings
            ---
            Network Settings
            ---
            Serial Settings
            ---
            Operating Settings


outerText : Model Name---
            MAC Address---
            Serial No.2390
            Firmware Version---
            System Uptime---

It is showing all of the outerText objects, and displaying the information held within the two outerText.它显示所有的outerText 对象,并显示两个outerText 中保存的信息。 have tried to set this as a variable, and tried to index using试图将其设置为变量,并尝试使用索引

$test[0]

but returns an empty line (no error).但返回一个空行(没有错误)。 is there a way i can index either of these to display the serial number?有没有办法可以索引其中任何一个以显示序列号?

Thanks!谢谢!

edit: have also been playing around with foreach loops, nothing returning so far编辑:也一直在玩 foreach 循环,到目前为止没有任何返回

When you use Format-List, you convert your type to object[] which loses some of the information you can use to access the variables.当您使用 Format-List 时,您将您的类型转换为 object[],这会丢失一些您可以用来访问变量的信息。

Simple example of such behavior is,这种行为的简单例子是,

$var1 = Get-Process winlogon
$var2 = Get-Process winlogon | Format-List

Write-Output "Handle for var1 -> $($var1.Handles)"
Write-Output "Handle for var2 -> $($var2.Handles)"

You would expect the output of both of the var1 and var2 to be the same but, interestingly,您可能希望 var1 和 var2 的输出相同,但有趣的是,

Output is输出是

Handle for var1 -> 258
Handle for var2 -> 

and the types changes as well并且类型也会发生变化

-> $var1.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                                  
True     False    Process                                  System.ComponentModel.Component                                                                                                                                                           

-> $var2.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                                  
True     True     Object[]                                 System.Array                  

Suggestion建议

If you want to be able to access the OuterText's serial numbers, I would suggest working without Format-List that you are using in obtaining the $test variable如果您希望能够访问 OuterText 的序列号,我建议您不要使用用于获取$test变量的 Format-List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM