VMware Cloud Community
dmn0211
Enthusiast
Enthusiast
Jump to solution

Output from Foreach loop

I need a way to output the information for NTP entries from each host. I am using the following script to obtain the information and output it to the console, just can not figure out how to get it to output to a file for insertion to a report.

$row.ntp = $ntps=$hosts.config.datetimeinfo.ntpconfig; foreach ($ntp in $ntps) {Write-output $vmh.name $dns.server}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure your lines of code came through as you intended ?

But you try something like this to get the report in CSV format

$report = @()
$hosts = Get-VMHost | Get-View

foreach($esx in $hosts){
	foreach($ntp in $esx.config.datetimeinfo.ntpconfig.server){ 
		$row = "" | Select-Object Hostname, NTPServer
		$row.Hostname = $esx.name
		$row.NTPServer = $ntp
		$report += $row
	}
}
$report | Export-Csv -noTypeInformation "C:\ntpservers.csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure your lines of code came through as you intended ?

But you try something like this to get the report in CSV format

$report = @()
$hosts = Get-VMHost | Get-View

foreach($esx in $hosts){
	foreach($ntp in $esx.config.datetimeinfo.ntpconfig.server){ 
		$row = "" | Select-Object Hostname, NTPServer
		$row.Hostname = $esx.name
		$row.NTPServer = $ntp
		$report += $row
	}
}
$report | Export-Csv -noTypeInformation "C:\ntpservers.csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dmn0211
Enthusiast
Enthusiast
Jump to solution

That is prefect, but I am somewhat confused by the $esx in $hosts

$host = Get-VMHost | Get-View

whats does $esx = ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Get-VMHost returns 1 or more ESX servers as (a) VMHostImpl object(s).

To get at the NTP configuration we need to convert the VMHostImpl object to a HostSystem object.

Independent if we have 1 or more ESX hosts, PowerShell will exeute the Get-View cmdlet on each object that is passed through the pipe.

The result (1 or more HostSystem objects) is/are stored in the variable $hosts.

In the foreach loop the script takes each HostSystem in turn and stores the object in the variable $esx.

Thanks to the foreach statement we don't have to worry if there are 1 or HostSystem objects in the variable $hosts.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dmn0211
Enthusiast
Enthusiast
Jump to solution

Ok, so the "HostSystem in turn and stores the object in the variable $esx" is by default, since I did not see the variable declared.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In PowerShell you don't have to declare variables in advance.

In fact the foreach statement handles the assignment to the $esx variable.

The foreach statement runs through the array ($hosts) and assigns each element of the array in turn to the variable $esx.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dmn0211
Enthusiast
Enthusiast
Jump to solution

That will help in the future. Thank you for the input and feedback. We are lucky to have a guy like you around on the forum!

0 Kudos