New CimSession - PowerShell


Overview

New-CimSession creates a new Common Information Model (CIM) session that establishes a connection to a remote computer for managing and querying WMI resources. It enables administrators to perform remote management and scripting tasks on remote systems.

Syntax

New-CimSession [-ComputerName] <string[]> [-Credential] <PSCredential> [-Port] <int> [-Authentication] <AuthenticationLevel> [-OperationTimeoutSec] <int> [-EnableNetworkAccess] [-UseSSL] [-UseHttp] [-SessionOption] <WSManSessionOption[]>

Options/Flags

  • -ComputerName: Specifies the name of the remote computer to connect to. Multiple computer names can be provided for establishing concurrent sessions to multiple computers.
  • -Credential: Indicates the credentials to use for authentication. If not specified, the current user’s credentials are used.
  • -Port: Sets the port number for the CIM session connection. Default is 5985.
  • -Authentication: Configures the authentication level for the session. Valid options are Default, Packet, CredSSP. Default is Default.
  • -OperationTimeoutSec: Defines the maximum time (in seconds) a session command can execute before timing out. Default is 120 seconds.
  • -EnableNetworkAccess: Allows the session to access network resources, such as files and printers, on the remote computer. This is disabled by default.
  • -UseSSL: Encrypts the session communication using SSL. Default is $false.
  • -UseHttp: Uses the HTTP protocol for session communication instead of HTTPS. Default is $false.
  • -SessionOption: Specifies additional options for the session, such as session locale, proxy settings, and more.

Examples

  • Connect to a remote computer using the default credentials:
New-CimSession -ComputerName hostname
  • Connect to a remote computer with specific credentials:
$cred = Get-Credential
New-CimSession -ComputerName hostname -Credential $cred
  • Connect to multiple remote computers concurrently:
New-CimSession -ComputerName server1,server2,server3
  • Connect to a remote computer using HTTP and enable network access:
New-CimSession -ComputerName hostname -UseHttp -EnableNetworkAccess

Common Issues

  • Connection issues: Ensure that the remote computer is accessible and that the firewall is not blocking necessary ports. Verify that the credentials used have sufficient permissions.
  • Authentication failures: Check if the specified credentials are correct and have the appropriate permissions on the remote computer. Ensure that the authentication level configured matches the remote computer’s requirements.
  • Timeout errors: Increase the -OperationTimeoutSec value if commands are taking longer than expected to execute.

Integration

New-CimSession can be integrated with other PowerShell commands for advanced tasks:

  • Get-CimInstance: Retrieve WMI instances from the remote computer.
  • Invoke-CimMethod: Invoke methods on WMI classes on the remote computer.
  • Set-CimInstance: Modify or create WMI instances on the remote computer.
  • Register-CimIndicationEvent: Register for and receive WMI events from the remote computer.
  • Get-CimSession: Retrieves existing CIM sessions.
  • Remove-CimSession: Disconnects and removes CIM sessions.
  • Enter-CimSession: Initiates an interactive session on the remote computer.