VBScript Help - Wireless Disable

Discussion in 'Scripting' started by ramg1967, Apr 17, 2015.

  1. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    138
    15
    10
    Hi - I have this VBScript that, I tried on few systems to help the users to disable Wireless when docked on Lenovo Docking Stations and enabled when undocked from docking stations.

    The script is working fine but one problem - the script has to be run as admin and users do not have the permission. I would like to make some modification to the script that can help to run the script as regular domain users. I would appreciate, if any one can take a look at the script and help me what modification needs to be done.

    Here is the script: If you want the script for download - I can upload to a file share site. Let me know.

    *********************************************
    '* Net Switcher - Watches one network card *
    '* for connectivity, and toggles another *
    '* http://www.intelliadmin.com *
    '*********************************************

    Sub EnableAdapter( sAdapterName, bStatus )
    Dim objWMIService, colItems
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
    For Each objItem in colItems
    if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
    if (bStatus) then
    objItem.Enable
    else
    objItem.Disable
    end if
    end if
    Next
    End Sub

    Function AdapterStatus( sAdapterName )
    Dim objWMIService, colItems
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)

    AdapterStatus = false
    For Each objItem in colItems
    if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
    AdapterStatus = (objItem.NetConnectionStatus=2)
    end if
    Next
    End Function

    Function AdapterExists( sAdapterName )
    Dim objWMIService, colItems
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)

    AdapterExists = false
    For Each objItem in colItems
    if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
    AdapterExists = True
    end if
    Next
    End Function


    Dim bCurrentStatus
    Dim bChanged
    Dim sWatchNetworkCard
    Dim sSwitchNetworkCard

    if (WScript.Arguments.Count<2) then
    WScript.Echo "*********************************"
    WScript.Echo "* IntelliAdmin Net Switcher *"
    WScript.Echo "* http://www.intelliadmin.com *"
    WScript.Echo "*********************************"
    WScript.Echo vbCrLf & "Usage: " & vbCRLF
    WScript.Echo " NetSwitch.vbs <Card To Watch> <Card To Switch>"
    WScript.Echo vbCrLF & "Explanation: " & vbCRLF
    WScript.Echo " Net Switcher can be used to make sure your"
    WScript.Echo " wireless card is only enabled when no "
    WScript.Echo " ethernet connection is available"
    WScript.Echo vbCrLF & "Example: " & vbCRLF
    WScript.Echo " NetSwitch.vbs " & chr(34) & "Local Area Connection" & chr(34) & " " & _
    chr(34) & "Wireless Connection" & Chr(34)
    WScript.Quit

    end if

    sWatchNetworkCard = WScript.Arguments(0)
    sSwitchNetworkCard = WScript.Arguments(1)

    if (Not(AdapterExists(sWatchNetworkCard))) then
    WScript.Echo "Error: Could not find the adapter (" & sWatchNetworkCard & ")"
    WScript.Quit
    end if

    if (Not(AdapterExists(sSwitchNetworkCard))) then
    WScript.Echo "Error: Could not find the adapter (" & sSwitchNetworkCard & ")"
    WScript.Quit
    end if

    bChanged=TRUE

    while(True)
    if (bChanged) then
    bChanged=FALSE
    bCurrentStatus = AdapterStatus(sWatchNetworkCard)
    if (bCurrentStatus) then
    EnableAdapter sSwitchNetworkCard,False
    else
    EnableAdapter sSwitchNetworkCard,True
    end if
    end if
    WScript.Sleep(1000)
    if (bCurrentStatus<>AdapterStatus(sWatchNetworkCard)) then
    bChanged=TRUE
    end if
    wend
     
  2. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    #2 QuantumBug, Jul 8, 2015
    Last edited by a moderator: Apr 20, 2017
    Add this code

    Code:
    If Not WScript.Arguments.Named.Exists("elevate") Then  CreateObject("Shell.Application").ShellExecute WScript.FullName _
        , WScript.ScriptFullName & " /elevate", "", "runas", 1
      WScript.Quit
    End If
    Regards, The Dev.
     
  3. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    138
    15
    10
    @ The Dev - Sorry, I did not see your post for this long.

    I will try your addition to the script. I believe above should be added to the top of the script before it starts the process - right?