0

When I insert file path through my metro app (by browsing to the folder path), the value that I get (in the database) is the same as the below path whereby there's double slashes. Thus, it always give this error:

An exception of type 'System.Management.Automation.ParameterBindingException' occurred in System.Management.Automation.dll but was not handled in user code

Here's the code with some dummy values for testing:

public bool InsertNewVm(VirtualMachineDetails addVmDetails)
    {
        bool addVmMsg = true;
        //string vmName = addVmDetails.VmName.ToString();
        addVmDetails.VmName = "NewMedia";
        addVmDetails.VmRam = "512MB";
        addVmDetails.VmHardDisk = "C:\\Users\\Public\\Documents\\Hyper-V\\Virtual hard disks\\Win8 Virtual Machine.vhdx";
        addVmDetails.VmOS = "Windows 8";
        addVmDetails.VmIncSoftware = "Visual Studio 2012";
        addVmDetails.VmNetwork = "Virtual Switch";
        addVmDetails.VmDiploma = "INE";
        addVmDetails.VmYear = "Year 1";
        addVmDetails.VmModule = "New Media";

        conn.Open();

        if (addVmDetails.VmName != "")
        {
            string checkkVmAdd = "SELECT * FROM VirtualMachine where VMName = '" + addVmDetails.VmName.ToString() + "'";
            SqlCommand cmdValidate = new SqlCommand(checkkVmAdd, conn);

            SqlDataReader dr = cmdValidate.ExecuteReader();

            while (dr.Read())
            {
                if (dr["VMName"].ToString() == addVmDetails.VmName.ToString()) 
                {
                    addVmMsg = false;
                    return addVmMsg;
                }
            }
            dr.Close();

            string insertVm = "INSERT INTO VirtualMachine(VMName, Ram, HardDisk, OS, IncludedSoftware, SwitchName, Diploma, Year, Module) values ('" + addVmDetails.VmName.Trim() +
            "','" + addVmDetails.VmRam.Trim() + "','" + addVmDetails.VmHardDisk.Trim() + "','" + addVmDetails.VmOS.Trim() + "','" + addVmDetails.VmIncSoftware.Trim() + "','" + addVmDetails.VmNetwork.Trim() +
            "','" + addVmDetails.VmDiploma.Trim() + "','" + addVmDetails.VmYear.Trim() + "','" + addVmDetails.VmModule.Trim() + "')";

            SqlCommand cmdInsert = new SqlCommand(insertVm, conn);

            cmdInsert.ExecuteNonQuery();

            PowerShell ps = PowerShell.Create();
            ps.AddCommand("New-VM");
            ps.AddParameter("Name", addVmDetails.VmName.Trim());
            ps.AddParameter("VHDPath", addVmDetails.VmHardDisk.Trim());
            ps.AddParameter("SwitchName", addVmDetails.VmNetwork.Trim());
            ps.AddParameter("MemoryStartupBytes", addVmDetails.VmRam.Trim());
            var result = ps.Invoke();

            conn.Close();
            return addVmMsg;
        }
        else
        {
            addVmMsg = false;
        }
        return addVmMsg;
    }

EDIT:

It turns out that the error isn't coming from the file path. Error occurs when I want to specify the memory size. When I comment out this line below, virtual machine can be created without error:

ps.AddParameter("MemoryStartupBytes", addVmDetails.VmRam.Trim())

Thus, I'm unsure how exactly to create without error when specifying the memory size. In PowerShell, I have to include MB or GB etc after any numbers that I entered for the memory size as without it, I can't create the virtual machine.

Any help is greatly appreciated.

Thanks.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 1
    try using addVmDetails.VmHardDisk = String.Format("'{0}'", @Path) – Dmitry Savy Jul 02 '13 at 07:26
  • @DmitrySavy It turns out that the file path is not the problem. Even if I declare it as @"C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Win8 Virtual Machine.vhdx", a virtual machine can be created. It's the MemoryStartupBytes problem. It gives me error when I want to specify the memory size. By default the value is 512MB. So, if I comment out the "ps.AddParameter("MemoryStartupBytes", addVmDetails.VmRam.Trim());" line, the virtual machine can be created without error since it uses the default value without me specifying the size. Error happens when I want to specify the size. – user2541476 Jul 02 '13 at 09:14

0 Answers0