Hello, I need to optimize this vCheck script for my daily report, I already modified it successfully for a couple of easy details. However I'm stuck at something else. As you can see below I´m filtering out certain hosts in test and some vmnics never used. However I also need to filter out only certain vmnics of certain ESXi hosts. Because those nics only on those hosts will never be used. That will give me even a clearer vcheck output but I´m not able to get that, if anyone has a clue pls
$vsList = Get-VirtualSwitch
foreach ($VMHost in ($VMH | Where-Object {$_.Name -notlike "esx3" -and $_.Name -notlike "esx1" -and $_.Name -notlike '10.11.30.41'} )) {
foreach($pnic in ($VMHost.ExtensionData.Config.Network.Pnic) | Where-Object{$_.device -notcontains "vmnic6"} | Where-Object{$_.device -notcontains "vmnic7"}){
$vSw = $vsList | Where-Object {($_.VMHost -eq $VMHost) -and ($_.Nic -contains $pNic.Device)}
$pnic | Select-Object @{N="ESXname";E={$VMHost.Name}},@{N="pNic";E={$pnic.Device}},@{N="vSwitch";E={$vSw.Name}},@{N="Status";E={if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}} | Where-Object {$_.Status -eq "down"}
}
}
Although the https://communities.vmware.com/blogs/vtipsntricks/2013/11/26/workaround-to-disable-one-of-the-unused... didn´t work from command line It did work however by adding the NIC as PCI device in the web client. That tricks the ESXi to see the NIC as used. I´m done then
Since you are not using mete-characters in the -lie expression, you are executing an exact match.
What are the names actually like? Can you do a
$VMH | select Name
If you want to match on the start of ESXi name, you could use -notmatch RegEX expression, and combine the comparison targets with a RegEx 'or' (|).
You also seem to have reversed the operands on the -notcontains operator.
Try like this, and see if that makes a difference
$vsList = Get-VirtualSwitch
foreach ($VMHost in ($VMH | Where-Object {$_.Name -notmatch "^esx3|^esx1|^10.11.30.41"} )) {
foreach($pnic in ($VMHost.ExtensionData.Config.Network.Pnic | Where-Object{"vmnic6","vmnic7" -notcontains $_.device})){
$vSw = $vsList | Where-Object {($_.VMHost -eq $VMHost) -and ($_.Nic -contains $pNic.Device)}
$pnic | Select-Object @{N="ESXname";E={$VMHost.Name}},
@{N="pNic";E={$pnic.Device}},
@{N="vSwitch";E={$vSw.Name}},
@{N="Status";E={if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}} | Where-Object {$_.Status -eq "down"}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many thanks indeed Luc! That definitely shortens the code... acknowledged. The names are like this:
esx1-org-site.domainname
basically the FQDN. I didn't know about the way you use the operands, my code works anyway. I thought the way is:
where{$_.action -operand "value"}
However the goal I'm struggling with is a different one and it's not in the code yet as all I tried it failed. So far it was easy as I'm filtering out entire ESX objects and all vmnics 6 and 7 regardless of any ESX objects as they are never used anywhere. What I also need to achieve is to filter out the vmnics down(unused) of certain hosts, while keep monitoring the alive vmnics of the same hosts. So if the alive vmnics go down I'm told by the script in the report. I'll explain:
1. Most ESXi hosts (blades) have vmnic0 and vmnic1
2. unfortunately some new hosts are configured by the hardware team in a different way, also they are different hardware and so they have vmnic0 up, vmnic1 down, vmnic2 up, vmnic3 down.
I need NOT to see in the report those vmnics down while the script monitors the ones that are up. For the blades hosts I could configure the virtual connect to hide the unused NICs but these new hosts are DL and don't have that option in the iLO page.
You could include a test on the HW type of the ESXi node, and then based on that adapt your esxi node and vmnic filtering.
In such complex situations I always try to write out the tests in a kind of decision table.
And then in the code combine the branches with -and and -or operators.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks I'll try and revert, the thing is that in
foreach($pnic in ($VMHost.ExtensionData.Config.Network.Pnic
I have no way to operate on the ESX name as it is not available. I'll try with
foreach ($VMHost in ($VMH
to get to work on both name and device name
You could use the Pipelinevariable and use a pipeline construction, something like this
Get-VMHost -PipelineVariable esx | where{$_.Name -notmatch "^esx3|^esx1|^10.11.30.41"} |
Get-VMHostNetworkAdapter -VMKernel -PipelineVariable vnic |
where{"vmnic6","vmnic7" -notcontains $_.device} |
Select @{N='EsxName';E={$esx.Name}},
@{N='pNic';E={$_.DeviceName}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again Luc, but the
Get-VMHost -PipelineVariable esx | where{$_.Name -notmatch "^esx3|^esx1|^10.11.30.41"}
already filters out the ESXi host totally and do not get passed to the pipeline so that unfiltered vmnics are captured eventually. That is the problem I'm having since beginning. I'll see to work it out on
foreach($VMHost in ($VMH |
Do you have the filtering rules available in a structured notation?
I mean, do you have the order of tests to perform?
Something like
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
let's see if I got it...
so the vmnics above should never be filtered out.
so the vmnics I can filter out for all ESXi hosts are:
vmnic3,6 and 7. Done. It works with this:
$vsList = Get-VirtualSwitch
foreach ($VMHost in ($VMH | Where-Object {$_.Name -notmatch "^esx3|^esx1"} )) {
foreach($pnic in $VMHost.ExtensionData.Config.Network.Pnic | where{"vmnic3","vmnic6","vmnic7" -notcontains $_.device}){
$vSw = $vsList | Where-Object {($_.VMHost -eq $VMHost) -and ($_.Nic -contains $pNic.Device)}
$pnic | Select-Object @{N="ESXname";E={$VMHost.Name}},@{N="pNic";E={$pnic.Device}},@{N="vSwitch";E={$vSw.Name}},@{N="Status";E={if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}} | Where-Object {$_.Status -eq "down"}
}
}
now I have left to filter vmnic1,4,5 for only certain hosts.
One more question, how, and on what, do you test for Type A, B or C?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
this script is plugin number 74 in the Host folder of the vCheck. When I do my tests I add to the script:
$VMH = get-cluster "vClusterX" | get-vmhost
This way I isolate the script from the vCheck context and I can concentrate my tests only on the vCluster where those ESXi hosts are placed. Unfortunately those hosts (HP DL) share resources in the same vClusterX with the HP blades hosts (EVC enabled). So I cannot create adhoc codes only for the vCluster at hand. I hope I addressed your question correctly. I have the impression that I better work on the output of the script and remove the unwanted info.
Your choice, but without the complete hierarchy of the tests, it will be difficult to write a working filtering mechanism.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
the thing is that I don't understand what is meant by "tests", surely I have gaps. If I'll come up with something I'll revert, in the meantime thanks for your time
I had time to elaborate this a little further. I also checked this:
Workaround to disable one of the unused NIC or FCOE ports in ESXi 3.x, 4.x and 5.x using vmkchdev but it does not work in my case.
So I noticed that vmnic4 and 5 have the same driver Intel and are not used anywhere so I filtered out by driver too, so:
$vsList = Get-VirtualSwitch
foreach ($VMHost in ($VMH | Where-Object {$_.Name -notmatch "^esx3|^esx1"} )) {
foreach($pnic in $VMHost.ExtensionData.Config.Network.Pnic | where{"vmnic3" -notcontains $_.device}){
$vSw = $vsList | Where-Object {($_.VMHost -eq $VMHost) -and ($_.Nic -contains $pNic.Device)}
$pnic | Select-Object @{N="ESXname";E={$VMHost.Name}},@{N="NIC Driver";E={$pNic.Driver}},@{N="pNic";E={$pnic.Device}},@{N="vSwitch";E={$vSw.Name}},@{N="Status";E={if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}} | Where-Object {$_.Status -eq "down"} | where {$pnic.Driver -ne "ixgbe"} | select ESXname,pNic,vSwitch,Status
}
}
I had to re-select at the end as by adding the Driver it changed the output format. So now I have left vmnic1 only for certain hosts.
Although the https://communities.vmware.com/blogs/vtipsntricks/2013/11/26/workaround-to-disable-one-of-the-unused... didn´t work from command line It did work however by adding the NIC as PCI device in the web client. That tricks the ESXi to see the NIC as used. I´m done then