Kaseya Community

Error Checking with File sizes

This question is answered

Hello!

I've been working on going into our mountain of Agent Procedures and adding error checking to APs that were created some time ago.  for the most part, I've been successful.   But something I want to add is some verification around file sizes.  Example:

We used to just test to see if a file was installed - if not, we'd copy down a msi (or exe) installer and run it with whatever switches it needed.  I've added some other bits to check on, but I'd like to say "Check to see if this installer exists.  If yes, check if the file size of the installer on the server is equal to the file size of teh installer on the client.  If no, copy the file from the server to the client." 

I thought I had an answer to this by simply using getVariable to check the "File Size" property using the server's network location (\\server\folder\file.exe) and another using the client's local location (C:\kworking\folder\file.exe).  this works fine when people are in the office, but it doesn't work so well when they are at home, or elsewhere outside our network.  I tried using the file path Kaseya APs use when writing a file (VSASharedFiles\folder\file.exe) but that fails no matter where someone is located.

Is there a way to check a file path on the VSA and store it into a variable?  or is this something I should run up teh chain as a Feature request?

Verified Answer
  • Justin,

    Consider using the writeFile step.  writeFile will automatically do what you're describing - check fot the presence of the file on an endpoint and then write it to the endpoint only if it does not exist OR if the version on the server is different than the one on the endpoint (using hash).    You can use writeFile from the kserver to any agent OR from a LanCache to any machine assigned to the LanCache.  You could create your own procedure to do this, but if you can configure the script to leverage the kserver or lancache, you might be able to same the extra processing.  The benefit to using the kserver is that the agent should be able to get the file from any network, as long as the agent is connected to the internet.

    If you want to go the route of variables, consider defining the path in Manage Variables - you can define the UNC per machine group and then call the managed variable in any procedure.

All Replies
  • Justin,

    Consider using the writeFile step.  writeFile will automatically do what you're describing - check fot the presence of the file on an endpoint and then write it to the endpoint only if it does not exist OR if the version on the server is different than the one on the endpoint (using hash).    You can use writeFile from the kserver to any agent OR from a LanCache to any machine assigned to the LanCache.  You could create your own procedure to do this, but if you can configure the script to leverage the kserver or lancache, you might be able to same the extra processing.  The benefit to using the kserver is that the agent should be able to get the file from any network, as long as the agent is connected to the internet.

    If you want to go the route of variables, consider defining the path in Manage Variables - you can define the UNC per machine group and then call the managed variable in any procedure.

  • Hi Brande,

    Thank you for explaining that.  I've pretty much always used writeFile, but never knew that it automatically did  those things.  I guess I need to go back and read through the documentation again.  But that pretty much solves my issue, and helps make my APs a bit shorter and cleaner (now I just need to go back and remove the unneeded steps from about 30 different APs...)

    Thanks!

    -=Justin=-

  • Brande - you might know that writeFile() is very slow for all but the simplest smallest files.  See:

    helpdesk.kaseya.com/.../229012128-WriteFile-is-slow-to-copy-files

    community.kaseya.com/.../20493.aspx

    It would be great if there were an easy way to check a file hash that came in over getURL (or even get a file hash to begin with).  Or if getURL could return a failed status if curl dies in the middle of the transfer (because it doesn't).

  • There is:

    Get-FileHash -Path 'C:\kworking\downloadedFile.exe' -Algorithm MD5

  • Just realized, that only works in PowerShell 4 and newer. Probably not the best solution for pushing down to a myriad of machines.

    Principle is the same though. It is easy to find a small command line utility to calculate it and grab the output.

    Here is some C# code to do it. You could put that in a PowerShell script easily enough.

    Here is the C# code to do it if it helps:

    using (var md5Obj = MD5.Create())
    {
            using (var stream = File.OpenRead(spiFile))
                    md5 = md5Obj.ComputeHash(stream).ToHexString();
    }


    The ToHexString() extension method is (the light bulbs are supposed to be [ i ] without spaces:

    public static String ToHexString(this byte[] bytes, bool ToUpper = false)
    {
            //Code copied from: http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa/14333437#14333437
            char[] c = new char[bytes.Length * 2];
            int b;

            if (ToUpper)
            {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                            b = bytesIdea >> 4;
                            c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
                            b = bytesIdea & 0xF;
                            c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
                    }
            }
            else
            {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                            b = bytesIdea >> 4;
                            c[i * 2] = (char)(87 + b + (((b - 10) >> 31) & -39));
                            b = bytesIdea & 0xF;
                            c[i * 2 + 1] = (char)(87 + b + (((b - 10) >> 31) & -39));
                    }
            }

            return new string(c);
    }



    added notes about the light bulbs
    [edited by: jondle at 10:12 AM (GMT -7) on Apr 11, 2017]