I am generating a list of ESXi hosts, and of all of the vmkernel ports on Each host. For each vmkernel port, I am listing the IP address.
What I want to do is select all of the hosts where the third octet of the IP address does not match the expected value.
For example,
vmk0 should always be on 192.168.10.0 / 24
but some hosts have vmk0 on 192.168.11.0 / 24 or 192.168.12.0 / 24
How can I make this report select all of the hosts which have vmk0 on a subnet where the third octect is not equal to 10?
Here is the exisxting report:
$vmhosts = get-vmhost
foreach ($vmhost in $vmhosts)
{
$vmhost.name
$esxcli = $vmhost | Get-EsxCLI
$vmkernel = $esxcli.network.ip.interface.list() | %{$_.Name}
$vmk_output = foreach ($vmk in $vmkernel) { $vmk + ":" + ($esxcli.network.ip.interface.ipv4.get($vmk) | %{$_.IPv4Address}) }
$vmk_output
}
Thanks!
Try something like this
$vmhosts = get-vmhost
foreach ($vmhost in $vmhosts){
$vmhost.name
$esxcli = $vmhost | Get-EsxCLI
$vmkernel = $esxcli.network.ip.interface.list() | %{$_.Name}
$vmk_output = foreach ($vmk in $vmkernel) {
$esxcli.network.ip.interface.ipv4.get($vmk) |
where {$_.IPv4Address.Split('.')[2] -ne "10"} |
%{$vmk + ":" + $_.IPv4Address}
}
$vmk_output
}
The Where-clause will compare the 3th number with 10, if it doesn't match, the object will pass.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The Split function works great - thanks. I modified it as below. Everything works great writing to the host. However, how can I modify this so that the output goes to a comma-delimited file?
$vmhosts = get-vmhost -location rtp_test_cluster1
$vmk0sub = '10'
foreach ($vmhost in $vmhosts)
{
$esxcli = $vmhost | Get-EsxCLI
$vmkernel = $esxcli.network.ip.interface.list() | %{$_.Name}
$vmk_output = foreach ($vmk in $vmkernel)
{
if($vmk -eq "vmk1")
{
$vmk1ip= ($esxcli.network.ip.interface.ipv4.get($vmk) | %{$_.IPv4Address})
if ($esxcli.network.ip.interface.ipv4.get($vmk) | %{$_.IPv4Address.Split('.')[2] -ne $vmk1sub})
{
write-host $vmhost $vmk $vmk1sub
}
}
}
}
Try something like this
$report = @()
foreach ($vmhost in Get-VMHost){
$esxcli = Get-EsxCLI -VMHost $vmhost
foreach ($vmk in ($esxcli.network.ip.interface.list() | Select -ExpandProperty Name)){
$esxcli.network.ip.interface.ipv4.get($vmk) |
where {$_.IPv4Address.Split('.')[2] -ne "10"} | %{
$report += New-Object PSObject -Property @{
ESX = $vmhost.Name
VMK = $vmk
IP = $_.IPv4Address
}
}
}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference