A great feature of many of the newer PowerShell cmdlets is that they use CIM sessions. This makes working with multiple remote machines a lot easier:
We can then set up a CIM session pointing at multiple remote machines, and re-use that session to repeatedly perform actions against those same machines.
Today, I had to clean up some old iSCSI target settings for a particular SAN volume on a few Hyper-V servers. Rather than messing around on each server individually, here’s what I did.
To set up the CIM session:
$sess = New-CimSession -ComputerName hyperv01,hyperv02,hyperv03
To view the iSCSI targets for the volume in question, a volume named VOL-TEST-1 on a HP LeftHand/StoreVirtual SAN:
Get-IscsiTarget -NodeAddress "*lefthand*vol-test-1" -CimSession $sess
To disconnect those targets:
Disconnect-IscsiTarget -NodeAddress "*lefthand*vol-test-1" -CimSession $sess
DISCLAIMER: Note that although PowerShell does prompt for confirmation before disconnecting the sessions, you need to exercise extreme caution with commands like this to ensure that you don’t disconnect sessions that are in use.