Reply to Message

View discussion in a popup

Replying to:
LucD
Leadership
Leadership

I'm afraid there are a couple of flaws in that script.

  • The -like operator will only return true if the list contains an exact match, i.e. FQDN vs FQDN or hostname vs hostname
  • There is an unnecessary number of calls to Get-VM

$ServerList = Get-Content C:\scripts\servers.txt

$Report = @()

$vms = Get-VM

foreach ($Server in $ServerList){

    $vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{

        $report += New-Object PSObject -Property @{

           VM_Name = $_.Name

           DNS_Name = $_.ExtensionData.Guest.Hostname

        }

    }

}

$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation


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

View solution in original post