locked
Find VMs? RRS feed

  • Question

  • Wondering how to find all your virtual servers so you can find VMs to migrate to Hyper-V? 
    tony soper
    Friday, October 10, 2008 8:33 PM

Answers

  • The Msvm_ComputerSystem Class topic on MSDN also shows the following in community content, submitted by Thomas Lee:

    Get list of VMs on a host using WMI and PowerShell
          
    # get-vm.ps1
    # Gets a list of all VMs running on this system
    # Thomas Lee - tfl@psp.co.uk

    # Get list from WMI
    $vmbase = get-wmiobject Msvm_ComputerSystem -namespace root\virtualization

    # Get hosting computer System Name

    $HostName = $vmbase | ? {$_.Caption -eq "Hosting Computer System"} | select name

    # print results
    "{0} Virtual Machines on: {1} are:" -f $($vmbase.count-1),$Hostname.name

    foreach ($vm in $vmbase) {
    if ($vm.caption -ne "Hosting Computer System") {$vm.elementname}
    }

    This script produces the following output:

     

    PS C:\Users\tfl> . 'C:\Users\tfl\AppData\Local\Temp\Untitled1.ps1'
    3 Virtual Machines on: COOKHAM8 are:
    Parent
    Rebecca
    Ubuntu-Root2

    PS C:\Users\tfl>


    tony soper
    • Marked as answer by IamMred Monday, January 11, 2010 5:29 AM
    Thursday, October 16, 2008 3:29 PM
  • Some friends here on the Hyper-V team shared a PowerShell 1.0 script for getting a list of VMs:

    # List VMs

    $vmList = gwmi -namespace root\virtualization Msvm_ComputerSystem |`
        where{$_.Name -ne $env:COMPUTERNAME}

    $vmList | select ElementName,Name,EnabledState,StatusDescriptions | format-list

    For more info on how to use PS cmdlets see: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

    See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

    For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download


    tony soper
    • Marked as answer by IamMred Monday, January 11, 2010 5:29 AM
    Friday, August 21, 2009 4:58 PM

All replies

  • '********************************************************
    '*
    '*   Script is based on a script written by Paul Williams
    '*   to find virtual servers
    '*
    '*   Modified by Richard Carpenter to create output file
    '*   and clean up resource allocation
    '*
    '********************************************************
    Option explicit

    ' constants
    CONST path = "c:\temp\"

    ' variants
    dim dse, dnc, dns, item, virsvrs

    ' output normal blurb

    'Print descriptions of what's happening

    Echo "Locating a domain controller and querying Active Directory " & _
         "Domain Services (AD DS) for Service Connection Point (SCP) objects..."

    set dse=getObject("LDAP://RootDSE")
    dnc = dse.get("defaultNamingContext")

    echo "done." & vbCrLf
    echo "Using domain: " & dnc & VbCrLf

    dns = dse.get("dNSHostName")

    echo "Using domain controller: " & lcase(dns) & vbCrLf
    echo "Finding all [registered] Virtual Server 2005 R2 SP1 instances in domain..." & vbCrLf

    virsvrs = getVirtualServerSCPs(dnc)

    echo "done." & vbCrLf

    ' we've found the servers, so display in a (tabbed) list

    print "Virtual Server 2005 R2 SP1 instances:" & vbCrLf

    for each item in virsvrs
        print lcase(item)
    next

    ' all done, output [very] basic stats

    echo vbCrLf & vbCrLf & "Script completed.  " & ubound(virsvrs) + 1 & _
         " hostnames returned." & vbCrLf & vbCrLf

    'Release all aquired objects
    set dse = nothing
     
    ' ***********************************************
    ' Function::getVirtualServerSCPs() As String
    '
    ' Function returns an array of strings containing
    ' the hostname of each object that matches the
    ' LDAP query against the default domain.
    '
    ' The query is for all serviceConnectionPoints with
    ' a name of "MS Virtual Server"
    '
    ' ***********************************************
    Private Function getVirtualServerSCPs(defaultNamingContext) ' As String()
     dim adoCommand, adoConnection, adoRecordSet
     dim ldapBase, ldapFilter, ldapAttributes, ldapScope, ldapQuery ' As String
     dim serviceBindingInformation ' As String
     dim returnVal(), i : i = 0

     ldapBase = "LDAP://" & defaultNamingContext
     ldapFilter = "(&(objectCategory=serviceConnectionPoint)(cn=MS Virtual Server))"
     ldapAttributes = "serviceBindingInformation"
     ldapScope = "subtree"
     
     ldapQuery = "<" & ldapBase & ">;" & ldapFilter & ";" & _
     ldapAttributes & ";" & ldapScope
     
     Set adoConnection = CreateObject("ADODB.Connection")
     Set adoCommand = CreateObject("ADODB.Command")
     
     adoConnection.provider = "ADsDSOObject"
     adoConnection.open "Active Directory Provider"
     
     adoCommand.activeConnection = adoConnection
     adoCommand.CommandText = ldapQuery
     
     adoCommand.properties("Page Size") = 100
     adoCommand.properties("Size Limit") = 10000
     adoCommand.properties("Cache Results") = False
     
     ' execute the command
     Set adoRecordSet = adoCommand.execute
     
     do until adoRecordSet.EOF
      serviceBindingInformation = adoRecordSet.fields("serviceBindingInformation")
      if(isArray(serviceBindingInformation) AND _
       uBound(serviceBindingInformation) = 1)then
     
       redim preserve returnArr(i)
       returnArr(i) = serviceBindingInformation(1)
       i = i + 1
      end if
      adoRecordSet.MoveNext
     loop

     ' Free resources
     set adoRecordSet = nothing
     set adoCommand = nothing
     set adoConnection = nothing

     getVirtualServerSCPs = returnArr
    End Function


    ' ***********************************************
    ' Echo(string output)
    '
    ' Sub prints the passed string to the console (or
    ' a message box, depending on the script's calling
    ' process).
    '
    ' Note.  This uses WRITE not WRITELN
    '
    ' ***********************************************
    private sub Echo(outputString)
     wscript.stdout.write outputString
    end Sub



    ' ***********************************************
    ' Print(string output)
    '
    ' Sub prints the passed string to the console (or
    ' a message box, depending on the script's calling
    ' process).
    '
    ' Note.  This uses WRITE not WRITELN
    '
    ' ***********************************************
    Private sub Print(outputString)
    dim objFS, strOutFile
    dim OutFile
    dim OutputFile

       OutputFile = path & "VS_Servers.txt"
       Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
       Set OutFile = objFS.OpenTExtFile(OutputFile , 8,true)
       OutFile.writeline OutputString
       OutFile.close
    end Sub


    tony soper
    • Marked as answer by tonysoper_MSFT Friday, October 10, 2008 8:33 PM
    • Unmarked as answer by tonysoper_MSFT Friday, November 14, 2008 7:25 PM
    Friday, October 10, 2008 8:33 PM
  • The Msvm_ComputerSystem Class topic on MSDN also shows the following in community content, submitted by Thomas Lee:

    Get list of VMs on a host using WMI and PowerShell
          
    # get-vm.ps1
    # Gets a list of all VMs running on this system
    # Thomas Lee - tfl@psp.co.uk

    # Get list from WMI
    $vmbase = get-wmiobject Msvm_ComputerSystem -namespace root\virtualization

    # Get hosting computer System Name

    $HostName = $vmbase | ? {$_.Caption -eq "Hosting Computer System"} | select name

    # print results
    "{0} Virtual Machines on: {1} are:" -f $($vmbase.count-1),$Hostname.name

    foreach ($vm in $vmbase) {
    if ($vm.caption -ne "Hosting Computer System") {$vm.elementname}
    }

    This script produces the following output:

     

    PS C:\Users\tfl> . 'C:\Users\tfl\AppData\Local\Temp\Untitled1.ps1'
    3 Virtual Machines on: COOKHAM8 are:
    Parent
    Rebecca
    Ubuntu-Root2

    PS C:\Users\tfl>


    tony soper
    • Marked as answer by IamMred Monday, January 11, 2010 5:29 AM
    Thursday, October 16, 2008 3:29 PM
  • Some friends here on the Hyper-V team shared a PowerShell 1.0 script for getting a list of VMs:

    # List VMs

    $vmList = gwmi -namespace root\virtualization Msvm_ComputerSystem |`
        where{$_.Name -ne $env:COMPUTERNAME}

    $vmList | select ElementName,Name,EnabledState,StatusDescriptions | format-list

    For more info on how to use PS cmdlets see: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

    See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

    For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download


    tony soper
    • Marked as answer by IamMred Monday, January 11, 2010 5:29 AM
    Friday, August 21, 2009 4:58 PM