Delete file on Remote Server Using WMI - c#

I'm trying to delete an existing file on the remote server using WMI.
Here's my code:
string name = #"\\servername\\OCROut\\basketball.txt";
ConnectionOptions options = new ConnectionOptions(remoteServer, "username", "password", "ntlmdomain:domainName", ImpersonationLevel.Impersonate, AuthenticationLevel.Default, true, null, System.TimeSpan.MaxValue);
ManagementScope scope = new ManagementScope("\\\\server\\root\\cimv2", options);
scope.Connect();
var query = new ObjectQuery(string.Format("SELECT * FROM CIM_Datafile WHERE Drive = 'D' AND Name = '{0}' AND Filename = 'basketball' and Extension = 'txt'", name));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
var tobeDeleted = searcher.Get();
foreach (ManagementObject item in searcher.Get())
{
item.InvokeMethod("Delete", null);
}
The Query is working file but but my Count = 0 when i'm executing the searcher.Get() method. I tried everything, different slashes, without the drive, Filename and extension but nothing seem to be working and i know that the file exists.
Any help would be highly appreciated.

It seems which you are passing wrong values in the params. the Name property must contain the full local path of the file, so try this :
string name = #"D:\\OCROut\\basketball.txt";
var query = new ObjectQuery(string.Format("SELECT * FROM CIM_Datafile WHERE Name = '{0}'", name));

WMI Script to delete single/multi files in remote server
#for single file
$file = Get-WmiObject -Query "Select * from CIM_Datafile Where Name='c:\\Desktop\\a.txt'" -ComputerName 10.14.34.81 -Credential administrator
if($file)
{
$file.delete()|out-null
}
#For Multiple files in a directory
$files = Get-WmiObject -Query "ASSOCIATORS OF {Win32_Directory.Name='c:\Desktop\Temp'} Where ResultClass = CIM_DataFile" -ComputerName 10.14.34.81 -Credential administrator
if($files)
{
$files|%{$_.Delete()|out-null}
}

Related

How to get file details using C# on a remote machine with username and password

Tried to search for something similar but could not find.
I have file on a remote machine which I want to query for it`s details like version, date, etc.
How do I do that when I need to enter credentials for that machine?
FileVersionInfo does not give option to do so.
Thanks
Update:
As I said above I checked what FIleVersionInfo gives me (and also tried it) and that will not work for me.
I also tried using WMI and failed with (although it looked like the direction I need)
Here is the WMI code I tried - haven`t got much far:
var computerName = "IP_ADDRESS";
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "username";
conn.Password = "password";
conn.Authority = "ntlmdomain:DOMAIN";
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.EnablePriviledges = true;
var scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", computerName), conn);
scope.Connect();
string Drive = "c:";
string Path = "\\\\inetpub\\\\wwwroot\\\\FOLDER\\\BIN\\\File.dll";
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}", (string)WmiObject["Name"]);// String
}
I mainly need the file properties version and date.
Thanks
Thanks to #Draken comment above I`ve added missing properties on the ConnectionOptions and also fixed my mistake on the domain name.
Here is the code I use to get to the file in the network PC
var computerName = "IP_ADDRESS";
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "username";
conn.Password = "password";
conn.Authority = "ntlmdomain:DOMAIN";
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.EnablePriviledges = true;
var scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", computerName), conn);
scope.Connect();
string Drive = "c:";
string Path = "\\\\inetpub\\\\wwwroot\\\\FOLDER\\\BIN\\\File.dll";
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}", (string)WmiObject["Name"]);// String
}

Start exe file on remote machine

I have a process a method that successfully stops the process. But how can I start it? It is a .exe file that lays on the remote machines harddrive.
var ui = new ImpersonateUser();
var processName = "notepad.exe";
object[] processArgs = { #"C:\\WINDOWS\notepad.exe" };
try
{
ui.Impersonate(Domain, _userName, _pass);
ManagementPath path = new ManagementPath
{
Server = "serverName",
NamespacePath = "\\ROOT\\CIMV2",
ClassName = "Win32_Process"
};
ManagementScope scope = new ManagementScope(path);
ManagementClass management = new ManagementClass(path);
var query = new SelectQuery("SELECT * from Win32_process WHERE name = '" + processName + "'");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null); //This work
Thread.Sleep(3000);
management.InvokeMethod("Create", processArgs); //doesnt work. Why ?
}
}
}
How can I make the .exe start after I have shut it down?
You have a typo (D instead of T) in a program name. It should be noTepad.exe and not noDepad.exe Besides I suggest to check results returned by InvokeMethod In your case it returns 9 what means Path Not Found. Here is a full list of codes.
UPDATE
If InvokeMethod returns 0 but you don't see a new instance of notepad on a remote machine it means that it was run in background. However, you should be able to see this new instance in Windows Task Manager.

Searching for a File on Remote Machine WMI C#

I want to search for a file on remote machine. I don't know the EXACT file path but I know its under C:\Windows\System
My query is something like this in WMI
string querystr = "SELECT * FROM CIM_DataFile Where Path='C:\\Windows\\System'";
ObjectQuery query = new ObjectQuery(querystr );
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, query);
I get invalid query error.
Is the query valid ? Any way to specify Path Under ?
You have two issues in your code
you must double escape the \ char, because this is a reserved symbol in the WMI
the path property must not include the drive.
try this sample
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
namespace GetWMI_Info
{
class Program
{
static void Main(string[] args)
{
try
{
string ComputerName = "localhost";
ManagementScope Scope;
if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
ConnectionOptions Conn = new ConnectionOptions();
Conn.Username = "";
Conn.Password = "";
Conn.Authority = "ntlmdomain:DOMAIN";
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
}
else
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
Scope.Connect();
string Drive = "c:";
//look how the \ char is escaped.
string Path = "\\\\Windows\\\\System32\\\\";
ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0}",(string)WmiObject["Name"]);// String
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
}
}

