Hi everyone thanks for reading my question. I have something like below that works:<\/p>\n
$clustercheck = Get-Cluster\n $clshostreport = @()\n $clsdatastorereport = @()\n foreach($clsname in $clustercheck){\n\n $hosts = Get-VMHost -Location $clsname | select -expandproperty name\n foreach($clshost in $hosts){\n $hostcputotal = [Math]::Round(((get-vmhost -name $clshost | select -expandproperty cputotalmhz)*2),2)\n $hostcpuusage = [Math]::Round((((get-vmhost -name $clshost | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average)),2)\n $hostmemtotal = [Math]::Round((get-vmhost -name $clshost | select -expandproperty memorytotalgb),2)\n $hostmemusage = [Math]::Round((get-vmhost -name $clshost | select -expandproperty memoryusagegb),2)\n $freecpuperc = [Math]::Round(((1 - ($hostcpuusage/$hostcputotal))*100),2)\n $freememperc = [Math]::Round(((1 - ($hostmemusage/$hostmemtotal))*100),2)\n $clshostreport += [PSCustomObject]@{\n Hostname = (get-vmhost -name $clshost | select -expandproperty name )\n HostCPUTotalMHz = $hostcputotal\n HostCPUUsageMHz = $hostcpuusage\n HostCPUFreePercentage = $freecpuperc\n HostMemTotalGB = $hostmemtotal\n HostMemUsageGB = $hostmemusage\n HostMemFreePercentage = $freememperc\n\n }\n\n $clshostreport | select Hostname,HostCPUTotalMHz,HostCPUUsageMHz,HostCPUFreePercentage,HostMemTotalGB,HostMemUsageGB,HostMemFreePercentage | Export-Csv .\\clshostreport.csv -NoTypeInformation -encoding \"unicode\"\n \n }\n\n $clsdatastore = get-cluster -name $clsname | get-datastore | where {$_.ExtensionData.Summary.MultipleHostAccess}\n foreach ($clsds in $clsdatastore){\n\n $clsstgtotal = $clsds | select-object -expandproperty capacityGB\n $clsstgfree = $clsds | select -expandproperty freespaceGB \n $clsstgfreeperc = ($clsstgfree/$clsstgtotal)*100\n $clsdatastorereport += [PSCustomObject] @{\n DsName = $clsds.Name\n TotalSpace = [Math]::Round($clsstgtotal,2)\n FreeSpace = [Math]::Round($clsstgfree,2)\n FreeSpacePercentage = [Math]::Round($clsstgfreeperc,2)\n\n }\n\n $clsdatastorereport | select DsName, TotalSpace, FreeSpace, FreeSpacePercentage | Export-Csv .\\clsdsreport.csv -NoTypeInformation -encoding \"unicode\"\n \n }\n\n }\n<\/code><\/pre>\n
Advertisement
As you can see I have two foreach loops which output is two different .csv files.<\/p>\n
Advertisement
I want them to be in one .csv, with first foreach’s output on top and second foreach at the bottom.<\/p>\n
I tried adding putting the same .csv files while the second foreach i added -append like<\/p>\n
$clsdatastorereport | select DsName, TotalSpace, FreeSpace, FreeSpacePercentage | Export-Csv .\\clshostreport.csv -append -NoTypeInformation -encoding “unicode”<\/p>\n
but the the second foreach loop output wont come out.<\/p>\n
Any help is appreciated. Thanks<\/p>","upvoteCount":4,"answerCount":4,"datePublished":"2022-02-10T07:43:15.000Z","author":{"@type":"Person","name":"kurolearnstocode","url":"https://community.spiceworks.com/u/kurolearnstocode"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi everyone thanks for reading my question. I have something like below that works:<\/p>\n
$clustercheck = Get-Cluster\n $clshostreport = @()\n $clsdatastorereport = @()\n foreach($clsname in $clustercheck){\n\n $hosts = Get-VMHost -Location $clsname | select -expandproperty name\n foreach($clshost in $hosts){\n $hostcputotal = [Math]::Round(((get-vmhost -name $clshost | select -expandproperty cputotalmhz)*2),2)\n $hostcpuusage = [Math]::Round((((get-vmhost -name $clshost | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average)),2)\n $hostmemtotal = [Math]::Round((get-vmhost -name $clshost | select -expandproperty memorytotalgb),2)\n $hostmemusage = [Math]::Round((get-vmhost -name $clshost | select -expandproperty memoryusagegb),2)\n $freecpuperc = [Math]::Round(((1 - ($hostcpuusage/$hostcputotal))*100),2)\n $freememperc = [Math]::Round(((1 - ($hostmemusage/$hostmemtotal))*100),2)\n $clshostreport += [PSCustomObject]@{\n Hostname = (get-vmhost -name $clshost | select -expandproperty name )\n HostCPUTotalMHz = $hostcputotal\n HostCPUUsageMHz = $hostcpuusage\n HostCPUFreePercentage = $freecpuperc\n HostMemTotalGB = $hostmemtotal\n HostMemUsageGB = $hostmemusage\n HostMemFreePercentage = $freememperc\n\n }\n\n $clshostreport | select Hostname,HostCPUTotalMHz,HostCPUUsageMHz,HostCPUFreePercentage,HostMemTotalGB,HostMemUsageGB,HostMemFreePercentage | Export-Csv .\\clshostreport.csv -NoTypeInformation -encoding \"unicode\"\n \n }\n\n $clsdatastore = get-cluster -name $clsname | get-datastore | where {$_.ExtensionData.Summary.MultipleHostAccess}\n foreach ($clsds in $clsdatastore){\n\n $clsstgtotal = $clsds | select-object -expandproperty capacityGB\n $clsstgfree = $clsds | select -expandproperty freespaceGB \n $clsstgfreeperc = ($clsstgfree/$clsstgtotal)*100\n $clsdatastorereport += [PSCustomObject] @{\n DsName = $clsds.Name\n TotalSpace = [Math]::Round($clsstgtotal,2)\n FreeSpace = [Math]::Round($clsstgfree,2)\n FreeSpacePercentage = [Math]::Round($clsstgfreeperc,2)\n\n }\n\n $clsdatastorereport | select DsName, TotalSpace, FreeSpace, FreeSpacePercentage | Export-Csv .\\clsdsreport.csv -NoTypeInformation -encoding \"unicode\"\n \n }\n\n }\n<\/code><\/pre>\nAs you can see I have two foreach loops which output is two different .csv files.<\/p>\n
I want them to be in one .csv, with first foreach’s output on top and second foreach at the bottom.<\/p>\n
I tried adding putting the same .csv files while the second foreach i added -append like<\/p>\n
$clsdatastorereport | select DsName, TotalSpace, FreeSpace, FreeSpacePercentage | Export-Csv .\\clshostreport.csv -append -NoTypeInformation -encoding “unicode”<\/p>\n
but the the second foreach loop output wont come out.<\/p>\n
Any help is appreciated. Thanks<\/p>","upvoteCount":4,"datePublished":"2022-02-10T07:43:15.000Z","url":"https://community.spiceworks.com/t/second-foreach-loop-will-not-append-to-export-csv/824684/1","author":{"@type":"Person","name":"kurolearnstocode","url":"https://community.spiceworks.com/u/kurolearnstocode"}},{"@type":"Answer","text":"