Author Topic: Use Windows PowerShell to Manage Virtual Machines  (Read 1881 times)

0 Members and 1 Guest are viewing this topic.

Use Windows PowerShell to Manage Virtual Machines
« on: July 02, 2010, 05:19:25 PM »

Offline Nick

  • Administrator
  • Platinum Member
  • *
  • Posts: 46027
  • Karma: +1000/-0
  • Gender: Male
  • NickCS
    • http://www.facebook.com/nickcomputerservices
    • http://www.twitter.com/nickcomputer
    • Computer Chiangmai

Use Windows PowerShell to Manage Virtual Machines

Here are a few examples of how you can use Windows PowerShell scripts to manage virtual machines running on a Server Core installation. Note that these scripts are presented as samples and may need to be customized to work in your environment.

You can use Windows PowerShell to manage virtual machines running on Hyper-V servers running Server Core, but you must run your scripts remotely from a computer that has Windows PowerShell installed on it. In addition, your Windows PowerShell scripts can be used only to access the WMI interface on the targeted Server Core installation. This means the primary Windows PowerShell cmdlet you will use to manage virtual machines running on Server Core is the Get-WmiObject cmdlet, which also has the associated gwmi alias in Windows PowerShell.

Here are a few examples of how you can use Windows PowerShell scripts to manage virtual machines running on a Server Core installation. Note that these scripts are presented as samples and may need to be customized to work in your environment.


Displays the State of All Virtual Machines

Code: [Select]
$VMState=@{2="Running" ; 3="Stopped" ; 32768="Paused" ; 32769="Suspended";
32270="Starting" ; 32771="Snapshotting" ; 32773="Saving" ; 32774="Stopping" }
get-wmiobject -computername localhost -Namespace root\Virtualization
-query "Select * from MSVM_Computersystem where Description like
'%Virtual%' " | format-table -autosize @{Label=”VM Name”;
expression={$_.elementName}}, Description, @{Label =”VM State”;
expression={$VmState[$_.EnabledState]}}


Creates a Snapshot of All Virtual Machines

Code: [Select]
$VSMgtSvc=Get-WmiObject -ComputerName localhost
-NameSpace "root\virtualization"
-Class "MsVM_virtualSystemManagementService"
get-wmiobject -computername localhost -Namespace root\Virtualization
-query "Select * from MSVM_Computersystem where Description like
'%Virtual%' " | foreach-object {$VSMgtSvc.psbase.invokeMethod
("CreateVirtualSystemSnapshot",@($_,$Null,$null)) }


Saves the State of All Running Virtual Machines

Code: [Select]
$VSMgtSvc=Get-WmiObject -ComputerName localhost -NameSpace "root\
virtualization" -Class "MsVM_virtualSystemManagementService"
Get-WmiObject -computername Localhost -NameSpace "root\virtualization"
-Query "Select * From MsVM_ComputerSystem Where Caption Like 'Virtual%'
and EnabledState = 2" | foreach-Object {$_.RequestStateChange(32769) }


From the Microsoft Press book Windows Server 2008 Server Core Administrator’s Pocket Consultant by Mitch Tulloch.

credit: technet.microsoft.com


 
Share this topic...
In a forum
(BBCode)
In a site/blog
(HTML)


Related Topics

  Subject / Started by Replies Last post
0 Replies
2951 Views
Last post June 11, 2010, 06:56:51 PM
by Nick
0 Replies
3014 Views
Last post June 11, 2010, 07:03:17 PM
by Nick
0 Replies
2911 Views
Last post June 11, 2010, 07:10:30 PM
by Nick
0 Replies
3541 Views
Last post June 16, 2010, 02:10:11 PM
by Nick
0 Replies
2846 Views
Last post June 16, 2010, 07:14:52 PM
by Nick
0 Replies
3320 Views
Last post June 16, 2010, 09:24:15 PM
by Nick
0 Replies
3444 Views
Last post July 03, 2010, 03:11:45 PM
by Nick
0 Replies
2692 Views
Last post July 03, 2010, 03:14:50 PM
by Nick
0 Replies
3894 Views
Last post July 03, 2010, 03:15:40 PM
by Nick
0 Replies
503 Views
Last post November 10, 2016, 12:12:16 PM
by Nick