Powershell script to extract specific VM info from Hyper-V SCVMM

Welcome Forums General PowerShell Q&A Powershell script to extract specific VM info from Hyper-V SCVMM

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
2 days, 16 hours ago.

  • Author
    Posts
  • #113513

    Participant
    Points: 0
    Rank: Member

    Hi Folks,

    Looking for some guidance here, here is my script:

    $Data = @()
    
    
    $VMs = "SOMEVM"
    
    
    foreach($VM in $VMs)
    
    {
    
    $VMInfo = Get-VM -Name $VM
    
    $VMNetwork = $VMInfo | Get-VMNetworkAdapter
    
    $VMVHD = $VMInfo | Get-VMHardDiskDrive | Get-VHD
    
    $VMHOST = $VMInfo | Get-VMHost
    
    $VMWMI = Get-WmiObject Win32_OperatingSystem -ComputerName $VM
    
    $VMWMIPROD = Get-WmiObject Win32_Product -ComputerName $VM
    
    $VMWMIPRODLOGDSK = Get-WmiObject win32_logicaldisk -ComputerName $VM
    
    $VMWMIPROC = Get-WMIObject Win32_Processor -ComputerName $VM
    
    $VMWMISVRUUID = $VMInfo | Get-WmiObject Win32_ComputerSystemProduct
    
    
    $VMCustom = New-Object System.Object
    
    
    $VMCustom | Add-Member -Type NoteProperty -Name Applications1 -Value $VMWMIPROD.name
    
    $VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VMInfo.VMName
    
    $VMCustom | Add-Member -Type NoteProperty -Name IPAddresses -Value $VMNetwork.IPAddresses[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name VMid -Value $VMinfo.id
    
    $VMCustom | Add-Member -Type NoteProperty -Name ProcessorCount -Value $VMinfo.ProcessorCount
    
    $VMCustom | Add-Member -Type NoteProperty -Name MemoryAssigned -Value $VMinfo.MemoryAssigned
    
    $VMCustom | Add-Member -Type NoteProperty -Name VMHDsize -Value $VMVHD.size[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name VMUsedHDSize -Value $VMVHD.filesize[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name OSVersion -Value $VMWMI.Version
    
    $VMCustom | Add-Member -Type NoteProperty -Name Uptime -Value $VMinfo.Uptime
    
    $VMCustom | Add-Member -Type NoteProperty -Name ClusterName -Value $VMinfo.cluster
    
    $VMCustom | Add-Member -Type NoteProperty -Name Hyper-v-ComputerName -Value $VMinfo.ComputerName
    
    $VMCustom | Add-Member -Type NoteProperty -Name TotalMemoryCapacity -Value $VMHOST.MemoryCapacity
    
    $VMCustom | Add-Member -Type NoteProperty -Name DriveLetter -Value $VMWMIPRODLOGDSK.DeviceID[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name Freespace -Value $VMWMIPRODLOGDSK.FreeSpace[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name HostProvSize -Value $VMWMIPRODLOGDSK.Size[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name PoweredState -Value $VMinfo.State
    
    $VMCustom | Add-Member -Type NoteProperty -Name HOSTUUID -Value $VMWMISVRUUID.UUID
    
    $VMCustom | Add-Member -Type NoteProperty -Name LogicalProcessorCount -Value $VMHOST.LogicalProcessorCount
    
    $VMCustom | Add-Member -Type NoteProperty -Name CPULoadPercentage -Value $VMWMIPROC.LoadPercentage[0]
    
    $VMCustom | Add-Member -Type NoteProperty -Name CreationTime -Value $VMinfo.CreationTime
    
    
    $Data += $VMCustom
    
    }
    
    
    $Data | Export-CSV "C:\Users\hpvadmin\Desktop\v2.4\TEST-Output\output.csv" -Delimiter "," -NoTypeInformation
    
    

    My first issue is that when the line

    $VMCustom | Add-Member -Type NoteProperty -Name IPAddresses -Value $VMNetwork.IPAddresses[0]
    

    is run, I only get the first IP address listed for that VM. How do I get the read IP addresses to be added to the end of this string in my array?

    Any help greatly appreciated!

  • #113573

    Participant
    Points: 2
    Rank: Member

    You could start debugging your script by fixing your post (Your script is unreadyble because it's all on one line). Remove all unnecessary white space and comments and put line breaks and indenteations to make it easy to read. VSCode does it almost automaticly for you. 😉
    Then you should limit your script or function to one sinlge purpose to figure out how to get the information you want. If you have this you can integrate it to your "general approach".

    • #113623

      Participant
      Points: 0
      Rank: Member

      Hi Olaf Soyk,

      Many thanks for the feedback, I will edit my post.

  • #114402

    Participant
    Points: 0
    Rank: Member

    Maybe the code below will help

    
    [String[]]$IPAddr=($VMNetwork).IPAddress
    
    $xxx=$IPAddr.Count
    
    if ($xxx -gt 1) {
    
    While ($xxx -ge 1) {
    
    $VMCustom | Add-Member -inputObject $infoObject -memberType NoteProperty -name "IPAddr_$xxx"    -Value $IPAddr[$xxx-1]
    
    $xxx-=1;           }
    
    }
    
    else {
    
    $VMCustom | Add-Member -inputObject $infoObject -memberType NoteProperty -name "IPAddr_$xxx"    -Value $IPAddr
    
    }
    
    
  • #114436

    Participant
    Points: 10
    Rank: Member

    As for this...

    $VMNetwork.IPAddresses[0]
    

    .. you are specifically asking only for the first IPA, so not really PS, but your specific request.

    Try this, small modification to your post...

     
    $VMs = (Get-VM -Name '*').Name
    
    foreach($VM in $VMs)
    {
        $VMInfo = Get-VM -Name $VM
        $VMNetwork = $VMInfo | Get-VMNetworkAdapter
        $VMVHD = $VMInfo | Get-VMHardDiskDrive | Get-VHD
        # $VMHOST = $VMInfo.VMName | Get-VMHost -Credential $Creds 
    
        $VMWMI = Get-WmiObject Win32_OperatingSystem -ComputerName $VM
    
        $VMWMIPROD = Get-WmiObject Win32_Product -ComputerName $VM
        $VMWMIPRODLOGDSK = Get-WmiObject win32_logicaldisk -ComputerName $VM
        $VMWMIPROC = Get-WMIObject Win32_Processor -ComputerName $VM
    
        $VMWMISVRUUID = $VMInfo | Get-WmiObject Win32_ComputerSystemProduct
    
        $VMCustom = New-Object System.Object
        $VMCustom | Add-Member -Type NoteProperty -Name Applications1 -Value $VMWMIPROD.name
        $VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VMInfo.VMName
        $VMCustom | Add-Member -Type NoteProperty -Name IPAddresses -Value $VMNetwork.IPAddresses[0]
        $VMCustom | Add-Member -Type NoteProperty -Name VMid -Value $VMinfo.id
        $VMCustom | Add-Member -Type NoteProperty -Name ProcessorCount -Value $VMinfo.ProcessorCount
        $VMCustom | Add-Member -Type NoteProperty -Name MemoryAssigned -Value $VMinfo.MemoryAssigned
        $VMCustom | Add-Member -Type NoteProperty -Name VMHDsize -Value $VMVHD.size[0]
        $VMCustom | Add-Member -Type NoteProperty -Name VMUsedHDSize -Value $VMVHD.filesize[0]
        $VMCustom | Add-Member -Type NoteProperty -Name OSVersion -Value $VMWMI.Version
        $VMCustom | Add-Member -Type NoteProperty -Name Uptime -Value $VMinfo.Uptime
        $VMCustom | Add-Member -Type NoteProperty -Name ClusterName -Value $VMinfo.cluster
        $VMCustom | Add-Member -Type NoteProperty -Name Hyper-v-ComputerName -Value $VMinfo.ComputerName
        $VMCustom | Add-Member -Type NoteProperty -Name TotalMemoryCapacity -Value $VMHOST.MemoryCapacity
        $VMCustom | Add-Member -Type NoteProperty -Name DriveLetter -Value $VMWMIPRODLOGDSK.DeviceID[0]
        $VMCustom | Add-Member -Type NoteProperty -Name Freespace -Value $VMWMIPRODLOGDSK.FreeSpace[0]
        $VMCustom | Add-Member -Type NoteProperty -Name HostProvSize -Value $VMWMIPRODLOGDSK.Size[0]
        $VMCustom | Add-Member -Type NoteProperty -Name PoweredState -Value $VMinfo.State
        $VMCustom | Add-Member -Type NoteProperty -Name HOSTUUID -Value $VMWMISVRUUID.UUID
        $VMCustom | Add-Member -Type NoteProperty -Name LogicalProcessorCount -Value $VMHOST.LogicalProcessorCount
        $VMCustom | Add-Member -Type NoteProperty -Name CPULoadPercentage -Value $VMWMIPROC.LoadPercentage[0]
        $VMCustom | Add-Member -Type NoteProperty -Name CreationTime -Value $VMinfo.CreationTime
    
        $VMCustom | Export-CSV "d:\temp\output.csv" -Delimiter "," -Append -NoTypeInformation
    }
    

    Also, you have a lot of issues in how you are calling data in your collection. You are not properly formatting most of it, so, even when it hits the CSV, it's not human readable.

You must be logged in to reply to this topic.