{"id":1292,"date":"2018-03-12T08:33:32","date_gmt":"2018-03-12T16:33:32","guid":{"rendered":"http:\/\/www.developerscloset.com\/?p=1292"},"modified":"2018-06-12T09:26:13","modified_gmt":"2018-06-12T17:26:13","slug":"powershell-script-to-query-vmm","status":"publish","type":"post","link":"https:\/\/www.developerscloset.com\/?p=1292","title":{"rendered":"PowerShell Script to Query VMM"},"content":{"rendered":"<div class=\"\">\n<div id=\"content\" class=\"page view\">\n<div id=\"main-content\" class=\"wiki-content\">\n<h1 id=\"PowerShellScripttoQueryVMM-QueryListofVMs\" class=\"auto-cursor-target\">Query List of VMs<\/h1>\n<p class=\"auto-cursor-target\">The following PowerShell script will query VMM for a list of VMs. Use this as a start.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nImport-Module&amp;nbsp;virtualmachinemanager;\r\nGet-VM -VMMServer vmm.servername01 | Format-List -property Name, Owner, Description, HostName, OperatingSystem, CPUCount, Memory\r\n<\/pre>\n<h1 id=\"PowerShellScripttoQueryVMM-QueryListofVMsandExportasCSV\">Query List of VMs and Export as CSV<\/h1>\n<p>Filter the VM name for all APP servers and export the list to csv:<\/p>\n<div class=\"code panel pdl conf-macro output-block\">\n<div class=\"codeContent panelContent pdl\">\n<div id=\"highlighter_310202\" class=\"syntaxhighlighter sh-confluence nogutter powershell\">\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nImport-Module\u00a0virtualmachinemanager;\r\n$MasterList\u00a0= @();\r\n$serverList\u00a0= Get-VM\u00a0-VMMServer vmm.servername01\u00a0|\u00a0Where-Object\u00a0{ (($_.Name\u00a0-like\u00a0'*APP*'\u00a0-or\u00a0$_.Name\u00a0-like\u00a0'*WEB*'\u00a0-or\u00a0$_.Name\u00a0-like\u00a0'*CORP*'\u00a0-or\u00a0$_.Name\u00a0-like\u00a0'*UTIL*')\u00a0-and\u00a0$_.Name\u00a0-notlike\u00a0'*DEV*') };\r\nforeach\u00a0($server\u00a0in\u00a0$serverList) {\r\n\u00a0\u00a0\u00a0$serverNetworkAdapter\u00a0= Get-SCVirtualNetworkAdapter\u00a0-VM\u00a0$server;\r\n\u00a0\u00a0\u00a0$MyObject\u00a0=\u00a0New-Object\u00a0PSObject\u00a0-Property\u00a0@{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ServerName =\u00a0$server.Name;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ServerEnvironment =\u00a0$server.Name.Substring(3,4);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0HostName =\u00a0$server.HostName;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0VMStatusString = &#x5B;string]$server.StatusString;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0VMState = &#x5B;string]$server.VirtualMachineState;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ServerIPAddress =\u00a0$serverNetworkAdapter.IPv4Addresses;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CPUCount =\u00a0$server.CPUCount;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Memory =\u00a0$server.Memory;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0OperatingSystem =\u00a0$server.OperatingSystem;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0IsHighlyAvailable =\u00a0$server.IsHighlyAvailable;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Description =\u00a0$server.Description.Replace(&#x5B;Environment]::NewLine,\u00a0'');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CreationTime =\u00a0$server.CreationTime;\r\n\u00a0\u00a0\u00a0};\r\n\u00a0\u00a0\u00a0$MasterList\u00a0+=\u00a0$MyObject;\r\n};\r\n$MasterList\u00a0|\u00a0Select-Object\u00a0@{label='Server Name';expression={$_.ServerName.ToUpper()}}, @{label='Environment';expression={$_.ServerEnvironment.ToUpper()}}, @{label='VM Status';expression={if ($_.VMStatusString\u00a0-eq\u00a0$_.VMState) { $_.VMStatusString } else { $_.VMState +\u00a0' ('\u00a0+ $_.VMStatusString +\u00a0')'}}}, @{label='Host Name';expression={$_.HostName.Replace('.company.com','').ToUpper()}},@{Name=\u2019ServerIPAddress\u2019;Expression={if (&#x5B;string]::join(&quot;;&quot;, ($_.ServerIPAddress))) {&#x5B;string]::join(&quot;;&quot;, ($_.ServerIPAddress))} else {\u00a0&quot; &quot;\u00a0}}},@{label='Cores';expression={$_.CPUCount}},@{label='Memory';expression={$_.Memory}},@{label='OS';expression={$_.OperatingSystem}},@{label='Highly Available';expression={$_.IsHighlyAvailable}},@{label='Comments';expression={if ($_.Description) {$_.Description +\u00a0', Created '\u00a0+ $_.CreationTime} else {\u00a0'Created '\u00a0+ $_.CreationTime }}} |\u00a0EXPORT-CSV\u00a0c:\\temp\\server-list.csv\u00a0-notype;\r\n<\/pre>\n<\/div>\n<div class=\"\">\n<div id=\"content\" class=\"page view\">\n<div><\/div>\n<\/div>\n<\/div>\n<div id=\"likes-and-labels-container\" class=\"ViewPage_likesAndLabelsContainer_nyS\">\n<div>\n<div>\n<div class=\"LikeButton_LikeButton_1Ym\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Query List of VMs The following PowerShell script will query VMM for a list of VMs. Use this as a start. Import-Module&amp;nbsp;virtualmachinemanager; Get-VM -VMMServer vmm.servername01 [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1292","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1BQ8S-kQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/posts\/1292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1292"}],"version-history":[{"count":3,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/posts\/1292\/revisions"}],"predecessor-version":[{"id":1378,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=\/wp\/v2\/posts\/1292\/revisions\/1378"}],"wp:attachment":[{"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.developerscloset.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}