Creates a Session object that can then be used for subsequent network operations.
WSMan.CreateSession( _ [ ByVal connection ], _ [ ByVal flags ], _ [ ByVal connectionOptions ] _ )
connection [in, optional]
The protocol and service to connect to, including either IPv4 or IPv6. The format of the connection information is as follows: <Transport><Address><Suffix>. For examples, see Remarks. If no connection information is provided, the local computer is used.
flags [in, optional]
The session flags that specify the authentication method, such as Negotiate authentication or Digest authentication, for connecting to a remote computer. These flags also specify other session connection information, such as encoding or encryption. This parameter must contain one or more of the flags in __WSManSessionFlags for a remote connection. For more information, see Session Constants. No flag settings are required for a connection to WinRM on the local computer. The default is WSManFlagUseNegotiate.
For more information, see Authentication for Remote Connections and the connectionOptions parameter.
connectionOptions [in, optional]
A pointer to a ConnectionOptions object that contains a user name and password. The default is NULL.
A Session object that can then be used to perform local or remote WinRM operations.
The CreateSession method initializes the Session object by gathering parameters, such as flags, credentials, and a connection string for the connection parameter. CreateSession does not actually connect to the local or remote computer. If the connection cannot be established, a failure occurs on the first Session operation, such as a Get or Enumerate, after the call to CreateSession. This behavior differs from a WMI connection to a namespace on a remote computer. For more information, see Windows Remote Management and WMI.
The following VBScript code example is used to call this method.
Set session = _ wsman.CreateSession("<Transport><Address><Suffix>")
The following examples show the different formats used to specify connection information in the connection parameter (when creating an HTTPS session, the <Address> field must match the server computer certificate name, otherwise a failure occurs):
Uses HTTPS to connect to the default web service location.
Uses HTTPS to connect to the specific web service location.
Uses HTTPS and IPv6 with the default port.
Uses HTTPS and IPv6 with the given port.
The following VBScript code example creates a session on the local computer.
Set NewSession = Wsman.CreateSession
The following VBScript code example creates a session on a remote computer that is identified by an IP address. The script supplies a user name and password for an account. The flags WSManFlagCredUserNamePassword and WSManFlagUseBasic are combined to indicate that the account is a local account on the remote computer. If the creation of the session fails, the script terminates. The script uses the methods that return the constant, such as WSMan.SessionFlagUseBasic.
To run this script, be aware that you must configure the default configuration settings for both client and server to allow unencrypted traffic and Basic authentication (AllowUnencrypted set to True and Basic set to True). For more information, see Installation and Configuration for Windows Remote Management.
iFlags = WSMan.SessionFlagUseBasic Or WSMan.SessionFlagCredUsernamePassword Set Options = Wsman.CreateConnectionOptions Options.Username = "MyUserName" Options.Password = "MyPassword" Set NewSession = WSMan.CreateSession("127.0.51.1", iFlags, _ Options)
In the following VBScript code example, the account is a domain account and Negotiate authentication is used. With Negotiate authentication, you must specify the user name as
iFlags = WSMan.SessionFlagUseNegotiate Or WSMan.SessionFlagCredUsernamePassword Set Options = Wsman.CreateConnectionOptions Options.Username = "MyComputer\MyUserName" Options.Password = "MyPassword" Set NewSession = WSMan.CreateSession("127.0.51.1", iFlags, _ Options)
|Minimum supported client
|Minimum supported server
||Windows Server 2008