Introduction

This sample shows how you can parse the output of csupload tool with Get-VMImage switch so as to display the list of uploaded vhd images in a better manner.

 

Prerequisite


Description

You can get the list of all the VM Images you have uploaded to your subscription by running the following command from your command prompt:  

 

Windows Shell Script
Edit|Remove
csupload.exe Get-VMImage -Connection "SubscriptionId=xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx;CertificateThumbprint=xxxxxxxxxxxxxxxxxxxxxxxx"
However, the output you get is just on your command prompt window and is not presentable. The output looks like as below:

 

To make this more presentable and to view it in a grid, we make call to the above command by calling the csupload process from our C# code, and then parsing the output and adding properties such as name, label etc to an object. This object is then bound to a Gridview.

 

Note: Following code is just for sample purpse only, and might not be optimized one. Please check before using it in production.

 

C#
Edit|Remove
protected void Page_Load(object sender, EventArgs e) 
        { 
            // Start the process. 
            Process p = new Process(); 
            // Redirect the output stream. 
            p.StartInfo.UseShellExecute = false; 
            p.StartInfo.RedirectStandardOutput = true; 
 
            //change the path as per your SDK 
            p.StartInfo.FileName = @"C:\Program Files\Windows Azure SDK\v1.3\bin\csupload.exe"; 
 
            //TODO: you can get the subscription id and thumbprint values form config file 
            p.StartInfo.Arguments = "Get-VMImage -Connection \"SubscriptionId=a1e16723-14b2-40c7-9bbb-dba3ecbcab70;CertificateThumbprint=D72DBFBAF3F21971F897DA45DEC49991D6809E66\""; 
            p.Start(); 
            string output = p.StandardOutput.ReadToEnd(); 
            p.WaitForExit(); 
 
            //splitting based on two next line characters, as each vhd detail is sepearted by a blank line 
            string[] singleDetails = Regex.Split(output, "\r\n\r\n"); 
 
            //object to hold VHD properties 
            List<AzureVHD> list = new List<AzureVHD>(); 
 
            //loop through each detail block 
            for (int i = 1; i < singleDetails.Length - 1; i++) 
            { 
                AzureVHD vhd = new AzureVHD(); 
 
                //split on next line character to sepearte out each line describing a single property 
                string[] vhdProperties = Regex.Split(singleDetails[i], "\r\n"); 
 
                foreach (string property in vhdProperties) 
                { 
                    if (property.StartsWith("Name")) 
                    { 
                        vhd.Name = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    if (property.StartsWith("Label")) 
                    { 
                        vhd.Label = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    if (property.StartsWith("Status")) 
                    { 
                        vhd.Status = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    if (property.Split(':')[0].Trim() == "Parent") 
                    { 
                        vhd.Parent = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    //if (property.StartsWith("Uuid")) 
                    //{ 
                    //    vhd.UID = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    //} 
                    if (property.StartsWith("Timestamp")) 
                    { 
                        vhd.TimeStamp = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    if (property.StartsWith("MountSizeInBytes")) 
                    { 
                        vhd.MountSizeInBytes = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    if (property.StartsWith("CompressedSizeInBytes")) 
                    { 
                        vhd.CompressedSizeInBytes = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    } 
                    //if (property.StartsWith("ParentUuid")) 
                    //{ 
                    //    vhd.ParentUuid = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    //} 
                    //if (property.StartsWith("ParentTimestamp")) 
                    //{ 
                    //    vhd.ParentTimeStamp = property.Substring(property.IndexOf(':') + 1).Trim(); 
                    //} 
                } 
                list.Add(vhd); 
                 
            } 
            Label1.Text = "Total VM Images Uploaded : " + list.Count.ToString(); 
            GridView1.DataSource = list; 
            GridView1.DataBind(); 
        }

In the code above, we ensure that complete output has been read to the standard output by making call to WaitForExit method. Then we parse the output and add it to list of object of type AzureVHD. This class is declared as below:

 

C#
Edit|Remove
public class AzureVHD 
    { 
        public string Name { getset; } 
        public string Label { getset; } 
        public string Status { getset; } 
        public string Parent { getset; } 
        //public string UID { get; set; } 
        public string TimeStamp { getset; } 
        public string MountSizeInBytes { getset; } 
        public string CompressedSizeInBytes { getset; } 
        //public string ParentUuid { get; set; } 
        //public string ParentTimeStamp { get; set; } 
 
    }
 

Final output looks like as follow:

 

 

Source Code Files