VBS 使用 RegEx 返回命令行返回的一部分

发布于 2024-12-21 12:22:55 字数 990 浏览 0 评论 0原文

我正在尝试通过 ssh 执行命令,仅获取返回命令的第一部分,并将其设置为要使用的变量。我正在尝试使用 RegExp,因为我只需要前 4 位数字,所以我使用模式代码“^\d{1,4}”,我可以通过此代码成功获得整个返回;

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
      Do Until oExec.StdOut.AtEndOfStream
         ID = oExec.StdOut.ReadLine
      Loop
       WScript.Echo ID

但是现在,当我尝试使用 RegExp 和 echo 来查看是否得到我想要的内容时,我收到“类型不匹配”错误。

Set RegExp = New RegExp
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
RegExp.Pattern = "^\d{1,4}"
      Do Until oExec.StdOut.AtEndOfStream
         ID = oExec.StdOut.ReadLine
          WScript.Echo RegExp.execute(ID)
      Loop

如果有人对代码有问题有任何煽动,请告诉我。如果您知道任何替代方案,我将不胜感激。使用 shell 脚本我可以得到我正在寻找的东西,但我需要在 Windows 上运行这个 VBS。这是 Shell 脚本(如果有人可以将其翻译为 VBS)。

.............for i in `command | sed ‘1d’ | awk ‘{print $1}’`..............

提前感谢任何帮助...我在这个问题上绞尽脑汁有一段时间了,然后精疲力竭。

I'm trying to execute a command via ssh, take only the first part of the returned command, and set it as a variable to be used. I'm attempting to use RegExp and because I only need the first 4 digits, I use the pattern code "^\d{1,4}" I can successfully get the entire return via this code;

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
      Do Until oExec.StdOut.AtEndOfStream
         ID = oExec.StdOut.ReadLine
      Loop
       WScript.Echo ID

But Now when I try to use RegExp and echo to see if I'm getting what I want I get a "Type Mismatch" error

Set RegExp = New RegExp
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
RegExp.Pattern = "^\d{1,4}"
      Do Until oExec.StdOut.AtEndOfStream
         ID = oExec.StdOut.ReadLine
          WScript.Echo RegExp.execute(ID)
      Loop

If anyone has any incite on what's wrong with the code please let me know. If you know of any alternative I'd appreciate it. Using a shell script I can get what I'm looking for but I need to run this VBS with windows. Here's the Shell script if anyone can translate it to VBS.

.............for i in `command | sed ‘1d’ | awk ‘{print $1}’`..............

Appreciate any help in advance... Been racking my brain on this one for a while and getting burnt out.

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

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

发布评论

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

评论(1

提赋 2024-12-28 12:22:55

您不能 .Echo 一个对象,例如从 .Execute 获得的匹配集合;访问其第一项的 .Value:

>> sInp = "1234xxx"
>> Set reHead = New RegExp
>> reHead.Pattern = "^\d{4}"
>> Set oMTS = reHead.Execute(sInp)
>> WScript.Echo oMTS(0).Value
>>
1234
>> WScript.Echo oMTS
>>
Error Number:       13
Error Description:  Type mismatch

You can't .Echo an object like the match collection you get from .Execute; access the .Value of its first item:

>> sInp = "1234xxx"
>> Set reHead = New RegExp
>> reHead.Pattern = "^\d{4}"
>> Set oMTS = reHead.Execute(sInp)
>> WScript.Echo oMTS(0).Value
>>
1234
>> WScript.Echo oMTS
>>
Error Number:       13
Error Description:  Type mismatch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文