Link to home
Start Free TrialLog in
Avatar of venmarces
venmarcesFlag for Canada

asked on

Windows Server 2008 Command DOS or PowerShell commands to know who is opened a Network File

Hi,

If I will provide you with a Network File's path then I would like to know through a DOS Command or a PowerShell Command who is opened the actual file (Username, DateTime)

I already tried the OpenFiles.exe / Query command ... But I wonder if there is any other command that provide the same result

Also, I am open for any .Net program or any other type of script that I can execute on my server locally to provide the info

Thanks
SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
arana, the OP already stated they used that.

venmarces, isn't this almost the same as in https://www.experts-exchange.com/questions/28956718/Network-Opened-Files-Script-or-Tool-without-Server-access.html ?
What is the issue with openfiles? Besides that you (probably) need admin credentials for it to work?
Avatar of Arana (G.P.)
Arana (G.P.)

QELMO the OP mentioned he needs to provide the path and get the result instead of the regular result of openfiles that gives you EVERY open file, so no he didnt mention he already tried with the find filter.
Well, I thought OpenFile supports a file mask, but it does not (only for closing files with that mask). So yes, your answer is valid if filtering for the file name is the issue.
Avatar of venmarces

ASKER

Qlemo, Yes this question is similar to the one that we exchanged during the last 24 hours.
Now, I want to build a solution for my users that will just interogate a web service on my Server to check if the file is locked or not and by who.

In this case I can manage better security issues ... So for this reason I asked a new question that it is clear different from the old one. and Asking for the OpenFiles.exe / query options that instead of listing the huge report of all opened files then I can check only for one file ....

arana : what is the output of your command supposed to be ? I have no output
The issued command should show whether YOURPATHHERE is opened currently.
then what is the output value ... is it Boolean ? How I can catch it ?
As-is the command results in nothing or the complete line containing the file name (and more). However, without /v it will not show the complete path.

Combining openfiles and PowerShell, you could use:
$file = Read-Host 'Which filename to look for? Wildcard "*" is allowd.'
openfiles /query /s server /v /fo csv /nh |
  convertFrom-CSV -header Server, ID, User, Type, Locks, Mode, Name |
  ? { $_.Name -like "*$file" } |
  format-table -auto Name, User, Type, Locks

Open in new window

Certainly not the fastest way, but it does not require to use PowerShell Remoting or remote execution.
this is the output from the example I gave you (with /v parameter ):
C:\Test >openfiles /query /v|find "TRESS.exe"

SAURON          24159199 agarcia              Windows    0          Read
    C:\Program Files (x86)\Grupo Tress\Programas\TRESS.exe

C:\Test >

If more than one user is opening the file, the result would be more than one line , each with the proper username
arana

I tried your command line and still don't see anything happened as you mentioned ... maybe I am doing something wrong ...  

see attached pic
User generated imageAlso, it is taking long time before executed
Are you certain the file is open?

Regarding the running time, the program has to get all open files, and that might last some time. Only after it has finished its work, the find can process the output. So there might be collected a lot of information you do not need, and then it is dismissed again. This takes time (and memory).
yes the file was open and I got nothing as an output

I would like to ask again regarding your last script where you combine both DOS and Powershell

Since I am not really familiar with this .. how I can run it ? Can I encapsulate it inot a batch file ?

Show me an example on how I can use this script

thanks
For getting a feeling, just paste it as is into a powershell prompt. The script asks for a file name to look for.
If it would work that way for you, you can store the script into a .ps1 file. Since the default action for PS1 files is to open them in an editor (for security reasons they are not executed by default), you'll need a batch or shortcut using
powershell -NoExit -ExecutionPolicy Unrestricted -File '\\server\share\scripts\Get-OpenFile.ps1'

Open in new window

(with the correct path and script name, of course) to execute. The PowerShell prompt will stay open - otherwise you won't see the result. Type exit to  get out of it, or close the window.
Of course we can add a Read-Host 'Press Return to continue' | Out-Null to the script (and remove -NoExit when calling it), if you want to have the PowerShell prompt closed after execution.
Please try only ".txt" and see if you get any results, maybe its a long path I just want to be sure you have some output at all, still I find powershell solution more robust, I still would like to find out why openfiles isnt working for you.
Sorry for this delay ... I think we are so close to have something workable for us.

here is a screenshot for what I can have on my server

User generated image
I would like to ask the following :  
- I want to put this command inside a program ... so no need to user interaction ... is there a way to take off the Wildcard question and allow displaying the whole list of files ?

- Sounds like when I tried to display only DOC files then I cant see the name of the resource who opened the file neither the rest of info I want to see .... thoughts ?

- Anyway to export this list in a CSV file or TXT file .... how we can tweak the code for this ?

thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all for this help