none
How to kill an open file session on a server for a specific file programmatically

    Question

  • I am using a powershell script that updates an Excel file every day.

    The script is being executed via a scheduled task on a management workstation built specifically for this purpose.

    This file is going to be modified by users every day who are adding their own data to it.

    The file is located on a network share.

    Right now my plan is for the script to:

    1. Create a copy of the excel file for its own use
    2. Write its changes this copy and save it
    3. Overwrite the original copy of the Excel file with the one the script modified- just doing a "copy -force"

    The users should have the file closed at night when the script runs, but if they accidentally leave it open the script won't be able to overwrite it.

    What would be the cleanest way to force close the excel file on the file server where it is located before the script runs?

    Its easy to identify open file sessions on a server, but is there a way to single out one file and kill any open connections to it via a script?

    I don't want to run any local script on end users computers to achieve this.

    Thanks for any assistance.

    Wednesday, February 20, 2013 6:05 PM

Answers

  • Look in th e repository for a scritp that can close files on a share.  There is no native way to do this from any scripting language.  You can do it with teh API extensio to PowerShell posted in the repository.


    ¯\_(ツ)_/¯

    • Marked as answer by red888 Wednesday, February 20, 2013 7:07 PM
    Wednesday, February 20, 2013 6:20 PM

All replies

  • Look in th e repository for a scritp that can close files on a share.  There is no native way to do this from any scripting language.  You can do it with teh API extensio to PowerShell posted in the repository.


    ¯\_(ツ)_/¯

    • Marked as answer by red888 Wednesday, February 20, 2013 7:07 PM
    Wednesday, February 20, 2013 6:20 PM
  • OPENFILES /parameter [arguments]

    Description:
        Enables an administrator to list or disconnect files and folders
        that have been opened on a system.

    Parameter List:
        /Disconnect      Disconnects one or more open files.

        /Query           Displays files opened locally or from shared
                         folders.

        /Local           Enables / Disables the display of local open files.

        /?               Displays this help message.

    Examples:
        OPENFILES /Disconnect /?
        OPENFILES /Query /?
        OPENFILES /Local /?


    David Candy

    Wednesday, February 20, 2013 7:18 PM
  • Why so fast on the answer button?

    As DavidMCandy pointed out, you can use OpenFiles.

    The following PowerShell would do the job:

    $File = "C:\Share\SomeFile.txt"
    $FileSessionIDs = OpenFiles /Query /NH /FO CSV | % { if ($_.StartsWith("`"")) { $arrFSI = $_.Split(","); if ($arrFSI[3].Trim("`"") -eq $File) { $arrFSI[0].Trim("`"") } } }
    $FileSessionIDs | % { OpenFiles /Disconnect /ID $_ }


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

    Wednesday, February 20, 2013 7:32 PM
  • OPENFILES does not work if you do not have the debug flags set correctly on your system.  Setting this adds a considerable amount of overhead and systems should not be run in production with this flag set.

    See MSDN and OPENFILES documentation.

    Shared fil3es can be closed easily using the share API.  It is the same as selecting the file inMMC and cliocking 'close file'.

    OPENFILES  does not know about shares remotely.  It can only be run locally.

    Here is the warning:

    C:\Program Files\Windows Resource Kits\Tools>openfiles
    INFO: The system global flag 'maintain objects list' needs to be enabled to see local opened files. See Openfiles /? for more information.

    Here is the help:

    OPENFILES /parameter [arguments]
    Description:
        Enables an administrator to list or disconnect files and folders
        that have been opened on a system.
    Parameter List:
        /Disconnect      Disconnects one or more open files.
        /Query           Displays files opened locally or from shared folders.
        /Local           Enables / Disables the display of local open files.
                         Note: Enabling this flag adds performance overhead.

    Notice that it cannot be used until the flag is set which requires a reboot.  Note the warning about performance hits.  This cause the handle of every open file on a system to be tracked.  This should only be used for debugging.


    ¯\_(ツ)_/¯

    Wednesday, February 20, 2013 7:42 PM
  • This: http://gallery.technet.microsoft.com/scriptcenter/Enumerate-OPen-Files-on-feb939f7

    ...returns file objects remotely that can be closed.  The API code has all of the needed components and it does not require setting any debug flags.

    This exists because it is the correct way to manage files on a share.  It is very lightweight.  It only lacks a simple s3earch routine to pick out  asingle file so you need to enumerate all retruned files to dffind the one you want to close.


    ¯\_(ツ)_/¯

    Wednesday, February 20, 2013 7:47 PM