Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Error in execution of PowerShell C# codings

$
0
0

Hi,

I have difficulty creating new virtual machine. I always get this error when invoking the powershell codes:

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

I'm creating it through metro app which implements a service and inserting the data that I typed into the database. While this is being done, the code also creates a virtual machine through powershell codes. 

Here's the code for the service that I use. I've inserted some dummy values for testing:

public bool InsertNewVm(VirtualMachineDetails addVmDetails)
        {
            bool addVmMsg = true;
            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;
        }

I suspect that the error is due to this file path:

"C:\\Users\\Public\\Documents\\Hyper-V\\Virtual hard disks\\Win8 Virtual Machine.vhdx";

How can I insert this path into the powershell code without getting error? When I insert it through my app (by browsing to the folder path), the value that I get is the same as the above path whereby there's double \\. Thus, it always give an error.

Can someone help me out on this as I'm in deep need of assistance to overcome this problem?

Any help is greatly appreciated. 

Thanks.



Viewing all articles
Browse latest Browse all 21975

Trending Articles