如何通过脚本查找虚拟硬盘的分区数?(How to find number of partition of a virtual hard disk through script?)

编程入门 行业动态 更新时间:2024-10-24 09:30:24
如何通过脚本查找虚拟硬盘的分区数?(How to find number of partition of a virtual hard disk through script?)

我试图通过脚本找到Hyper-V 2012的VHD上的分区数。 目前我正在使用VBScript和PowerShell脚本执行此操作。 为此,我首先使用WMI查询来查找VBScript中的磁盘编号(VHD),并将此磁盘编号作为参数传递给PowerShell脚本,该脚本提供了分区数。

这是我的问题:

有没有直接的方法通过脚本找到附加VHD的分区数? 如果是,请分享。

我在VBScript中使用的方法来查找磁盘号,每次都给我不同的磁盘号vhd,具体取决于连接的磁盘(物理硬盘,VHD,备份磁盘,USB)到系统。 这可能是错误的一段时间。 请告诉我如何找出连接到系统的指定VHD的磁盘编号。

在VBScript中查找代码以查找磁盘编号:

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脚本代码:

$DN = $args[0] Write-Host "disk number $DN" $ObjPartition = Get-Partition -DiskNumber DN | measure $NoOfPartition = $ObjPartition.count Write-Host "No of Partition $NoOfPartition"

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:

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

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"

最满意答案

您可以在PowerShell中自动执行diskpart以确定磁盘ID和路径,然后使用WMI查询中的ID来确定分区号。 例:

('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
}

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
}

                    
                     
          

更多推荐

本文发布于:2023-08-06 14:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分区   脚本   硬盘   find   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!