简体   繁体   中英

How to make this WSH script work? (details in message)

This script is supposed to open both Windows shell Status and Properties dialogs of the first found network connection which is enabled or connected. However, only the Properties dialog is opened. The verb for the Status dialog is already correct, which is "Stat&us". The script was tested and will be used under Windows XP Pro SP3 32-Bit. It was tested with a connected 3G dialup and a LAN Loopback. Both have the same problem.

dim a,b,c
set a=createobject("shell.application")
set b=a.namespace(0).parsename("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").getfolder
for i=0 to (b.items.count-1)
  set c=b.items.item(i)
  for j=0 to (c.verbs.count-1)
    'only process connected/enabled
    if (lcase(c.verbs.item(j)) = "disc&onnect") or (lcase(c.verbs.item(j)) = "disa&ble") then
      'open status and properties dialogs
      c.invokeverb("Stat&us")     'this doesn't work
      c.invokeverb("P&roperties") 'this one works
      msgbox "Press OK to close all and exit"
      wscript.quit
    end if
  next
next

In Windows XP there is a bug whose effect requires the Status verb to be invoked from within the Explorer process. Since WScript/CScript is not a child of the Explorer process, any attempt to invoke the status verb with prove futile despite the lack of any apparent errors. This bug appears to have been fixed in later versions as the script below is tested and working in Vista x64.

Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder 

For Each objShellFolderItem in objShellFolder.Items
    Set colShellFolderItemVerbs = objShellFolderItem.Verbs

    For Each objShellFolderItemVerb in colShellFolderItemVerbs
        strVerb = objShellFolderItemVerb.Name
        If (strVerb = "C&onnect / Disconnect") Then
            objShellFolderItem.InvokeVerb("Properties")
            objShellFolderItem.InvokeVerb("Status")

            MsgBox "Press OK to close and exit"
            WScript.Quit(0)
        End If
    Next
Next

Option 1

Does that mean that you're out of luck? Not entirely. I have two different suggestions for you. The first uses a little trickery. Status is the default action for any network connection while it is in a connected state. Open up your network connections, right-click the connection your wish to monitor and choose Create Shortcut. You can place the shortcut anywhere you like. By default it will be named something like "Wireless Network Connection - Shortcut.lnk" on your Desktop. Typing that on the command line or via the Run or Exec methods in your script will do exactly what you need. I tried playing around with doing this all via scripting but ran into issues tryint to automate the Create Shortcut verb.

Option 2

A second option is also a bit of a workaround but may work if your 3G connection uses the dialup networking. The command line rundll32.exe rnaui.dll,RnaDial {name of connection to establish} will open the dialog to connect, however, if already connected, it opens the Status dialog for the connection. You could then try a script like this:

Set objShell = CreateObject("Shell.Application")
Set objShellFolder = objShell.Namespace(0).ParseName("::{7007ACC7-3202-11D1-AAD2-00805FC1270E}").GetFolder

For Each objShellFolderItem in objShellFolder.Items
    strConnection = objShellFolderItem.Name
    strCommandLine = "rundll32.exe rnaui.dll,RnaDial " & Chr(34) & strConnection & Chr(34)

    Set colShellFolderItemVerbs = objShellFolderItem.Verbs

    For Each objShellFolderItemVerb in colShellFolderItemVerbs
        strVerb = objShellFolderItemVerb.Name
        If (strVerb = "C&onnect / Disconnect") Then
            objShellFolderItem.InvokeVerb("Properties")
            CreateObject("WScript.Shell").Run strCommandLine

            MsgBox "Press OK to close and exit"
            WScript.Quit(0)
        End If
    Next
Next

Option 3

A final option would be to use WMI to display the information about your network connection. This is a more traditional scripting approach.

In any case, I hope this helps out. Don't forget to change the verbs as required. They do change from one version of Windows to the next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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