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 { 
    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 21, 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 and Get-Partition will not run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.255