I need help to find a way to check if port is connected to a VM, if not connected then delete port. for deletion part i am using below script. but need assistance to check if its connect to any vm, if not connected delete
$esxinames = Get-Content -Path c:\vmhostfile.txt
ForEach ($servers in $esxinames)
{
Get-VMHost $servers | Get-VirtualPortGroup -Name "portname" | Remove-VirtualPortGroup -Confirm:$false
}
also can the script be -RunAsync, for some reason it was not accepting -RunAsync at end of script.
Something like this?
$esxinames = Get-Content -Path c:\vmhostfile.txt
ForEach ($servers in $esxinames) {
$pg = Get-VMHost $servers | Get-VirtualPortGroup -Name "portname"
if($pg.ExtensionData.Port.Count -eq 0){
Remove-VirtualPortGroup -VirtualPortGroup $pg -Confirm:$false
}
else{
Write-Host "Portgroup $($pg.Name) is in use"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do
PS: the Remove-VirtualPortgroup cmdlet doesn't have the RunAsync switch
$esxinames = Get-Content -Path c:\vmhostfile.txt
ForEach ($servers in $esxinames) {
Get-VMHost $servers | Get-VirtualPortGroup -Name "portname" |
where { $_.ExtensionData.Port.Count -eq 0 } |
Remove-VirtualPortGroup -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
can we use If and else loop,
just to output in "else" that "vmname" is used and cannot be deleted.
Sorry hope am not asking much
"vmname", I suspect you mean portgroup?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Something like this?
$esxinames = Get-Content -Path c:\vmhostfile.txt
ForEach ($servers in $esxinames) {
$pg = Get-VMHost $servers | Get-VirtualPortGroup -Name "portname"
if($pg.ExtensionData.Port.Count -eq 0){
Remove-VirtualPortGroup -VirtualPortGroup $pg -Confirm:$false
}
else{
Write-Host "Portgroup $($pg.Name) is in use"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thank you, it works as needed.