WMI throwing Exception "Invalid Query" on second attempt to query the same server

I have a following code, almost all the time this code works for most of my server, but for some specific servers, it just fails on second part of query. this code query the SQL Server Service First and in second part it queries the SQL Sevrer Agent Service.
I have tried all possible combination of creating another Scope and Query Object, but somehow the server I am trying to query does not return seccond part, looks like after the scope is connected and first query is executed, second part is blocked by something on server..! Any help on this one is appreciated.. almost 99% of servers works fine and returns desire results, but just 2 or 3 servers fails for second part..
If this is WMI issue on the server it self..? is there any other way to achieve these status..? like IPC or Sockets..? please help..!
Hash.
try
{
agentserviceName = "SQLSERVERAGENT";
serviceName = "MSSQLSERVER";
query = new System.Management.SelectQuery(string.Format("select name, startname, State, StartMode from Win32_Service where name = '{0}'", serviceName));
ManagementScope scope = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
//ManagementScope scope = new ManagementScope("\\\\ST0176V\\root\\cimv2");
scope.Connect();
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(scope, query);
// MessageBox.Show((String)dgv_ChangeSvcAccount.Rows[i].Cells[1].Value.ToString());
foreach (ManagementObject service in searcher.Get())
{
dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = service["startname"];
dgv_ChangeSvcAccount.Rows[i].Cells[4].Tag = serviceName;
dgv_ChangeSvcAccount.Rows[i].Cells[5].Value = "Currently : " + service["State"] + " - Set As : " + service["StartMode"];
}
if (searcher.Get().Count == 0)
{
dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = "NO SQL Service Found";
}
searcher.Dispose();
ManagementScope scope2 = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
// ObjectQuery query2 = new ObjectQuery("SELECT * FROM Win32_Service WHERE NAME LIKE '" + serviceName.ToString().ToUpper() + "'");
System.Management.SelectQuery query2 = new System.Management.SelectQuery(string.Format("select name, startname, State, StartMode from Win32_Service where name like '{0}'", agentserviceName));
System.Management.ManagementObjectSearcher searcher1 = new System.Management.ManagementObjectSearcher(scope2, query2);
foreach (ManagementObject service in searcher1.Get()) // <---- this line throws exception for invalid query, and it is always 2 servers which does that, rest of servers returns proper results. the servers which throws this Invlid Query exceptions are Windows 2000 Server with SP4.
{
dgv_ChangeSvcAccount.Rows[i].Cells[6].Value = service["startname"];
dgv_ChangeSvcAccount.Rows[i].Cells[6].Tag = agentserviceName;
dgv_ChangeSvcAccount.Rows[i].Cells[7].Value = "Currently : " + service["State"] + " - Set As : " + service["StartMode"];
}
searcher1.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Since, as we've been discussing in the comments, we're thinking it might have to do with having multiple active ManagementScope objects, try changing the first half of your code to this:
string agentserviceName = "SQLSERVERAGENT";
string serviceName = "MSSQLSERVER";
// Let the SelectQuery class build our WQL query text...
string className = "Win32_Service";
string condition = string.Format("Name = '{0}'", serviceName);
string[] selectedProperties = new string[] { "Name", "StartName", "State", "StartMode" };
SelectQuery query = new SelectQuery(className, condition, selectedProperties);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
searcher.Scope = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
foreach (ManagementObject service in searcher.Get())
{
dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = service["startname"];
dgv_ChangeSvcAccount.Rows[i].Cells[4].Tag = serviceName;
dgv_ChangeSvcAccount.Rows[i].Cells[5].Value = "Currently : " + service["State"] + " - Set As : " + service["StartMode"];
}
if (searcher.Get().Count == 0)
{
dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = "NO SQL Service Found";
}
}
// Second query goes here...
That will dispose the ManagementObjectSearcher for the first query when it's done being used, and also ensure that it holds the only reference to the ManagementScope for the remote server.

OS Information of networkclient using C#

I'm working on an application which scans a given networkrange for computers. From the found clients I need to get the IP, hostname, Mac address, OS Information etc.
Now, I have all of the above, except the OS version. Does anyone have a clue on how I could achieve this?
I'm stuck.
Thanks in advance, Christophe
You could run Nmap using Process class from System.Diagnostics and parse the result:
var process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = "/c nmap -O -v targethost",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
// here you can parse to obtain the operating system
}
Creating your own OS detector in C# would be hard but if you are interested in an overview of how it works you can find it in this Nmap chapter: Chapter 8. Remote OS Detection
Use WMI, add reference to - System.Management dll and provide namespace, use following code with appropriate parameters-
ManagementScope scope = new ManagementScope();
try
{
ConnectionOptions conOptions = new ConnectionOptions();
options.Username = "<Provide username>";
options.Password = "<Provide password>";
options.EnablePrivileges = true;
options.Authority = "ntlmdomain:<domianname>";
scope = new ManagementScope(#"\\<IP address/machine name>\root\CIMV2", options);
scope.Connect();
SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
using (ManagementObjectCollection queryCollection = searcher.Get())
{
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine(string.Format("Computer Name : {0}", m["csname"]));
Console.WriteLine(string.Format("Windows Directory : {0}", m["WindowsDirectory"]));
Console.WriteLine(string.Format("Operating System: {0}", m["Caption"]));
Console.WriteLine(string.Format("Version: {0}", m["Version"]);
Console.WriteLine(string.Format("Manufacturer : {0}", m["Manufacturer"]));
}
}
}
catch (Exception ex)
{
}
You must have access right to steal this info else you will get Access right exception.

Categories

Resources