C# .NET - How to Run commands on Remote machine from client machine
Asked By karthik vadla on 31-Mar-12 04:42 AM
Hi all,
I'm in need of an application which will run commands on remote machine from client machine.
For example:
As a SDET (software design engineer and tester) I need to do load tests at my work place.Before doing this, i have to perform few prerequisites like restarting the IIS servers, SQL servers and need to delete files in some folders. I have to perform all these tasks daily around 10-15 remote systems, It's great burden on me.
In order to avoid this hassle, i started working on some automation which will do all these tasks. I have done with code which will run on local machine like restarting IIS server using c# app. Now i'm planning to connect remote machine based on the selection of machine name in my application interface and after connecting, when i click on IIS restart button., it should run the code on the remote machine.
I'm in great need of this application....anyhelp from your side would be appreciated immensely. Thank you
Please help me out.
kalpana aparnathi replied to karthik vadla on 31-Mar-12 06:15 AM
hi,
Use this example code :
object[] theProcessToRun = { "notepad.exe" };
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Username = "username";
theConnection.Password = "password";
ManagementScope theScope = new ManagementScope("\\\\" + IP + "\\root\\cimv2", theConnection);
ManagementClass theClass = new ManagementClass(theScope,
new ManagementPath("Win32_Process"), new ObjectGetOptions());
theClass.InvokeMethod("Create", theProcessToRun);
Regards,
karthik vadla replied to kalpana aparnathi on 31-Mar-12 07:02 AM
Thank you Kalpana..!!
Sure, Will check out.
Regards
Karthik
karthik vadla replied to kalpana aparnathi on 03-Apr-12 05:50 AM
Hi,
I tried out the sample code , but giving an error like : "A RPC server is unavailable". I tried to turn off firewall on remote machine but i don't have admin rights.
Any suggestions please, how to resolve it.
Regards
Karthik

