billyjoel
Enthusiast
Enthusiast

Script - can't filter where-object

Jump to solution

Hi all,

I'm working on the below script to get all the VMs where the name contains "LB" and the guest Os is like "Linux":

$Today = get-date -format dd-MMMM-yy

$outputfile = "C:\Users\user.name\Desktop\LB-report-$Today.csv"

$vcenters = Get-Content vcenters.txt

$credential = Get-Credential -Message "Enter vCenter Credentials."

foreach ($vcenter in $vcenters){

    Write-Output "connecting to vcenter" $vcenter

    Connect-VIServer $vcenter -Credential $credential

  

}

Get-VM |

    Select Name,PowerState,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N='VMHost';E={$_.VMHost.Name}},

    @{N="vCenter";E={$_.Uid.Split('@')[1].Split(':')[0]}} |

          Where-Object {($_.name  -Like "*lb*") -and ($_.ExtensionData.Config.GuestFullname -Like "*Linux*")} |

          Sort-Object Name, PowerState, Configured OS, VMHost, vCenter | Export-Csv $outputfile -NoTypeInformation

The thing is that the outut file is coming out empty, I know the issue could be in the "where-object" but not sure how to get it to work. Any idea?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Your Where-clause should come higher up in the pipeline construction.

The ExtensionData property is not available anymore after the Select-Object

Get-VM | Where-Object {($_.name  -Like "*lb*") -and ($_.ExtensionData.Config.GuestFullname -Like "*Linux*")} |

Select Name,PowerState,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N='VMHost';E={$_.VMHost.Name}},

    @{N="vCenter";E={$_.Uid.Split('@')[1].Split(':')[0]}} | 

Sort-Object Name, PowerState, Configured OS, VMHost, vCenter |

Export-Csv $outputfile -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Your Where-clause should come higher up in the pipeline construction.

The ExtensionData property is not available anymore after the Select-Object

Get-VM | Where-Object {($_.name  -Like "*lb*") -and ($_.ExtensionData.Config.GuestFullname -Like "*Linux*")} |

Select Name,PowerState,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N='VMHost';E={$_.VMHost.Name}},

    @{N="vCenter";E={$_.Uid.Split('@')[1].Split(':')[0]}} | 

Sort-Object Name, PowerState, Configured OS, VMHost, vCenter |

Export-Csv $outputfile -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
billyjoel
Enthusiast
Enthusiast

many thanks again mate, it worked Smiley Happy

0 Kudos