3

I am trying to find the number of partitions on a VHD of Hyper-V 2012 through a script. Currently I am doing this using a VBScript and a PowerShell script. For this I am first using a WMI query to find out the disk number (of the VHD) in the VBScript and passing this disk number as an argument to the PowerShell script, which gives the number of partitions.

Here are my questions:

  1. Is there any direct way to find the attached VHD's number of partitions through script? If yes, please share.

  2. The approach I am using in VBScript to find the disk number gives me different disk number of vhd every time depending on the disks attached (physical hard drive, VHD, backup disk, USB) to system in order. This could be wrong some time. Please tell me how to find out the disk number of specified VHD attached to the system.

Code in VBScript to find out the disk number:

strComputer = "."

Dim i

i = 0

Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" _
  & strComputer & "\root\cimv2")

Set colQuotas = objWMIService.ExecQuery _
  ("Select * from Win32_DiskQuota")

For each objQuota in colQuotas
  i= i+1
Next

i = i - 1 

Wscript.Echo i 

PowerShell script code:

$DN = $args[0]

Write-Host "disk number  $DN"

$ObjPartition = Get-Partition -DiskNumber DN | measure

$NoOfPartition = $ObjPartition.count

Write-Host "No of Partition $NoOfPartition"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kanvas
  • 165
  • 2
  • 7
  • 23

1 Answers1

2

You could automate diskpart in PowerShell to determine the disk IDs and paths, and then use the IDs in a WMI query to determine the partition number. Example:

('list vdisk' | diskpart) -match '\.vhd' | % {
  $a = $_.Trim() -split '  +', 5
  New-Object -Type PSObject -Property @{
    'ID'   = $a[1] -replace 'Disk '
    'Path' = $a[4]
  }
} | % {
  $flt = "DeviceID='\\\\.\\PHYSICALDRIVE$($_.ID)'"
  $partitions = gwmi Win32_DiskDrive -Filter $flt | select -Expand Partitions

  "{0}`t{1}`t{2}" -f $_.ID, $partitions, $_.Path
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks Ansgar it works. could you please tell me how to catch the output received by powershell ript. Dim strOutput strOutput = objShell.run("powershell -ExecutionPolicy unrestricted -noexit -file C:\ab\NoOfPartition.ps1 DiskNumber") WScript.echo strOutput // this gives 0 in echo – Kanvas Dec 04 '13 at 12:33
  • @Kanvas I would just drop the VBScript entirely. You don't need it and there is very little that PowerShell doesn't do better. If you must stick to VBScript: use the [`Exec`](http://msdn.microsoft.com/en-us/library/ateytk4a%28v=vs.84%29.aspx) method. `Run` doesn't give you access to the started program's `StdOut`. It just returns the program's exit code. – Ansgar Wiechers Dec 04 '13 at 14:43
  • is it possible to find this information only for a specific(provided) VHD. As list vdisk in diskpart gives list of all the vhd's attached to the system.Even this code gives information of those vhd's who was attached earlier but now detached from the system. – Kanvas Dec 27 '13 at 08:44
  • @Kanvas I'm not aware of a way to make `diskpart` list results selectively. You need to filter its output yourself. – Ansgar Wiechers Jan 03 '14 at 10:19