Hello there,
I'm quite new to writing scripts and recently I created a monstrosity below.
The problem is with the results. I have no idea why in the results I get the VM name and it's datastore over and over again until all of the disks are highlighted.
Any help will be very very very appreciated.
The script:
$raport = @()
Foreach ($i in (Get-Content 'Y:\vms.txt'))
{
$rekord = ""
$vm = get-vm -name "$i"
$dysk1 = $vm | Get-HardDisk | where {$_.Name -eq "Hard disk 1"}
$datastore1 = $dysk1.filename.split("]")[0].split("[")[1]
$disks = $vm | Get-HardDisk
foreach ($disk in $disks){
$hd = ""
$disksize = ($disk.CapacityGB)
$hd += $disksize
$rekord += $vm.name + ";" + $datastore1 + ";" + $hd}
$raport += $rekord
}
$raport | out-file "Y:\dyski.csv"
The results in csv:
VM1Name;datastoreName;60;VM1Name:datastoreName;50;VM1Name;datastoreName;40
VM2Name;datastoreName;45;VM2Name;datastoreName;40
The dream results in csv:
VM1Name;datastoreName;60;50;40
VM2Name;datastoreName;45;40
etc
Try like this
$raport = @()
Foreach ($i in (Get-Content 'Y:\vms.txt'))
{
$rekord = ""
$vm = get-vm -name "$i"
$disk = $vm | Get-HardDisk
$datastore = ($disk | where {$_.Name -eq "Hard disk 1"}).filename.split("]")[0].split("[")[1]
$rekord = "$($vm.Name);$($datastore);$(($disk | select -ExpandProperty CapacityGB) -join '|')"
$raport += $rekord
}
$raport | out-file "Y:\dyski.csv"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$raport = @()
Foreach ($i in (Get-Content 'Y:\vms.txt'))
{
$rekord = ""
$vm = get-vm -name "$i"
$disk = $vm | Get-HardDisk
$datastore = ($disk | where {$_.Name -eq "Hard disk 1"}).filename.split("]")[0].split("[")[1]
$rekord = "$($vm.Name);$($datastore);$(($disk | select -ExpandProperty CapacityGB) -join '|')"
$raport += $rekord
}
$raport | out-file "Y:\dyski.csv"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You kind Sir are the best.
Thank you very much!