我试图通过脚本找到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 iPowerShell脚本代码:
$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 iPowerShell 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 }
更多推荐
发布评论