获取命令输出参数

时间:2016-08-25 13:27:33

标签: command-line vbscript

我运行一个命令并获得所需的输出但是从这个输出数据我需要获得所需的参数值。问题是必需的参数名称出现在2个地方,所以我有的代码总是返回第2次实例参数值代替第1次出现。我不知道在VBScript中我们是否能完全匹配。

命令输出为:

Product Name: DellRack81
Part Number: QAWSX
System Board Spare Part Number: 01032-001

从上面的输出中,我想获得“零件号:”值,但代码返回“系统板备件号”的值。

现在获得的结果是:

Product Name    Part Number   System Board Spare Part Number
DellRack81      01032-001     01032-001

我正在努力的是:<​​/ p>

Product Name    Part Number   System Board Spare Part Number
DellRack81      QAWSX         01032-001

4 个答案:

答案 0 :(得分:0)

你天真的状况

InStr(1,StrTest,"Part Number")  > 0

对于&#34;零件编号&#34;都是如此和&#34;系统板备件号&#34;,因此StrPNo设置两次。更改条件以检查位置 1

<强>更新

Option Explicit

Dim a : a = Split("Product Name: DellRack81|Part Number: QAWSX|System Board Spare Part Number: 01032-001", "|")
ReDim b(2)
Dim s
For Each s In a
    WScript.Echo s
    If InStr(1, s,"Product Name:") = 1 Then b(0) = UCase(Trim(Mid(s,(InStr(1,s,":",1)+2))))
    If InStr(1, s,"Part Number:") = 1 Then b(1) = UCase(Trim(Mid(s,(InStr(1,s,":",1)+2))))
    If InStr(1, s,"System Board Spare Part Number:") = 1 Then b(2) = UCase(Trim(Mid(s,(InStr(1,s,":",1)+2))))
Next
WScript.Echo Join(b, vbCrLf)

输出:

cscript 39146660.vbs
Product Name: DellRack81
Part Number: QAWSX
System Board Spare Part Number: 01032-001
DELLRACK81
QAWSX
01032-001

(你不应该在工作中使用这个策略)

答案 1 :(得分:0)

Set Sh = WScript.CreateObject("WScript.Shell")
cmdLine = '" type her your command line you want to execute"    
Set oEx = Sh.Exec(cmdLine)
Set oSOut = oEx.StdOut
Do While Not oSOut.AtEndOfStream
    Line = oSOut.ReadLine
    If InStr (1,Line,"Part Number:",1) > 0 Then
      MsgBox Line
      Exit Do 
    End If 
Loop

此脚本将执行您在 cmdline 变量中添加的命令行。

在第一个 Part Number: 中,它将显示该行,然后退出循环。它不会显示零件号的第二行。

OR

Set Sh = WScript.CreateObject("WScript.Shell")
cmdLine = '" type her your command line you want to execute"    
Set oEx = Sh.Exec(cmdLine)
Set oSOut = oEx.StdOut
Do While Not oSOut.AtEndOfStream
    Line = oSOut.ReadLine

    If StrComp(Left(Line,12),"Part Number:",1) = 0 Then
    MsgBox Line
    End If 
Loop

此代码将遍历命令行结果的所有行,但仅显示第一个部分的编号,而忽略列表中的其他部分。这次无需退出do循环。

答案 2 :(得分:0)

如果要检索三个项目 Product Name Part Number System Board Spare Part Number < / p>

Set Sh = WScript.CreateObject("WScript.Shell")
cmdLine =  '" type her your command line you want to execute"    
Set oEx = Sh.Exec(cmdLine)
Set oSOut = oEx.StdOut

txt=""
myArray=Array("Product Name","Part Number","System Board Spare Part Number")
Do While Not oSOut.AtEndOfStream
    Line = oSOut.ReadLine
For i=0 To  UBound(myArray)
    If InStr (1,Line,myArray(i),1) > 0 Then
    txt= Line&vbCrLf&txt
    End If 
    Next 
Loop
MsgBox txt

答案 3 :(得分:0)

如果您要检索三个项目 Product Name Part Number System Board Spare Part Number ,请使用这样它比其他人更强。

Set Sh = WScript.CreateObject("WScript.Shell")
cmdLine = '" type her your command line you want to execute"    
Set oEx = Sh.Exec(cmdLine)
Set oSOut = oEx.StdOut

txt=""
myArray=Array("Product Name","Part Number","System Board Spare Part Number")
Do  Until oSOut.AtEndOfStream
    Line = oSOut.ReadLine   
For i=0  To  UBound(myArray)
A=myArray(i)
L=Len(A)
LF=Left(Trim(Line),L)
 If StrComp(LF,A,1) = 0 Then
    txt= trim(Line) & vbCrLf & txt 
    End If 
 Next
Loop
  MsgBox txt