1

I am using Get-SmbOpenFile, But surprisingly, it looses its connection and I can see that there is no lock even after couple of seconds later on the file any more, even it is still opened. I safely can remove that file on my server.

Why, this can be? I have to check opened .VHDX disk file connected with user and need to close them if they are still opened.

Get-SmbOpenFile | Where-Object { $._Path -match '\\apvdp100\Profile\TestP1088\100.txt' } returns nothing even the file is opened remotely.

Get-SmbOpenFile | Where-Object { $._Path -match '100.txt' } returns the object if i open the file remotely and check within a second or so and stops outputting after few tries. Sometimes, even immediately issueing `Get-SmbOpenFile` doesn't outputs anything at all.

So, I have two questions :-)

So, what is the right way to search the file ? ( above Fully qualified path doesnt returns the search result )

Why the lock gets lost after couple of seconds later

Usman
  • 117
  • 6
  • Why would there be an expectation that the file would remain open? What is opening the files? – Greg Askew Sep 26 '20 at 12:22
  • What do you mean expectation? I opened the file on remote client machine and that file still remained as opened and didn't close. I switched back on my server machine and issued Get-SmbOpenFile which returned most of time for couple of tries ( 3 seconds ) the info that file is opened and suddenly it starts outputting nothing. – Usman Sep 26 '20 at 12:33
  • Of course, that file is physically opened on remote client machine and i edited the file there and switched to server to expect that commandlet must returns valid result all the time as long as file is opened by client machine – Usman Sep 26 '20 at 12:35
  • What application is used for editing? Just because a file is opened doesn't mean that it will maintain an open handle. It depends on the access used and the lock level. Sounds like you aren't opening the VHDX files the same way others may be. If you just need to create a VHDX file that remains open, create a minimal sized dynamically expanding VHDX, copy it to the share, then open it in Excel. Excel is great at locking files. – Greg Askew Sep 26 '20 at 13:53
  • no, actually i was all the time trying to open notepad files. Yes, my target is .vhdx files. But for debugging and playing around with this commandlet and learning about it , I temporary was creating notepad files or .rtf files ( Microsoft Word ). So, do you think experimenting with notepad based files wont work? Does notepad file opening and making keep that remains opened wont be right use case? – Usman Sep 26 '20 at 14:13
  • No, Notepad will not maintain an open handle. – Greg Askew Sep 26 '20 at 14:46

1 Answers1

0

Thanks for valuable comments @Greg Askew. You are right. It must be noted which application in particular locks the file. Notepad and .rtf files are not maintaining the locks and therefore they are not good use cases for testing these scenarios.

I later focused on '.VHDX' based files and mounted them for testing above command let which returned expected results accordingly.

Usman
  • 117
  • 6