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 { throw }
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 ...