KALYAN BHAVE

KALYAN BHAVE

  • NA
  • 2
  • 2.7k

How to login to different windows server at a time

Feb 1 2013 10:16 AM

Hi Team, This is my code which will log into one particular windows server and making the web service down.Can you please help me in login to different server with same Windows Id and password and make the same webservice down at a time in all the servers.

form.cs is my file name

*****************My code**************************



 

using

System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

using

System.Windows.Forms;

using

System.Management;

using

sac;

namespace

sac

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

try

{

ConnectionOptions options = new ConnectionOptions();

options.Username =

"ad.xxx.xxx";

options.Password =

"xxx";

ManagementScope scope = new ManagementScope("\\\\servername\\root\\cimv2", options);

//ManagementPath path = new ManagementPath("Win32_Process");

//ObjectGetOptions o = new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 5), true);

scope.Connect();

string strQuery = "SELECT * FROM Win32_Service where Name = 'PSR Instance 7200'";

ManagementObjectSearcher moSearch = new ManagementObjectSearcher(scope, new ObjectQuery(strQuery));

ManagementObjectCollection moServices = moSearch.Get();

ManagementObject moService=null;

foreach(ManagementObject o in moServices)

moService=o;

Console.WriteLine(moServices.Count);

ManagementOperationObserver observer = new ManagementOperationObserver();

InvokeMethodCompleteHandler completionHandlerObj = new InvokeMethodCompleteHandler();

observer.Completed +=

new CompletedEventHandler(completionHandlerObj.Done);

observer.ObjectReady +=

new ObjectReadyEventHandler(completionHandlerObj.NewObject);

moService.InvokeMethod(observer,

"StopService", null);

int nCount = 0;

string strMsg=null;

while (!completionHandlerObj.IsComplete)

{

if (nCount > 10)

{

strMsg =

"manageService: Failed to ";

break;

}

//wait 1/2 sec.

System.Threading.

Thread.Sleep(500);

nCount++;

}

//if call was not successful.

if (completionHandlerObj.ReturnObject != null)

{

if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")

{

strMsg =

"manageService: executed successfuly";

}

else

{

strMsg =

"manageService: Failed to ";

}

}

Console.WriteLine(strMsg);

label1.Text = strMsg;

}

catch (Exception exc)

{

Console.WriteLine("Exception caugh" + exc.ToString());

}

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

using

System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

using

System.Windows.Forms;

using

System.Management;

using

sac;

namespace

sac

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

try

{

ConnectionOptions options = new ConnectionOptions();

options.Username =

"ad.kalyan.bhave";

options.Password =

"KALson!@3";

ManagementScope scope = new ManagementScope("\\\\napb0044pdv\\root\\cimv2", options);

//ManagementPath path = new ManagementPath("Win32_Process");

//ObjectGetOptions o = new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 5), true);

scope.Connect();

string strQuery = "SELECT * FROM Win32_Service where Name = 'PSR Instance 7200'";

ManagementObjectSearcher moSearch = new ManagementObjectSearcher(scope, new ObjectQuery(strQuery));

ManagementObjectCollection moServices = moSearch.Get();

ManagementObject moService=null;

foreach(ManagementObject o in moServices)

moService=o;

Console.WriteLine(moServices.Count);

ManagementOperationObserver observer = new ManagementOperationObserver();

InvokeMethodCompleteHandler completionHandlerObj = new InvokeMethodCompleteHandler();

observer.Completed +=

new CompletedEventHandler(completionHandlerObj.Done);

observer.ObjectReady +=

new ObjectReadyEventHandler(completionHandlerObj.NewObject);

moService.InvokeMethod(observer,

"StopService", null);

int nCount = 0;

string strMsg=null;

while (!completionHandlerObj.IsComplete)

{

if (nCount > 10)

{

strMsg =

"manageService: Failed to ";

break;

}

//wait 1/2 sec.

System.Threading.

Thread.Sleep(500);

nCount++;

}

//if call was not successful.

if (completionHandlerObj.ReturnObject != null)

{

if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")

{

strMsg =

"manageService: executed successfuly";

}

else

{

strMsg =

"manageService: Failed to ";

}

}

Console.WriteLine(strMsg);

label1.Text = strMsg;

}

catch (Exception exc)

{

Console.WriteLine("Exception caugh" + exc.ToString());

}

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}



***************************************



 

My class.cs file



 

********************************my code **************************



 

using

System.Management;

using

System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

namespace

sac

{

public class InvokeMethodCompleteHandler

{

private bool isComplete = false;

private ManagementBaseObject returnObject;

/// <summary>

/// Trigger Done event when InvokeMethod is complete

/// </summary>

public void Done(object sender, CompletedEventArgs e)

{

isComplete =

true;

}

public void NewObject(object sender, ObjectReadyEventArgs obj)

{

returnObject = obj.NewObject;

}

/// <summary>

/// Get property IsComplete

/// </summary>

public bool IsComplete

{

get

{

return isComplete;

}

}

/// <summary>

/// Property allows accessing the result

/// object in the main function

/// </summary>

public ManagementBaseObject ReturnObject

{

get

{

return returnObject;

}

}

}

}

using

System.Management;

using

System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

namespace

sac

{

public class InvokeMethodCompleteHandler

{

private bool isComplete = false;

private ManagementBaseObject returnObject;

/// <summary>

/// Trigger Done event when InvokeMethod is complete

/// </summary>

public void Done(object sender, CompletedEventArgs e)

{

isComplete =

true;

}

public void NewObject(object sender, ObjectReadyEventArgs obj)

{

returnObject = obj.NewObject;

}

/// <summary>

/// Get property IsComplete

/// </summary>

public bool IsComplete

{

get

{

return isComplete;

}

}

/// <summary>

/// Property allows accessing the result

/// object in the main function

/// </summary>

public ManagementBaseObject ReturnObject

{

get

{

return returnObject;

}

}

}

}