Using PowerShell, WMI and diskpart to bring disks online

I use iSCSI LUNs and Windows Server Backup to back up my virtual machines. So far that has worked ok, but sometimes when a server reboots it will map the iSCSI LUN but fail to bring the disk online. Of course this leads to backups failing:

And as it turns out, the backup disk is indeed offline:

Using diskpart, the commands to rectify this would now be:

select disk 1
online disk

But of course I don’t want to do that manually, and besides it’s not always “disk 1”. Some PowerShell should however fix this.

Using the “Win32_DiskDrive” WMI Class I can display information about the physical disks in my machine:

All my iSCSI LUNs are being mapped from a Synology NAS, so I’ll use the model name property to narrow down the selection. The property I need for diskpart is “index”:

An example of a system with multiple iSCSI LUNs:

The script will do a “foreach” through all drives matching the filter criteria, build a diskpart command batch and pipe it to diskpart.exe:

$iSCSI = gwmi win32_diskdrive | where {$_.model -like "synology*"}
foreach ($disk in $iSCSI) 
{ 
$diskID = $disk.index
$dpscript = @"
select disk $diskID
online disk noerr
"@
$dpscript | diskpart
}

The result:

If the disk was already online the script will just continue with the next disk, courtesy of the “noerr” parameter in the diskpart batch.

One thought on “Using PowerShell, WMI and diskpart to bring disks online

Leave a Reply

Your email address will not be published. Required fields are marked *