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}
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
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
That is prefect, but I am somewhat confused by the $esx in $hosts
$host = Get-VMHost | Get-View
whats does $esx = ?
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
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.
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
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!