Obtaining Data from a Remote Computer

You can obtain data or manage resources on remote computers as well as the local computer. Connecting to a remote computer in a Windows Remote Management script is very similar to making a local connection. WMI instance data is available and, if the remote computer has BMC hardware that can communicate using the WS-Management protocol, then Intelligent Platform Management Interface (IPMI) data is also available. For more information, see Windows Remote Management and WMI and Remote Hardware Management.

You may need to create a ConnectionOptions object to specify information about the type of authentication requested for the logon.

If the account on the remote computer has the same logon username and password, the only extra information you need is the transport, the domain name, and the computer name. Because of User Account Control (UAC), the remote account must be a domain account and a member of the remote computer Administrators group. If the account is a local computer member of the Administrators group, then UAC does not allow access to the WinRM service. To access a remote WinRM service in a workgroup, UAC filtering for local accounts must be disabled by creating the following DWORD registry entry and setting its value to 1: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] LocalAccountTokenFilterPolicy.

To connect to a remote computer using your logon username and password

  1. Specify the target computer with a fully qualified domain name or an IP address and assign this to a constant. If an IPv6 address is specified, then the address must be enclosed in square brackets.

    Const RemoteComputer = "ComputerName.domain.com"
    
  2. Create a WSMan object.

    Set objWsman = CreateObject("WSMan.Automation")
    
  3. Create the session, specifying the transport, HTTP or HTTPS, and concatenating it with the constant representing the target computer.

    
    Set objSession = objWsman.CreateSession("https://" & RemoteComputer)
    

The following VBScript code example shows the complete script. The script includes a subroutine to transform the data from raw XML to human-readable form. For more information, see Displaying XML Output from WinRM Scripts.

Const RemoteComputer = "ComputerName.domain.com"

Set objWsman = CreateObject("WSMan.Automation")
Set objSession = objWsman.CreateSession("https://" & RemoteComputer)
strResource = "http://schemas.microsoft.com/wbem/wsman/1/" & _
  "wmi/root/cimv2/Win32_OperatingSystem"
Set objResponse = objSession.Enumerate(strResource)

While Not objResponse.AtEndOfStream
    DisplayOutput(objResponse.ReadItem) 
Wend

'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************
Sub DisplayOutput(strWinRMXml)
    Dim xmlFile, xslFile
    Set xmlFile = CreateObject("MSXml.DOMDocument") 
    Set xslFile = CreateObject("MSXml.DOMDocument")
    xmlFile.LoadXml(strWinRMXml)
    xslFile.Load("WsmTxt.xsl")
    Wscript.Echo xmlFile.TransformNode(xslFile) 
End Sub

To connect to a remote computer using a different account

  1. Specify the target computer with a fully qualified domain name or an IP address and assign this to a constant. If an IPv6 address is specified, then the address must be enclosed in square brackets.

    Const RemoteComputer = "ComputerName.domain.com"
    
  2. Create a WSMan object.

    Set objWsman = CreateObject("Wsman.Automation")
    
    
  3. Call the WSMan.CreateConnectionOptions method to create a ConnectionOptions object. The account on the remote computer must be a member of the local computer administrators group.

    Set objConnectionOptions = objWsman.CreateConnectionOptions
    objConnectionOptions.UserName = "Username"
    objConnectionOptions.Password = "Password"
    
  4. On the WSman.CreateSession call, specify the appropriate session connection flags in the flags parameter. For more information, see Session Constants. Specify the target computer with a fully qualified computer name or an IP address and the transport—http or https. This script requests Kerberos authentication from the remote WinRM service.

    Unlike WMI scripts, you can use several methods of authentication in WinRM scripts. For more information, see Authentication for Remote Connections.

    iFlags = objWsman.SessionFlagUseKerberos Or _
      objWsman.SessionFlagCredUserNamePassword
    Set objSession = objWsman.CreateSession("https://" & RemoteComputer, _
      iFlags, objConnectionOptions)
    
  5. After the session object is available, you can call any of the Session object methods to obtain data for a resource. You can get data for any resource that is available on the computer on which the session is running. For more information, see Obtaining Data from the Local Computer.

The following VBScript code example shows the complete script. The script includes a subroutine to transform the data from raw XML to human-readable form. For more information, see Displaying XML Output from WinRM Scripts. The script specifies Kerberos authentication, but if the remote computer is in a workgroup rather than a domain, specifying Kerberos generates an error.

Const RemoteComputer = "ComputerName.domain.com"

Set objWsman = CreateObject("Wsman.Automation")
Set objConnectionOptions = objWsman.CreateConnectionOptions
objConnectionOptions.UserName = "Username"
objConnectionOptions.Password = "Password"
iFlags = objWsman.SessionFlagUseKerberos Or _
  objWsman.SessionFlagCredUserNamePassword
Set objSession = objWsman.CreateSession("https://" & RemoteComputer, _
  iFlags, objConnectionOptions)
strResource = "http://schemas.microsoft.com/wbem/wsman/1/" & _
  "wmi/root/cimv2/Win32_OperatingSystem"
Set objResponse = objSession.Enumerate(strResource)

While Not objResponse.AtEndOfStream
    DisplayOutput(objResponse.ReadItem) 
Wend

'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************
Sub DisplayOutput(strWinRMXml)
    Dim xmlFile, xslFile
    Set xmlFile = CreateObject("MSXml2.DOMDocument.3.0") 
    Set xslFile = CreateObject("MSXml2.DOMDocument.3.0")
    xmlFile.LoadXml(strWinRMXml)
    xslFile.Load("WsmTxt.xsl")
    Wscript.Echo xmlFile.TransformNode(xslFile) 
End Sub

About Windows Remote Management

Using Windows Remote Management

Windows Remote Management Reference