Thursday, June 27, 2013

Getting error in change IPAddress,Subnet,DNSServer of the Virtual Machine using C#



private void SetNetAdapter()
        {
            string vmName = "VMURA";//D0B84C1C-E68C-4FD1-95CC-3838A30A7374

            ManagementScope _scope = new ManagementScope(@"root\virtualization\V2", null);
            ManagementObject virtualSystem = Utility.GetTargetComputer(vmName, _scope);
            ManagementObjectCollection virtualSystemSettings = virtualSystem.GetRelated//("Msvm_VirtualSystemSettingData");
            (
                "Msvm_VirtualSystemSettingData",
                "Msvm_SettingsDefineState",
                null,
                null,
                "SettingData",
                "ManagedElement",
                false,
                null
            );

         
            ManagementObject virtualSystemSetting = null;

            foreach (ManagementObject instance in virtualSystemSettings)
            {
                virtualSystemSetting = instance;
            }
            ManagementObjectCollection VMNetAdapters = virtualSystemSetting.GetRelated("Msvm_SyntheticEthernetPortSettingData");
            List<ManagementObjectCollection> NetworkSettings = new List<ManagementObjectCollection>();

            foreach (ManagementObject instanceAdp in VMNetAdapters)
            {
                NetworkSettings.Add(instanceAdp.GetRelated("Msvm_GuestNetworkAdapterConfiguration"));
            }
            var _0 = NetworkSettings[0].OfType<ManagementBaseObject>().First();
           
            _0["InstanceID"] = "2B36F20C-E133-4098-A38A-A15A35E206A8";
            _0["ProtocolIFType"] = "4096";
            _0["DHCPEnabled"] = true;
            _0["IPAddresses"] = new string[] { "192.168.63.236" };
            _0["Subnets"] = new string[] { "255.255.0.0" };
            _0["DefaultGateways"] = new string[] { "192.168.100.101" };
            _0["DNSServers"] = new string[] { "192.168.100.1" };
            virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");

            ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("SetGuestNetworkAdapterConfiguration");
            inParams["ComputerSystem"] = virtualSystem.Path.Path;
            inParams["NetworkConfiguration"] = new ManagementBaseObject[] { _0 };

            ManagementBaseObject addAdapterOutParams = virtualSystemService.InvokeMethod("SetGuestNetworkAdapterConfiguration", inParams, null);

            using (ManagementObject job = new ManagementObject((string)addAdapterOutParams["Job"]))
            {

                job.Scope = scope;
                JobState jobState = (JobState)((ushort)job["jobState"]);

                while (!((jobState == JobState.Completed) || (jobState == JobState.CompletedWithWarnings) || (jobState == JobState.Terminated) || (jobState == JobState.Exception) || (jobState == JobState.Killed)))
                {
                    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                    job.Get();
                }
                MessageBox.Show(job["ErrorDescription"].ToString());
                StringBuilder strAdptr = new StringBuilder();
                foreach (var item in job.Properties)
                {
                    strAdptr.AppendLine(item.Name + ":\t" + item.Value);
                }
                MessageBox.Show(strAdptr.ToString());
            }

}


Tuesday, June 11, 2013

A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)

A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)
Solution
Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >>SQL Server Network Configuration>>Protocols for SQLEXPRESS>>Right Click on TCP/IP >> Click on Enable
You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.
Thanks
Alok Kumar Sharma