PowerShell Script to Query VMM

Query List of VMs

The following PowerShell script will query VMM for a list of VMs. Use this as a start.

Import-Module virtualmachinemanager;
Get-VM -VMMServer vmm.servername01 | Format-List -property Name, Owner, Description, HostName, OperatingSystem, CPUCount, Memory

Query List of VMs and Export as CSV

Filter the VM name for all APP servers and export the list to csv:

Import-Module virtualmachinemanager;
$MasterList = @();
$serverList = Get-VM -VMMServer vmm.servername01 | Where-Object { (($_.Name -like '*APP*' -or $_.Name -like '*WEB*' -or $_.Name -like '*CORP*' -or $_.Name -like '*UTIL*') -and $_.Name -notlike '*DEV*') };
foreach ($server in $serverList) {
   $serverNetworkAdapter = Get-SCVirtualNetworkAdapter -VM $server;
   $MyObject = New-Object PSObject -Property @{
      ServerName = $server.Name;
      ServerEnvironment = $server.Name.Substring(3,4);
      HostName = $server.HostName;
      VMStatusString = [string]$server.StatusString;
      VMState = [string]$server.VirtualMachineState;
      ServerIPAddress = $serverNetworkAdapter.IPv4Addresses;
      CPUCount = $server.CPUCount;
      Memory = $server.Memory;
      OperatingSystem = $server.OperatingSystem;
      IsHighlyAvailable = $server.IsHighlyAvailable;
      Description = $server.Description.Replace([Environment]::NewLine, '');
      CreationTime = $server.CreationTime;
   };
   $MasterList += $MyObject;
};
$MasterList | Select-Object @{label='Server Name';expression={$_.ServerName.ToUpper()}}, @{label='Environment';expression={$_.ServerEnvironment.ToUpper()}}, @{label='VM Status';expression={if ($_.VMStatusString -eq $_.VMState) { $_.VMStatusString } else { $_.VMState + ' (' + $_.VMStatusString + ')'}}}, @{label='Host Name';expression={$_.HostName.Replace('.company.com','').ToUpper()}},@{Name=’ServerIPAddress’;Expression={if ([string]::join(";", ($_.ServerIPAddress))) {[string]::join(";", ($_.ServerIPAddress))} else { " " }}},@{label='Cores';expression={$_.CPUCount}},@{label='Memory';expression={$_.Memory}},@{label='OS';expression={$_.OperatingSystem}},@{label='Highly Available';expression={$_.IsHighlyAvailable}},@{label='Comments';expression={if ($_.Description) {$_.Description + ', Created ' + $_.CreationTime} else { 'Created ' + $_.CreationTime }}} | EXPORT-CSV c:\temp\server-list.csv -notype;

Leave a Reply