【问题标题】:Powershell to .vbs [duplicate]Powershell 到 .vbs [重复]
【发布时间】:2021-11-04 18:05:58
【问题描述】:

您好,我可以将以下内容转换为 .vbs 文件,以便在选择时可以在窗口中显示 IPAddress,例如 Wscript.echo?

get-netipaddress | Where-Object -FilterScript {$_.AddressFamily -match "IPv4"} |Where-Object -FilterScript {$_.InterfaceAlias -notlike "Loopback*"}| Where-Object {$_.IPAddress -like "192.168*"} | Select-Object -ExpandProperty IPAddress

推荐的修复方法不起作用,并希望避免使用 .ps1 到 .vbs

【问题讨论】:

    标签: powershell vbscript range ip subnet


    【解决方案1】:

    这里有几个选项...

    选项 1:仅 VBScript

    下面的示例假定您真正需要的唯一过滤器是“192.168*”,因为其他过滤器似乎对我的测试没有任何影响。但是,YMMV,所以你可能需要调整这个脚本:

    Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
    Set oItems = oWMI.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
     
    For Each oItem in oItems
      IP = oItem.IPAddress(0)
      If Left(IP,7) = "192.168" Then IPList = IPList & IP & VBCRLF
    Next
    
    MsgBox IPList
    

    选项 2:两个脚本文件(.vbs 和 .ps1)

    以下示例使用临时文件在脚本之间传递数据。这使用了您所有的原始过滤器,但与仅 VBScript 的版本相比非常慢。

    IPList.vbs

    Set oWSH = CreateObject("Wscript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oWSH.Run "Powershell.exe -ExecutionPolicy Bypass -File .\IPList.ps1",0,True
    Temp  = oWSH.ExpandEnvironmentStrings("%Temp%")
    Set oFile = oFSO.OpenTextFile(Temp & "\IPList.txt",1)
    IPList = oFile.ReadAll
    oFile.Close
    MsgBox IPList
    

    IPList.ps1

    get-netipaddress | Where-Object -FilterScript {$_.AddressFamily -match "IPv4"} |Where-Object -FilterScript {$_.InterfaceAlias -notlike "Loopback*"}| Where-Object {$_.IPAddress -like "192.168*"} | Select-Object -ExpandProperty IPAddress | Out-File -Encoding ascii "$env:TEMP\IPList.txt"
    

    【讨论】:

    • .vbs 非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 2012-03-09
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多