Announcement

Collapse
No announcement yet.

VM controls through a script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VM controls through a script

    Here is what I ve stumbled upon and it comes from Randy Williams. What I only added was the "stop" procedure , as he wanted to only "start" and "save"

    Param($operation, $servername)

    if ($operation -eq 'start')
    {
    $stateId = 2
    }
    elseif ($operation -eq 'save')
    {
    $stateId = (32769)
    }
    elseif ($operation -eq 'stop')
    {
    $stateId = 3
    }

    else
    { "Unknown operation"
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    throw
    }


    #Get a handle to the VM object
    $Core = get-wmiobject -namespace root\virtualization -class Msvm_Computersystem -filter "ElementName = '$servername'"

    #Set the state
    $status = $Core.RequestStateChange($stateId)

    #Are we already in this state?
    if ($status.ReturnValue -eq 32775)
    {
    "Already in this state"
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }

    #Wait for use to press key if it did not work successfully
    #0 = Success; 4096 = Job Running
    elseif (($status.ReturnValue -ne 0) -and ($status.ReturnValue -ne 4096))
    {
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    To enable your WS2008 to run non-signed PS scripts locally just type within PS shell
    Set-ExecutionPolicy RemoteSigned
    An excellent script to control your VMs, I believe.
    Last edited by azangr; 24th January 2010, 23:32.

  • #2
    Re: VM controls through a script

    Nice find azangr. Thanks.
    Cheers,

    Daniel Petri
    Microsoft Most Valuable Professional - Active Directory Directory Services
    MCSA/E, MCTS, MCITP, MCT

    Comment

    Working...
    X