0
這段代碼幾乎可以工作,它將VWare VMDK映射到Windows驅動器。代碼不是我的。VMWare VMDK映射到Windows驅動器號
在其他的信息也將返回「DD-SERV-01_15.vmdk爲VM 主要是驅動器盤符G:」
腳本會提示輸入憑據,並進行映射但是不順心錯誤的,只有最後一個虛擬機/驅動器被保存爲輸出 - 我希望有人可以看看並更新/修復代碼,以便它可以保存所有輸出嗎?
謝謝。
#Get VMware Disk Usage
# Created by Hugo Peeters
# http://www.peetersonline.nl
# VARIABLES
$Decimals = 1
$VCServer = "SERVERNAME"
# SCRIPT
# Connect to VC
Write-Progress "Gathering Information" "Connecting to Virtual Center" -Id 0
$VC = Connect-VIServer $VCServer
# Create Output Collection
$myCol = @()
# List Datastores (Datastore Name)
Write-Progress "Gathering Information" "Listing Datastores" -Id 0
$Datastores = Get-Datastore | Sort Name
# List vms
Write-Progress "Gathering Information" "Listing VMs and Disk Files" -Id 0
$VMSummaries = @()
ForEach ($vm in (Get-VM))
\t {
\t $VMView = $VM | Get-View
\t ForEach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match "SCSI Controller"}))
\t \t {
\t \t ForEach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | Where {$_.ControllerKey -eq $VirtualSCSIController.Key}))
\t \t \t {
\t \t \t $VMSummary = "" | Select VM, HostName, PowerState, DiskFile, DiskName, DiskSize, SCSIController, SCSITarget
\t \t \t $VMSummary.VM = $VM.Name
\t \t \t $VMSummary.HostName = $VMView.Guest.HostName
\t \t \t $VMSummary.PowerState = $VM.PowerState
\t \t \t $VMSummary.DiskFile = $VirtualDiskDevice.Backing.FileName
\t \t \t $VMSummary.DiskName = $VirtualDiskDevice.DeviceInfo.Label
\t \t \t $VMSummary.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB
\t \t \t $VMSummary.SCSIController = $VirtualSCSIController.BusNumber
\t \t \t $VMSummary.SCSITarget = $VirtualDiskDevice.UnitNumber
\t \t \t $VMSummaries += $VMSummary
\t \t \t }
\t \t }
\t Clear-Variable VMView -ErrorAction SilentlyContinue
\t }
# Loop through Datastores
ForEach ($Datastore in $Datastores)
\t {
\t # List vmdk files in datastore (vmdk Name)
\t Write-Progress "Gathering Information" ("Processing Datastore {0}" -f $Datastore.Name) -Id 0
\t $DSView = $Datastore | Get-View
\t $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
\t $fileQueryFlags.FileSize = $true
\t $fileQueryFlags.FileType = $true
\t $fileQueryFlags.Modification = $true
\t $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
\t $searchSpec.details = $fileQueryFlags
\t $searchSpec.sortFoldersFirst = $true
\t $dsBrowser = Get-View $DSView.browser
\t $rootPath = "["+$DSView.summary.Name+"]"
\t $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
\t ForEach ($result in $searchResult)
\t \t {
\t \t ForEach ($vmdk in ($result.File | ?{$_.Path -like "*.vmdk"} | Sort Path))
\t \t \t {
\t \t \t Write-Progress "Gathering Information" ("Processing VMDK {0}" -f $vmdk.Path) -Id 1
\t \t \t Write-Host "=============================================================================="
\t \t \t # Find vm using the vmdk (VM Name)
\t \t \t $VMRef = ($VMSummaries | ?{$_.DiskFile -match $Datastore.Name -and $_.DiskFile -match $vmdk.Path})
\t \t \t "VMDK {0} belongs to VM {1}" -f $vmdk.Path, $VMRef.VM
\t \t \t If ($VMRef.Powerstate -eq "PoweredOn")
\t \t \t \t {
\t \t \t \t Write-Host "VM is powered on" -ForegroundColor "yellow"
\t \t \t \t $Partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $VMRef.HostName
\t \t \t \t If ($?)
\t \t \t \t \t {
\t \t \t \t \t $Disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VMRef.HostName
\t \t \t \t \t $LogicalDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $VMRef.HostName
\t \t \t \t \t $DiskToPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $VMRef.HostName
\t \t \t \t \t $LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $VMRef.HostName
\t \t \t \t \t Write-Host "Read partition and disk information" -ForegroundColor "yellow"
\t \t \t \t \t # Match disk based on SCSI ID's
\t \t \t \t \t $DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
\t \t \t \t \t If ($DiskMatch -eq $null){Write-Warning "NO MATCHES!"}
\t \t \t \t \t Else
\t \t \t \t \t \t {
\t \t \t \t \t \t Write-Host "Found match:" -ForegroundColor "yellow"
\t \t \t \t \t \t $DiskMatch
\t \t \t \t \t \t # Find the Partition(s) on this disk
\t \t \t \t \t \t $PartitionsOnDisk = ($DiskToPartition | ?{$_.Antecedent -eq $DiskMatch.__PATH})
\t \t \t \t \t \t If ($PartitionsOnDisk -eq $null){Write-Warning "NO PARTITIONS!"}
\t \t \t \t \t \t Else
\t \t \t \t \t \t \t {
\t \t \t \t \t \t \t ForEach ($PartitionOnDisk in $PartitionsOnDisk)
\t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t Write-Host "Disk contains partition" -ForegroundColor "yellow"
\t \t \t \t \t \t \t \t $PartitionOnDisk.Dependent
\t \t \t \t \t \t \t \t $PartitionMatches = $Partitions | ?{$_.__PATH -eq $PartitionOnDisk.Dependent}
\t \t \t \t \t \t \t \t ForEach ($PartitionMatch in $PartitionMatches)
\t \t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t \t $LogicalDiskRefs = $LogicalDiskToPartition | ?{$_.Antecedent -eq $PartitionMatch.__PATH}
\t \t \t \t \t \t \t \t \t If ($LogicalDiskRefs -eq $null)
\t \t \t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t \t \t Write-Warning "NO LOGICAL DISKS!"
\t \t \t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t \t \t Else
\t \t \t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t \t \t ForEach ($LogicalDiskRef in $LogicalDiskRefs)
\t \t \t \t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t \t \t \t $LogicalDiskMatches = $LogicalDisks | ?{$_.__PATH -eq $LogicalDiskRef.Dependent}
\t \t \t \t \t \t \t \t \t \t \t ForEach ($LogicalDiskMatch in $LogicalDiskMatches)
\t \t \t \t \t \t \t \t \t \t \t \t {
\t \t \t \t \t \t \t \t \t \t \t \t Write-Host "Matching Logical Disk:" -ForegroundColor "yellow"
\t \t \t \t \t \t \t \t \t \t \t \t $LogicalDiskMatch
\t \t \t \t \t \t \t \t \t \t \t \t # Create Output Object
\t \t \t \t \t \t \t \t \t \t \t \t $myObj = "" | Select Datastore, DSSizeGB, DSFreeGB, DSPercentFree, DiskFile, VM, HardDisk, DriveLetter, DiskSizeGB, DiskFreeGB, PercFree
\t \t \t \t \t \t \t \t \t \t \t \t # List datastore name
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.Datastore = $Datastore.Name
\t \t \t \t \t \t \t \t \t \t \t \t # Determine datastore size in GB
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DSSizeGB = [Math]::Round(($Datastore.CapacityMB * 1MB/1GB),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DSFreeGB = [Math]::Round(($Datastore.FreeSpaceMB * 1MB/1GB),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t # Determine datastore free space (DS%Free)
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DSPercentFree = [Math]::Round((100*($Datastore.FreeSpaceMB/$Datastore.CapacityMB)),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t # List disk file name
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DiskFile = $vmdk.Path
\t \t \t \t \t \t \t \t \t \t \t \t # List VM Name
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.VM = $VMRef.VM
\t \t \t \t \t \t \t \t \t \t \t \t # Determine virtual hard disk/logical drive
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.HardDisk = $VMRef.DiskName
\t \t \t \t \t \t \t \t \t \t \t \t # Report driveletter
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DriveLetter = $LogicalDiskMatch.DeviceID
\t \t \t \t \t \t \t \t \t \t \t \t # Report Size
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DiskSizeGB = [Math]::Round(($LogicalDiskMatch.Size/1GB),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t # Report Free Space
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.DiskFreeGB = [Math]::Round(($LogicalDiskMatch.FreeSpace/1GB),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t # Calculate Percentage free space
\t \t \t \t \t \t \t \t \t \t \t \t $myObj.PercFree = [Math]::Round((100 * ([int]($LogicalDiskMatch.FreeSpace/1MB)/[int]($LogicalDiskMatch.Size/1MB))),$Decimals)
\t \t \t \t \t \t \t \t \t \t \t \t Write-Host "RESULT:" -ForegroundColor "yellow"
\t \t \t \t \t \t \t \t \t \t \t \t $myObj
\t \t \t \t \t \t \t \t \t \t \t \t # Add output object to output collection
\t \t \t \t \t \t \t \t \t \t \t \t $myCol += $myObj
\t \t \t \t \t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t \t \t \t \t Clear-Variable LogicalDiskMatches -ErrorAction SilentlyContinue
\t \t \t \t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t \t \t Clear-Variable LogicalDiskRefs -ErrorAction SilentlyContinue
\t \t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t \t Clear-Variable PartitionMatches -ErrorAction SilentlyContinue
\t \t \t \t \t \t \t \t }
\t \t \t \t \t \t \t }
\t \t \t \t \t \t Clear-Variable PartitionsOnDisk -ErrorAction SilentlyContinue
\t \t \t \t \t \t }
\t \t \t \t \t Clear-Variable DiskMatch -ErrorAction SilentlyContinue
\t \t \t \t \t Clear-Variable Disks -ErrorAction SilentlyContinue
\t \t \t \t \t Clear-Variable LogicalDisks -ErrorAction SilentlyContinue
\t \t \t \t \t Clear-Variable DiskToPartition -ErrorAction SilentlyContinue
\t \t \t \t \t Clear-Variable LogicalDiskToPartition -ErrorAction SilentlyContinue
\t \t \t \t \t }
\t \t \t \t Clear-Variable Partitions -ErrorAction SilentlyContinue
\t \t \t \t }
\t \t \t Else
\t \t \t \t {
\t \t \t \t Write-Host "VM is powered off" -ForegroundColor "yellow"
\t \t \t \t }
\t \t \t Clear-Variable VMRef -ErrorAction SilentlyContinue
\t \t \t Write-Progress "Gathering Information" ("Processing VMDK {0}" -f $vmdk.Path) -Id 1 -Completed
\t \t \t }
\t \t }
\t }
# Disconnect from VC
Disconnect-VIServer -Confirm:$False
# OUTPUT
Write-Host "==================================================="
Write-Host "==================================================="
$TotalDSFree = ($myCol | Select Datastore, DSFreeGB -Unique | Measure-Object DSFreeGB -Sum).Sum
$TotalDSSize = ($myCol | Select Datastore, DSSizeGB -Unique | Measure-Object DSSizeGB -Sum).Sum
$AverageDSFree = [Math]::Round(100 * ($TotalDSFree/$TotalDSSize),$Decimals)
$AverageDiskFree = [Math]::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum/($myCol | Measure-Object DiskSizeGB -Sum).Sum),$Decimals)
Write-Host "Total DS Free: $TotalDSFree"
Write-Host "Total DS Size: $TotalDSSize"
Write-Host "Average DS Free Percentage: $AverageDSFree"
Write-Host "Average Disk Free Percentage: $AverageDiskFree"
$myCol | Export-Csv -NoTypeInformation 'C:\TEMP\VMwareDiskUsage.csv'
大家好 - 任何人能幫助嗎?我迫切需要記錄一個現有的系統,並且我需要確保我正在接觸正確的Windows卷,因爲它們大多數都是Thin Provisioned和相同的大小。 –