insatlled software list using C#. The below code is not working for me. System.Management. ManagementScope oMs = new System.Management. ManagementScope ( " \ \ " + "Computername" + " \ root \ cimv2" , oConn); System.Management. ObjectQuery oQuery1 = new System.Management. ObjectQuery ( "SELECT * FROM Win32_Product administrative rights Also : using System.Management; public static string ConnectToRegistry ( string servername) { string regKeyToGet, keyToRead; ConnectionOptions oConn = new ConnectionOptions(); oConn.Username = "admin" ; oConn.Password = "pass" ; ManagementScope scope = new ManagementScope( @" \ " + servername + @" \ root \ default" , oConn); registry = new ManagementClass(scope, new ManagementPath( "StdRegProv" ), null ); / / Returns a specific value for a specified key ManagementBaseObject inParams en / details.aspx?familyid = 1D24563D-CAC9-4017-AF14-8DD686A96540&displaylang = en keywords: ManagementObjectCollectionManagementObjectSearcher, ManagementBaseObject, OpenRemoteBaseKey, ConnectionOptions, ManagementObject description: C# code to fetch Remote pc installed softwares list Hi, Can someone provide
space on a shared remote drive. my shared-name goes something like this - - \ RemoteServer \ ADrive$ ConnectionOptions oConn = new ConnectionOptions(); oConn.Username = "xUser"; oConn.Password = "xxxx"; string sSrvName = @" \ RemoteServer \ ADrive$ "; ManagementScope oMS = new ManagementScope(sSrvName, oConn); I get the error - "Invalid Parameter". If i remove the $ sign from the msdn.microsoft.com / Forums / en-US / netfxbcl / thread / 51f3ad3f-cfc3-4793-b83a-cc7e852084c2 keywords: ManagementObjectCollectionManagementObjectSearcher, ConnectionOptions, ManagementScope, disk, RPC server is unavailable description: Remote WMI Connection issue i needed to get the
a pc using code that will work on all windows formats? Thanks in advance Stefan ManagementClass oMClass = new ManagementClass ( "Win32_NetworkAdapterConfiguration" ); ManagementObjectCollection colMObj = oMCLass . GetInstances (); foreach ( ManagementObject objMO in colMObj ) Console . WriteLine ( objMO [ "MacAddress" ]. ToString Connection credentials to the remote computer - not needed if the logged in account has access ConnectionOptions oConn = new ConnectionOptions(); oConn.Username = "Administrator" ; oConn.Password = "" ; System.Management.ManagementScope oMs = new System.Management.ManagementScope( \ MachineID, oConn); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery( "select SerialNumber from Win32_BaseBoard" ); ManagementObjectSearcher Serial Number : " + oReturn[ "SerialNumber" ].ToString()); } Thanks for the help That link Helped alot keywords: ManagementObjectCollectionManagementObjectSearcher, ConnectionOptions, ManagementObject, ManagementScope, ManagementClass description: Unique PC id's Hi All I am trying to get a unique
to the remote computer - / / logged in account must have adminstrator access for a remote machine ConnectionOptions oConn = new ConnectionOptions(); / / oConn.Username = "username"; / / oConn.Password = "password"; string remoteMachineName = "localhost"; System.Management.ManagementScope oMs = new System.Management.ManagementScope(" \ \ " +remoteMachineName , oConn); / / pass the name of the service to query with this particular WQL query WHICH ARE IN DIFFERENT DOMAIN. please reply Thanks in advance, Balaji try { bool connected = false ; ConnectionOptions cred = new ConnectionOptions(); cred.Username = ConfigurationManager.AppSettings[ "RemoteMachineUserName" ].ToString(); cred.Password = ConfigurationManager.AppSettings[ "RemoteMachinePassword" ].ToString(); String hostName = Dns.GetHostByAddress(ConfigurationManager.AppSettings[ "RemoteMachineServiceIPAddress" ].ToString()).HostName; ManagementScope srvScope; srvScope = new ManagementScope( string .Format( " \ \ {0} \ root \ cimv2" , hostName), cred); ManagementPath win32Path = new ManagementPath( "Win32_Service" ); ManagementClass services; services
me, I want to track MAC address from where application is accessed public string getMACAddress() { ManagementClass mc = new ManagementClass ( "Win32_NetworkAdapterConfiguration" ); ManagementObjectCollection moc = mc.GetInstances(); string MACAddress = String .Empty; foreach ( ManagementObject mo in moc) { if 21-2006 http: / / www.dotnet247.com / 247reference / msgs / 18 / 91898.aspx ManagementObjectSearcher query1; ManagementObjectCollection queryCollection1; ManagementScope RemoteConn; ObjectQuery oQuery = new ObjectQuery("Select MacAddress, IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled = TRUE"); ConnectionOptions options = new ConnectionOptions(); options.Username = sDomain + " \ " + sUserName; / / could be in domain \ user format options.Password = sPwd; try { RemoteConn new ManagementScope(" \ \ " + RemoteHost + " \ root \ cimv2", options); query1 = new ManagementObjectSearche(RemoteConn, oQuery); queryCollection1 = query1.Get(); foreach( ManagementObject mo this code to find MAC id Public Function GetMACAddress() As String Dim mc As New ManagementClass( "Win32_NetworkAdapterConfiguration" ) Dim moc As ManagementObjectCollection = mc.GetInstances() Dim MACAddress As String = [ String ].Empty For Each all version of windows with the .Net runtime. Here's a code example: System.Management.ManagementClass mc = default(System.Management.ManagementClass); ManagementObject mo = default(ManagementObject); mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc
this code to find MAC id Public Function GetMACAddress() As String Dim mc As New ManagementClass( "Win32_NetworkAdapterConfiguration" ) Dim moc As ManagementObjectCollection = mc.GetInstances() Dim MACAddress As String = [ String ].Empty For Each your source code. Code Block Imports System.Management Module Module1 Sub Main() Dim oConn As ConnectionOptions = New ConnectionOptions oConn.Username = "Administrator" oConn.Password = "password" Dim oMs As System.Management.ManagementScope = New System.Management.ManagementScope( " \ machineID" , oConn) Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery( "select SerialNumber from Win32_MotherboardDevice Class hope this will help you keywords: VB.NET System imformationManagementObjectCollection, ManagementObjectSearcher, vb 6, ConnectionOptions, ManagementObject description: System imformation How to read system MAC id / serial number in vb.net
partition name Public Function GetDiskSpace() As System.UInt64 Dim diskClass As _ New System.Management.ManagementClass("Win32_LogicalDisk") Dim disks As System.Management.ManagementObjectCollection = _ diskClass.GetInstances() Dim disk As System.Management computer - not needed if the logged in account has access Try Dim oConn As New ConnectionOptions oConn.Username = "UserName" oConn.Password = "Password" Dim svlDetails As String = "" Dim svlDetails1 As String = "" Dim svlDetails2 As String = "" Dim oMs As New System.Management.ManagementScope(" \ <MachineName> ", oConn) 'get Fixed disk stats Dim oQuery As New System.Management.ObjectQuery("select FreeSpace will be ssaved in another textbox. . . can it be done? using array? thanks. . . keywords: ManagementObjectCollectionManagementObjectSearcher, ConnectionOptions, ManagementObject, ManagementScope, ManagementClass description: really in nid of help my task needs me to do a simple programme
NET 06-Jun-13 10:17 AM I have the following code - string machine = "ServerName1" ; ConnectionOptions co = new ConnectionOptions (); co.Impersonation = ImpersonationLevel .Impersonate; co.EnablePrivileges = true ; co.Authentication = AuthenticationLevel .Connect; ManagementScope scope = new ManagementScope ( " \ \ " + machine + " \ root \ cimv2" , co); scope.Connect(); Its working for my Local machine, but not on having the same issue. Find out a way to overcome it. . ?? Please help me keywords: ConnectionOptions, ManagementScope, Impersonate, Connect, Local, Query access, scope description: WMI Query access denied I have the following code - string machine = ServerName1; ConnectionOptions co = new ConnectionOptions(); co.Impersonat
WiFi signal strength on windows using C#?? Try this private double RetrieveSignalString() { double theSignalStrength = 0; ConnectionOptions theConnectionOptions = new ConnectionOptions (); ManagementScope theManagementScope = new ManagementScope ( "root \ wmi" ); ObjectQuery theObjectQuery = new ObjectQuery ( "SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE active = true" ); ManagementObjectSearcher theQuery = new public static string GetWIFISignalStrength() { try { ObjectQuery query = new ObjectQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength Where active = true"); ManagementScope scope = new ManagementScope("root \ wmi"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); string result = ""; foreach (ManagementObject obj in searcher mo[ "Ndis80211ReceivedSignalStrength" ].ToString() ); break ; } } catch ( Exception ex) { } return returnStrength; } keywords: Windows VistaManagementObjectCollection, ManagementObjectSearcher, Error, Vista, ConnectionOptions description: How to get WiFi signal strength on windows (XP / Vista / 7) How can i