0

当我通过 Metro 应用程序插入文件路径(通过浏览到文件夹路径)时,我得到的值(在数据库中)与下面的路径相同,其中有双斜杠。因此,它总是给出这个错误:

System.Management.Automation.dll 中发生“System.Management.Automation.ParameterBindingException”类型的异常,但未在用户代码中处理

这是带有一些用于测试的虚拟值的代码:

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;
    }

编辑:

事实证明,错误不是来自文件路径。当我想指定内存大小时发生错误。当我在下面注释掉这一行时,可以创建虚拟机而不会出错:

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

因此,我不确定在指定内存大小时如何准确地创建而不会出错。在 PowerShell 中,我必须在为内存大小输入的任何数字之后包括 MB 或 GB 等,因为没有它,我无法创建虚拟机。

任何帮助是极大的赞赏。

谢谢。

4

0 回答 0