question

MuhammadKhalil-6943 avatar image
0 Votes"
MuhammadKhalil-6943 asked RichMatheisen-8856 answered

Why Get-SmbOpenFile losses the lock when file still opened at remote machine?

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 or can rename the file.

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.

Even immediately after opening on remote machine from shared networked path, switched to file server and executed Get-SmbOpenFile it stops outputting anything and returns nothing.

It should in ideal case must return the information of opened file if it is opened otherwise nothing. But that's not the case as I explained earlier.

So, I have two questions :-)

  **1. So, what is the right way to search the file ? ( above Fully qualified path doesnt returns the search result )     
  2. Why the lock gets lost after couple of seconds later**








windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

In your first example you're using the '-match' operator. The regular expression you're using is actually looking for the string "apvdp100ProfileTestP1088100.txt" (where the "." represents "any character", not the literal value "."). I doubt that's what you intend. Because you intend to match the literal value "\" and ".", change that regular expression to: "\\apvdp100\\Profile\\TestP1088\\100.txt"

The "\" in a regular expression is an escape character. You need to escape the escape!
The "." in a regular expression means "match any single character".

If you intent to match the entire $_.Path value, you should be intentional and add "anchor" characters at both ends of your regular expression to avoid partial matches: "^\\apvdp100\\Profile\\TestP1088\\100.txt$"

If you're going to post your question in multiple forums, please make note of that by linking it in both places. Here's your question from ServerFault: why-get-smbopenfile-losses-the-lock-when-file-still-opened-at-remote


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Have you tried using Get-SMBSession and checking the "NumOpens" property of all sessions to see if there are open files for any sessions?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.