Export (0) Print
Expand All

Obtaining Data from the Local Computer

Although Windows Remote Management and WS-Management protocol are explicitly designed for remote communication, establishing a session on the local computer is the simplest case. Some scripts may require access data on the local computer as well as remote computers.

WinRM version 2.0:  

All operations are considered remote and the WinRM service must be started before any operation is performed. If a remote destination is not specified, then the localhost is used by default, and all operations will be sent to the local WinRM service. For more information about starting the WinRM service, see Installation and Configuration for Windows Remote Management.

When using the WinRM service for local operations, the following factors should be considered:

  • The local WinRM configuration can only be read by administrators.
  • WMI namespaces must have remote enable permissions set. For more information, see Securing a Remote WMI Connection.
  • If a WinRM listener is not created, then the WinRM service listens for local requests on port 47001.

Every WinRM script must start by establishing a session or connection to a computer by creating a Session object. After the session is created, you can use the Session object methods, such as Session.Enumerate or Session.Invoke to obtain data or to execute methods.

The creation of a session is somewhat similar to connecting to a Windows Management Instrumentation (WMI) namespace. The session is essentially a layer that allows you to send and receive data through SOAP messages and the WS-Management protocol. For more information, see WS-Management Protocol.

Calling the WSMan.CreateSession method to create a Session object will start a session that connects to the local WinRM.

Aa384424.wedge(en-us,VS.85).gifTo Create a WSMan Session and Obtain Data

  1. Create a WSMan object.
    
    
    Set objWsman = CreateObject("Wsman.Automation")
    
    
    
  2. Create a session by calling the WSMan.CreateSession method. This session runs under your logon username and password and can obtain data through the local WinRM.
    
    
    Set objSession = objWsman.CreateSession()
    
    
    
  3. Create a resource URI to identify the resource you want to manage or for which you want to obtain data. For more information about formatting a URI, see Resource URIs. This resource URI is for a specific instance of the WMI Win32_Service class, the Winmgmt service. For more information, see Windows Remote Management and WMI.
    
    
    strResource = "http://schemas.microsoft.com/wbem/wsman/1/"_
        & "wmi/root/cimv2/Win32_Service?Name=Winmgmt"
    
    
    
  4. Call Session methods that get or enumerate data using the resource URI. For more information, see WinRM Scripting API.
    
    
    strResponse = objSession.Get(strResource)
    Wscript.Echo strResponse
    
    
    
  5. To get or manage data from another computer or use different methods of authentication, see Obtaining Data from a Remote Computer.

The following VBScript code example shows the complete script that obtains the specific instance of the WMI Win32_Service named "Winmgmt".



Set objWsman = CreateObject("Wsman.Automation")
Set objSession = objWsman.CreateSession()
strResource = "http://schemas.microsoft.com/wbem/wsman/1/"_
    & "wmi/root/cimv2/Win32_Service?Name=Winmgmt"
strResponse = objSession.Get(strResource)
Wscript.Echo strResponse


The following VBScript code example shows the complete script with the data transform. For more information, see Displaying XML Output from WinRM Scripts.



Set objWsman = CreateObject("Wsman.Automation")
Set objSession = objWsman.CreateSession()
strResource = "http://schemas.microsoft.com/wbem/wsman/1/"_
    & "wmi/root/cimv2/Win32_Service?Name=Winmgmt"
strResponse = objSession.Get(strResource)
Set xmlFile = CreateObject("MSXml.DOMDocument")
Set xslFile = CreateObject("MSXml.DOMDocument")
xmlFile.LoadXml(strResponse)
xslFile.Load("WsmTxt.xsl")
Wscript.Echo xmlFile.TransformNode(xslFile)



Related topics

About Windows Remote Management
Using Windows Remote Management
Windows Remote Management Reference

 

 

Show:
© 2014 Microsoft