VMware Cloud Community
NomPrenom
Contributor
Contributor
Jump to solution

Help with begginer's script for datastore and harddisks

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
NomPrenom
Contributor
Contributor
Jump to solution

You kind Sir are the best.

Thank you very much!

0 Kudos