Using CIM sessions

Once a CIM session has been created, it can be used for one or more requests. In the following example, a CIM session is created and then used to gather disk and partition information:

$ErrorActionPreference = 'Stop' 
try { 
    $session = New-CimSession -ComputerName $env:COMPUTERNAME 
    Get-Disk -CimSession $session 
    Get-Partition -CimSession $session 
} catch { 

In the preceding script, if the attempt to create the session succeeds, the session will be used to get disk and partition information.

Error handling with try and catch is discussed in Chapter 17, Error Handling. The block is treated as a transaction; if a single command fails, the block will stop running. If the attempt to create a new session fails, Get-Disk ...

Get Mastering Windows PowerShell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.