I currently have a script that queries some hyperV hosts in our environment (These are not clustered).
The script asks the user for input of the name of the HyperV Hosts they would like to query for guest Virtual Disks. The first part of the script will return the host that the VM is housed on, the name of the VM and the location of the VHD.
The second portion of the script then queries the size on disk for each of the VM's VHD's.
Both portions of the script output what I want them to do but I am unable to join them into a single variable or export them to CSV in a standard format.
When I get the results from the first portion of the script $Results1
Host, VMName, Path
Result, Result, Result
When I get the results from the second portion of the script $Results2
Size
Result
When I try to combined them by doing $Results1 + $Results2 = $Results
The output only shows $Results1 and a bunch of blank space below it.
The same happens when I do the following:
$Results1 += $Results2
$Results1
Script below:
$Hosts = read-host 'What are the name of the host you would like to check (Comma separated for multiple, no spaces.)?'
$Hosts = $Hosts.Split(",")
$Results1 = @()
foreach ($H in $Hosts)
{
$Results1 += Get-VM –ComputerName $H | Where State -Like '*running*' |
Get-VMHardDiskDrive |
Select-Object -Property @{n='Host';e={$_.ComputerName}}, VMName, Path |
Sort-Object -Property VMName
}
$Results2 = @()
foreach ($H in $Hosts)
{
$IDs = get-vm -ComputerName $H | Select -Property ID
foreach ($ID in $IDs)
{
$results2 += get-vhd -ComputerName $H -VMId $ID.id | select @{n='Size';e={$_.Size / 1GB -as [int]}}
}
}