You are here:

ActiveXperts.com > ActiveXperts Network Monitor > WindowsManagement > WMI > Samples > Connect to remote WMI computers

ActiveXperts Network Monitor
Monitor servers, workstations, devices and applications in your network

WMI Samples - Connect to remote WMI computers

List all shares on local computer using credentials of currently logged on user
List all shares on local or remote computer using credentials of currently logged on user
Impersonation sample: List all shares on a remote computer using different credentials than logged on user

List all shares on local computer using credentials of currently logged on user

Option Explicit

ListShares()
WScript.Echo vbCrlf & "Ready."

Sub ListShares()
    Dim strObject
    Dim colShares
    Dim objWMIService, objShare

    Set objWMIService = GetObject( "winmgmts:" )
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
    For Each objShare In colShares
        Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
End Sub

List all shares on local or remote computer using credentials of currently logged on user

Option Explicit

Dim strComputer
Do
    strComputer = inputbox( "Please enter name of a computer (or . for local host)", "Input" )
Loop until strComputer <> ""
ListShares( strComputer )
WScript.Echo vbCrlf & "Ready."

Sub ListShares(  strComputer )
    Dim strObject
    Dim colShares
    Dim objWMIService, objShare

    Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
    For Each objShare In colShares
        Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
End Sub

Impersonation sample: List all shares on a remote computer using different credentials than logged on user

Option Explicit

Dim strComputer, strUser, strPassword
Do
    strComputer = inputbox( "Please enter computername (or . for local host)", "Input" )
Loop until strComputer <> ""
Do
    strUser = inputbox( "Please enter username", "Input" )
Loop until strUser <> ""
Do
    strPassword = inputbox( "Please enter password", "Input" )
Loop until strPassword <> ""
ListShares strComputer, strUser, strPassword

WScript.Echo vbCrlf & "Ready."


Sub ListShares(  strComputer, strUser, strPassword )
    Dim strObject
    Dim objLocator, objWMIService, objShare
    Dim colShares

    Set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
    Set objWMIService = objLocator.ConnectServer ( strComputer, "root/cimv2", strUser, strPassword )
    objWMIService.Security_.impersonationlevel = 3
    Set colShares = objWMIService.ExecQuery( "Select * from Win32_Share" )
        For Each objShare In colShares
            Wscript.Echo objShare.Name & " [" & objShare.Path & "]"
    Next
End Sub