Before you can enumerate classes, invoke methods, or examine properties of the managed server, you must create a connection object in your client. The connection object manages the connection with the CIM server, accepts CIM methods by proxy, and passes them to the CIM server. The following pseudocode illustrates how to create a connection by using command-line parameters passed to the client.

To make a connection to the CIMOM

Procedure

  1. Collect the connection parameters from the environment.
    use os
    
    function parse_environment() 
       ///Check if all parameters are set in the shell environment.///
       VI_SERVER = VI_USERNAME = VI_PASSWORD = VI_NAMESPACE=Null
       ///Any missing environment variable is cause to revert to command-line arguments.///
       try
          return { 'VI_SERVER':os.environ['VI_SERVER'], \
                   'VI_USERNAME':os.environ['VI_USERNAME'], \
    	               'VI_PASSWORD':os.environ['VI_PASSWORD'], \
    	               'VI_NAMESPACE':os.environ['VI_NAMESPACE'] }
       catch
          return Null
    
    use sys
    
    function get_params() 
       ///Check if parameters are passed on the command line.///
       param_host = param_user = param_password = param_namespace = Null
       if len( sys.argv ) == 5 
          print 'Connect using command-line parameters.'
          param_host, param_user, param_password, param_namespace = sys.argv [ 1:5 ] 
          return { 'host':param_host, \
                   'user':param_user, \
                   'password':param_password, \
                   'namespace':param_namespace }
       env = parse_environment()
       if env 
          print 'Connect using environment variables.'
          return { 'host':env['VI_SERVER'], \
                   'user':env['VI_USERNAME'], \
    	               'password':env['VI_PASSWORD'], \
    	               'namespace':env['VI_NAMESPACE'] }
       else
          print 'Usage: ' + sys.argv[0] + ' <host> <user> <password> [<namespace>]'
          print '  or set environment variables: VI_SERVER, VI_USERNAME, VI_NAMESPACE'
          return Null
    
    params = get_params()
    if params is Null 
       exit(-1)
  2. Create the connection object in the client.
    use wbemlib
    connection = Null
    
    function connect_to_host( params ) 
       ///Connect to the server.///
       connection = wbemlib.WBEMConnection( 'https://' + params['host'], \
                 ( params['user'], params['password'] ), \
                 params['namespace'] )
       return connection
    
    if connect_to_host( params ) 
       print 'Connected to: ' + params['host'] + ' as user: ' + params['user']
    else
       print 'Failed to connect to: ' + params['host'] + ' as user: ' + params['user']

    With some client libraries, creating a connection object in the client does not send a request to the CIMOM. A request is not sent until a method is called. To verify that such a client can connect to and authenticate with the server, see another use case, such as Listing Registered Profiles in the CIM Implementation.