Hi Friends!
I need to read a content hosts using "get-content" but there is something wrong in script below , and i need to select only ethernet adapter! can anyone help me ? thank you
Write "Gathering VMHost objects"
$vmhosts = Get-Content C:\Users\c1217635\Documents\WindowsPowerShell\scripts\hostlist.txt
#$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
$MyCol = @()
foreach ($vmhost in $vmhosts){
$ESXHost = $vmhost.Name
Write "Collating information for $ESXHost"
$networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
foreach($pnic in $networkSystem.NetworkConfig.Pnic){
$pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
foreach($Hint in $pnicInfo){
$NetworkInfo = "" | select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
$NetworkInfo.Host = $vmhost.Name
$NetworkInfo.PNic = $Hint.Device
$NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
$NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
$record = 0
Do{
If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
$NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
$NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
}
$record ++
}
Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
foreach ($obs in $Hint.Subnet){
$NetworkInfo.Observed += $obs.IpSubnet + " "
Foreach ($VLAN in $obs.VlanId){
If ($VLAN -eq $null){
}
Else{
$strVLAN = $VLAN.ToString()
$NetworkInfo.VLAN += $strVLAN + " "
}
}
}
$MyCol += $NetworkInfo
}
}
}
$Mycol | Sort Host| ConvertTo-HTML -head $a -body $Rodada | Out-File C:\Users\Documents\WindowsPowerShell\scripts\busca-VMNIC3.html
It would you help if you could indicate what is wrong with the script.
And perhaps show what output you would like to gather in the CSV.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I Received .html with empty content.
In output: Host,PNic,Speed,MAC,DeviceID,PortID (Only Ethernet),Observed,VLAN
First thing to check, the script seems to use an input file that is a CSV with a Name column.
While your version does a Get-Content.
What is the content of the input file ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
the content is my ESXi hosts.
Yes, but what is the layout of the file.
Can you post a sample of the content ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The hostlist.txt file might be the cause of the problem.
In the script you reference $vmhost.Name, but there is no Name column in that file.
I would suggest to change the file to a CSV file with a layout like this
"Name"
"esxihost079.domain.br"
"esxihost080.domain.br"
Then use an Import-Csv cmdlet to read the file, instead of the Get-Content.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I took this script and turned it into a function. Here is my function. It will error if your interfaces that are connected do not get CDP info. You can then take your file get-content <filename> | % { get-VMHostCDPInfo -VMHost ( get-vmhost $_) }
If you are getting errors make sure you have the name exactly as it is in vcenter. I found building it as a function helped make sure I had all good data before I pumped it into my cmdlet.
You can assign that to a variable and then pipe it to a CSV. $VARYOUBUILD | Export-CSV -Path <Filepath/filename.csv> -NoTypeInformation
function Get-VMHostCDPInfo
{
[CmdletBinding(DefaultParametersetName="VMHost")]
Param(
[Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true,Position=0,ParameterSetName="VMHost")]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMHost
)
BEGIN
{
$ESXHosts = @()
}
PROCESS
{
foreach ($ESXHost in $VMHost)
{
$VSwitches = Get-VirtualSwitch -VMHost $ESXHost -Standard
$VMHostView = Get-View $ESXHost.ID
foreach ($View in $VMHostView)
{
$esxname = $View.Name
$VMHostAdapters = Get-View $View.ConfigManager.NetworkSystem
foreach ($HostAdapter in $VMHostAdapters)
{
foreach($physnic in $HostAdapter.NetworkInfo.Pnic)
{
$pnicInfo = $VMHostAdapters.QueryNetworkHint($physnic.Device)
foreach($hint in $pnicInfo)
{
if ($physnic.LinkSpeed)
{
$hint.ConnectedSwitchPort | Add-Member -MemberType NoteProperty -Name ComputerName -Value $esxname
$hint.ConnectedSwitchPort | Add-Member -MemberType NoteProperty -Name Adapter -Value $physnic.Device
}
$Hash = [ordered]@{ 'Computername'= $esxname;
'Adapter'= $physnic.Device;
'vSwitch' = ($VSwitches | where Nic -match "$($physnic.device)");
'Switch' = $hint.ConnectedSwitchPort.DevID;
'SwitchPort' = $hint.ConnectedSwitchPort.PortId;
'CDPInfo'= $hint.ConnectedSwitchPort}
New-Object -TypeName PSObject -Property $Hash
}
}
}
}
}
}
END
{
}
}