VMware Cloud Community
monderick
Enthusiast
Enthusiast
Jump to solution

vm query script help

I've learned enough around here to compose the below scriptlet that will find VMs on specific datastores and attached to specific port groups but have been failing for hours trying to add another variable without exiting the loop and re-scanning all VM's again.

I need the get-VM command to also include get-annotation | ?{$_.Value -eq "backedup"} and also return on the results so the output looks like:

Parent              Datastore          Annotation    

VMname          Datastore1         backedup

get-cluster | ?{$_.Name -notlike "*lab*"} | get-vmhost | Get-Datastore | ?{$_.Name -notlike "*ISO*" -and $_.Name -notlike "*templates*"} | Foreach-Object {

    $ds = $_.Name

        $_ | Get-VM | Get-NetworkAdapter | ?{$_.NetworkName -like "Backup Network"} | Select-Object Parent,@{n='DataStore';e={$ds}}

}

any assistance would be appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($ds in (Get-Cluster | 
               
where($_.Name -notlike "*lab*"} |
               
Get-VMHost | Get-Datastore |
               
where {$_.Name -notlike "*ISO*" -and $_.Name -notlike "*templates*"})){
 
Get-VM -Datastore $ds |
 
where {(Get-NetworkAdapter -VM $_ | Select -ExpandProperty NetworkName) -ne "Backup Network"}
 
Select Name,
   
@{N="Datastore";E={$ds.Name}},
   
@{N="BackedUp";E={Get-Annotation -Entity $_ -Name "NameOfTheAttribute" | Select -ExpandProperty Value}}
}

You have to use the proper custom attribute name in place of the "NameOfTheAttribute" I used


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($ds in (Get-Cluster | 
               
where($_.Name -notlike "*lab*"} |
               
Get-VMHost | Get-Datastore |
               
where {$_.Name -notlike "*ISO*" -and $_.Name -notlike "*templates*"})){
 
Get-VM -Datastore $ds |
 
where {(Get-NetworkAdapter -VM $_ | Select -ExpandProperty NetworkName) -ne "Backup Network"}
 
Select Name,
   
@{N="Datastore";E={$ds.Name}},
   
@{N="BackedUp";E={Get-Annotation -Entity $_ -Name "NameOfTheAttribute" | Select -ExpandProperty Value}}
}

You have to use the proper custom attribute name in place of the "NameOfTheAttribute" I used


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

thanks much Luc, it appears that i need to specify the VM variable after Get-NetworkAdapter -VM as an error is thrown but unfortunately it doesn't accept anything i try

Get-NetworkAdapter : Missing an argument for parameter 'VM'. Specify a parameter of type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]' and try again.

At C:\VMware\dss.ps1:7 char:30

+   where {(Get-NetworkAdapter -VM | Select -ExpandProperty NetworkName) -ne "Back ...

+                              ~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-NetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : MissingArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter

0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

I take that back, there was a minor typo in line 2 and i figured out the missing variable.

Thanks again Luc, you're an amazing resource between here and your book.


foreach ($ds in (Get-Cluster |
               
where {$_.Name -notlike "*lab*"} |
               
Get-VMHost | Get-Datastore |
               
where {$_.Name -notlike "*ISO*" -and $_.Name -notlike "*templates*"})){
 
Get-VM -Datastore $ds |
 
where {(Get-NetworkAdapter -VM $_.Name | Select -ExpandProperty NetworkName) -ne "Backup Network"}
 
Select Name,
   
@{N="Datastore";E={$ds.Name}},
   
@{N="BackedUp";E={Get-Annotation -Entity $_ -Name "NameOfTheAttribute" | Select -ExpandProperty Value}}
}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yup, you're right.

I updated my copy as well, in fact using $_ instead of $_.Name should be a bit faster. It saves 1 Get-VM per call.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos