get file from remote machine using WMI

Hi,
I need to copy a file from a remote machine using wmi but i don't know how to define the path to my machine, below is the code that makes a copy of a file in the same machine, what should be the path if i wanted to copy it to my c:\example\ folder

ManagementBaseObject inParams, outParams;
foreach (ManagementObject mo in queryCollection)
{
Console.WriteLine(mo["Name"].ToString());

inParams = mo.GetMethodParameters("Copy");
inParams["FileName"] = "c:\\example.config; <----------------- what can i put here so that it refers to my machine
outParams = mo.InvokeMethod("Copy", inParams, null);

// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}

-----EDIT:-----

So, i've searched and experimented the whole weekend and nothing.
When I execute this code i get "returnValue 9" wich according to the WMI documentation represents "Invalid object".

The operation is sucessfull if i try to copy between folder on the same machine.

I'm using Windows Server 2003 on both machines.

I don't know any other way to copy a file from the remote machine to my machine.

           oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and Drive = 'c:' and FileName = 'machine' and path like '\\windows\\microsoft.net\\framework\\%\\config%'");

            query = new ManagementObjectSearcher(ms, oq);
            queryCollection = query.Get();

            ManagementBaseObject inParams, outParams;
            foreach (ManagementObject mo2 in queryCollection)
            {
                Console.WriteLine(mo2["Name"].ToString());

                inParams = mo2.GetMethodParameters("Copy");
                inParams["FileName"] =  "\\\\ibm-server2003\\I$\\machine.config";
                outParams = mo2.InvokeMethod("Copy", inParams, null);
              
                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
             
              
            }


Answer this question

get file from remote machine using WMI

  • John.Doe

    Could you specify a litle bit better, i didn't get what you said. Now my doubt is in how to map my folder on a remote machine.

  • museicon

    Well, i gave up on WMI and followed an advice from a user on Chanel9.

    File.Copy(@"\\toshiba-win2003\C$\windows\microsoft.net\framework\v2.0.50727\config\machine.config",@"i:\machine.config");

    I thought i needed some kind of permisson to use the method... seems not.

    tkns for the help anyways.

  • Joeschoe

    So, i've searched and experimented the whole weekend and nothing.
    When I execute this code i get "returnValue 9" wich according to the WMI documentation represents "Invalid object".

    The operation is sucessfull if i try to copy between folder on the same machine.

    I'm using Windows Server 2003 on both machines.

    I don't know any other way to copy a file from the remote machine to my machine.

    oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and Drive = 'c:' and FileName = 'machine' and path like '\\windows\\microsoft.net\\framework\\%\\config%'");

    query = new ManagementObjectSearcher(ms, oq);
    queryCollection = query.Get();

    ManagementBaseObject inParams, outParams;
    foreach (ManagementObject mo2 in queryCollection)
    {
    Console.WriteLine(mo2["Name"].ToString());

    inParams = mo2.GetMethodParameters("Copy");
    inParams["FileName"] = "\\\\ibm-server2003\\I$\\machine.config";
    outParams = mo2.InvokeMethod("Copy", inParams, null);

    // List outParams
    Console.WriteLine("Out parameters:");
    Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);


    }

  • stallion_alpa

  • parivesh

    why don't you map the remote machine to a drive (map network drive) then use the File.Copy() method in .NET to copy the file from the remote machine which has been mapped, to the local drive

    is this not an option



  • Any ole Joe

    well yes it is possible in Windows, just map the share drive then use File.Move class/method in .NET to copy the file



  • babarzhr

    well, i don't know if this is what you were thinking but i don't think it's so bad if i do the reverse mapping, in other words i map the application folder on the remote machine and use wmi to copy the file there, instead of mapping dozens of drives to my machine.

    Can you tell me if this is possible

  • Gunnar Adler

    well, make sure that the folder is shared (right click on folder > sharing & security and specify a share name)

    then on the remote computer simply map the shared folder by right clicking my computer > map a network drive and enter the shared folder:

    \\computername\ShareName

    this will then make your shared folder available on the remote machine from which the remote machine can access and copy files to and from.

    is this what you are after



  • Siteadm

    I never done it but it seems to me that mapping a drive is overkill since i just want to make a copy of the .net machine.config file to my machine.

    Not mentioning that the application is multi-thread, if i do that, i'll have dozens of drives mapped at the same time.

  • bennett1016

    Yes, you are correct.

    I'm using WMI and at the moment i can copy the file between folders on the remote machine, but not from the remote machine to a folder on my local machine.

    The application i'm developing is in C#.

    The code approach i'm trying is this:

    //this query is for testing porpose only
    oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and FileName = 'machine' and path like '%\\windows\\microsoft.net\\framework\\%\\config%'");

    query = new ManagementObjectSearcher(ms, oq);
    queryCollection = query.Get();


    ManagementBaseObject inParams, outParams;
    foreach (ManagementObject mo2 in queryCollection)
    {
    Console.WriteLine(mo2["Name"].ToString());

    inParams = mo2.GetMethodParameters("Copy");
    inParams["FileName"] = "\\\\ibm-xp\\C$\\machine.config"; <--- This line doesn't work
    outParams = mo2.InvokeMethod("Copy", inParams, null);

    // List outParams
    Console.WriteLine("Out parameters:");
    Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
    break;
    }

  • Frank Racis

    well not quite, i need to do this programatically, i have to do all this operations without phisically touching the other computers. :\

  • SomeDeveloperPerson

    try this:


    System.Text.StringBuilder sb = new StringBuilder();
    ConnectionOptions oConn = new ConnectionOptions();
    ManagementScope scope = new ManagementScope("\\\\server\\root\\cimv2", oConn);
    sb.Append(
    "SELECT * FROM CIM_Datafile WHERE Extension = 'config' AND FileName = 'machine'");// AND path LIKE '%\\win2000\\microsoft.net\\framework%'");

    ObjectQuery oQuery = new ObjectQuery(sb.ToString());
    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(scope, oQuery);
    ManagementObjectCollection oReturnCollection = oSearcher.Get();
    foreach (ManagementObject oReturn in oReturnCollection)
    {
    //MessageBox.Show(oReturn["Name"].ToString());
    oReturn.InvokeMethod(
    "Copy", new object[] { oReturn["Name"], "\\yourServer\\path\\filename.ext" });

    }





  • Freqy

    well the same approach would kind of also apply with WMI since it needs to some how reference the other machine...

    ill see what i can dig up



  • LalitSRana

    ok I see, I was confused!

    so you want to copy a file from a remote computer TO your local computer correct

    well this I believe can be done, as you have done, in WMI

    do you know the commands in WMI itself (without implementing it in C#)

    if so - are you able to post them

    Then I can try to help you out integrating them into C# hopefully

    try to see if this helps:

    http://forums.devshed.com/net-development-87/invoking-copy-from-wmi-between-connections-130157.html



  • get file from remote machine using WMI