我在Azure中有几个永久虚拟机,我想在某些情况下以编程方式启动/停止。但他们已经手动启动或停止了。
如果VM启动了,我可以通过REST API告诉我吗?
答案 0 :(得分:0)
您可以调用Get Deployment
操作(它也可以在您的永久虚拟机上运行),这将为您提供部署状态,您可以使用该状态决定是否需要在虚拟机上执行操作。
class Program
{
static string subscriptionId = "subscription id";
static string certificateData = "Data from ManagementCertificate node in publish setting file ";
static string cloudServiceName = "name of the cloud service";
static X509Certificate2 cert = null;
static void Main(string[] args)
{
cert = new X509Certificate2(Convert.FromBase64String(certificateData));
Test2();
Console.ReadLine();
}
static void Test2()
{
string endpoint = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/production", subscriptionId, cloudServiceName);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(endpoint);
req.ClientCertificates.Add(cert);
req.ContentType = "application/xml";
req.Headers.Add("x-ms-version", "2013-03-01");
req.Method = "GET";
try
{
using (var resp = req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
var responseData = sr.ReadToEnd();
Console.WriteLine(responseData);
}
}
}
catch (WebException webEx)
{
using (var resp = webEx.Response)
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
var errorDetails = sr.ReadToEnd();
Console.WriteLine(errorDetails);
}
}
}
}