Metode RequestStateChange dari kelas Msvm_ComputerSystem
Meminta agar status komputer virtual diubah ke nilai yang ditentukan. Memanggil metode RequestStateChange beberapa kali dapat mengakibatkan permintaan sebelumnya ditimpa atau hilang. Metode ini hanya didukung untuk instans kelas Msvm_ComputerSystem yang mewakili komputer virtual.
Saat perubahan status sedang berlangsung, properti RequestedState diubah ke nilai parameter RequestedState .
Sintaks
uint32 RequestStateChange(
[in] uint16 RequestedState,
[out] CIM_ConcreteJob REF Job,
[in] datetime TimeoutPeriod
);
Parameter
-
RequestedState [in]
-
Jenis: uint16
Status baru. Nilai yang lebih besar dari 32767 adalah nilai yang diusulkan DMTF dan dapat berubah.
Berikut adalah nilai yang mungkin:
-
Lainnya (1)
-
Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Lainnya.
-
Diaktifkan (2)
-
Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Diaktifkan.
-
Dinonaktifkan (3)
-
Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Disabled.
-
Matikan (4)
-
Hanya berlaku di Hyper-V versi 1 (V1). Komputer virtual dimatikan melalui layanan matikan. Sesuai dengan CIM_EnabledLogicalElement.EnabledState = ShuttingDown.
-
Offline (6)
-
Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Diaktifkan tetapi offline.
-
Uji (7)
-
Tangguhkan (8)
-
Quiesce (9)
-
Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Quiesce, Diaktifkan tetapi dijeda.
-
Reboot (10)
-
Transisi status dari Nonaktif atau Disimpan ke Berjalan.
-
Reset (11)
-
Reset komputer virtual. Sesuai dengan CIM_EnabledLogicalElement.EnabledState = Reset.
-
Penghematan (32773)
-
Dalam versi 1 (V1) Hyper-V, sesuai dengan EnabledStateSaving.
-
Menjeda (32776)
-
Di Hyper-V versi 1 (V1), sesuai dengan EnabledStatePausing.
-
Melanjutkan (32777)
-
Di Hyper-V versi 1 (V1), sesuai dengan EnabledStateResuming. Transisi status dari Dijeda ke Berjalan.
-
FastSaved (32779)
-
Sesuai dengan EnabledStateFastSuspend.
-
FastSaving (32780)
-
Sesuai dengan EnabledStateFastSuspending. Transisi status dari Berjalan ke FastSaved.
Nilai-nilai ini mewakili status kritis:
RunningCritical (32781)
OffCritical (32782)
StoppingCritical (32783)
SavedCritical (32784)
PausedCritical (32785)
StartingCritical (32786)
ResetCritical (32787)
SavingCritical (32788)
JedaKritik (32789)
MelanjutkanCritical (32790)
FastSavedCritical (32791)
FastSavingCritical (32792)
Job [out]
Jenis: CIM_ConcreteJob
Referensi opsional ke objek Msvm_ConcreteJob yang dikembalikan jika operasi dijalankan secara asinkron. Jika ada, referensi yang dikembalikan dapat digunakan untuk memantau kemajuan dan mendapatkan hasil metode .
TimeoutPeriod [in]
Jenis: tanggalwaktu
Parameter ini tidak digunakan.
Nilai kembali
Jenis: uint32
Metode ini mengembalikan salah satu nilai berikut.
Mengembalikan kode/nilai | Deskripsi |
---|---|
|
Sukses. |
|
Transisinya asinkron. |
|
Akses ditolak. |
|
|
|
|
|
|
|
|
|
|
|
|
|
Nilai yang ditentukan dalam parameter RequestedState tidak didukung. |
|
|
|
|
|
Keterangan
Akses ke kelas Msvm_ComputerSystem mungkin dibatasi oleh Pemfilteran UAC. Untuk informasi selengkapnya, lihat Kontrol Akun Pengguna dan WMI.
Contoh
Contoh C# berikut memulai atau menonaktifkan komputer virtual. Utilitas yang dirujuk dapat ditemukan di Utilitas umum untuk sampel virtualisasi (V2).
Penting
Agar berfungsi dengan benar, kode berikut harus dijalankan di server host komputer virtual, dan harus dijalankan dengan hak istimewa administrator.
using System;
using System.Management;
namespace HyperVSamples
{
public class RequestStateChangeClass
{
public static void RequestStateChange(string vmName, string action)
{
ManagementScope scope = new ManagementScope(@"\\.\root\virtualization\v2", null);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
if (null == vm)
{
throw new ArgumentException(
string.Format(
"The virtual machine '{0}' could not be found.",
vmName));
}
ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange");
const int Enabled = 2;
const int Disabled = 3;
if (action.ToLower() == "start")
{
inParams["RequestedState"] = Enabled;
}
else if (action.ToLower() == "stop")
{
inParams["RequestedState"] = Disabled;
}
else
{
throw new Exception("Wrong action is specified");
}
ManagementBaseObject outParams = vm.InvokeMethod(
"RequestStateChange",
inParams,
null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine(
"{0} state was changed successfully.",
vmName);
}
else
{
Console.WriteLine("Failed to change virtual system state");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine(
"{0} state was changed successfully.",
vmName);
}
else
{
Console.WriteLine(
"Change virtual system state failed with error {0}",
outParams["ReturnValue"]);
}
}
public static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: <application> vmName action");
Console.WriteLine("action: start|stop");
return;
}
RequestStateChange(args[0], args[1]);
}
}
}
Contoh Visual Basic Scripting Edition (VBScript) berikut memulai atau menonaktifkan komputer virtual.
Penting
Agar berfungsi dengan benar, kode berikut harus dijalankan di server host komputer virtual, dan harus dijalankan dengan hak istimewa administrator.
dim objWMIService
dim fileSystem
const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096
const Enabled = 2
const Disabled = 3
Main()
'-----------------------------------------------------------------
' Main routine
'-----------------------------------------------------------------
Sub Main()
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
strComputer = "."
set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization\v2")
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 2 then
vmName= objArgs.Unnamed.Item(0)
action = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript StartVM.vbs vmName start|stop"
WScript.Quit
end if
set computerSystem = GetComputerSystem(vmName)
if RequestStateChange(computerSystem, action) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "RequestStateChange failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
On Error Resume Next
query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Turn on a virtual machine
'-----------------------------------------------------------------
Function RequestStateChange(computerSystem, action)
WriteLog Format2("RequestStateChange({0}, {1})", computerSystem.ElementName, action)
RequestStateChange = false
set objInParam = computerSystem.Methods_("RequestStateChange").InParameters.SpawnInstance_()
if action = "start" then
objInParam.RequestedState = Enabled
else
objInParam.RequestedState = Disabled
end if
set objOutParams = computerSystem.ExecMethod_("RequestStateChange", objInParam)
if (WMIMethodStarted(objOutParams)) then
if (WMIJobCompleted(objOutParams)) then
WriteLog Format1("VM {0} was started successfully", computerSystem.ElementName)
RequestStateChange = true
end if
end if
End Function
'-----------------------------------------------------------------
' Handle wmi return values
'-----------------------------------------------------------------
Function WMIMethodStarted(outParam)
WMIMethodStarted = false
if Not IsNull(outParam) then
wmiStatus = outParam.ReturnValue
if wmiStatus = wmiStarted then
WMIMethodStarted = true
end if
end if
End Function
'-----------------------------------------------------------------
' Handle wmi Job object
'-----------------------------------------------------------------
Function WMIJobCompleted(outParam)
dim WMIJob
set WMIJob = objWMIService.Get(outParam.Job)
WMIJobCompleted = true
jobState = WMIJob.JobState
while jobState = JobRunning or jobState = JobStarting
WScript.Sleep(1000)
set WMIJob = objWMIService.Get(outParam.Job)
jobState = WMIJob.JobState
wend
if (jobState <> JobCompleted) then
WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription)
WMIJobCompleted = false
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\StartVM.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format2(myString, arg0, arg1)
Format2 = Format1(myString, arg0)
Format2 = Replace(Format2, "{1}", arg1)
End Function
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
Format1 = Replace(myString, "{0}", arg0)
End Function
Persyaratan
Persyaratan | Nilai |
---|---|
Klien minimum yang didukung |
Windows 8 [khusus aplikasi desktop] |
Server minimum yang didukung |
Windows Server 2012 [hanya aplikasi desktop] |
Ruang nama |
Root\Virtualization\V2 |
MOF |
|
DLL |
|