Link to home
Start Free TrialLog in
Avatar of gbohrman
gbohrmanFlag for United States of America

asked on

Powershell get-smbopenfile at remote server

I'm trying to use get-smbopenfile to look at open files on a remote server then be able to close them. Example DATASERVER is running Server 2008 so I can't run get-smbopenfile on that.  I want to run it on another server called server1 that it can run on.
I want to see what files are open at location \\dataserver\shared\work files.
Then I want to close all files at that location every night around 7:00pm.
Avatar of Robert
Robert
Flag of United States of America image

If it is 2008 R2 you could just updated powershell.
How to Install Windows PowerShell 4.0 - TechNet Articles - United States (English) - TechNet Wiki (microsoft.com) 
or you would need to do something like this to run that command remotely.
(i just used e: as I didn't know the local path based on your post) (requires firewall to allow connection and WinRM service to be running) 
$Cim = New-CimSession ServerName -Credential (get-credential) Get-SmbOpenFile -CimSession $Cim | where-object { $_.Path -like 'E:\shared\work file\*' }

Open in new window

Avatar of gbohrman

ASKER

I get the following error.
New-CimSession : A positional parameter cannot be found that accepts argument 'Get-SmbOpenFile'.

My 2008 R2 server has powershell 5.1.  I have tried it from there and have been told by multiple people that get-smbopenfile will not run on a 2008 server. Regardless of the powershell version.  I'm not sure if thats what causing the above error trying to run it on the remote server that is 2008.


try the following (think the new line was the actual issue)
$Cim = New-CimSession ServerName -Credential (get-credential) 
Get-SmbOpenFile -CimSession $Cim | where-object { $_.Path -like 'E:\shared\work file\*' }

Open in new window

Looks like its getting closer. Now i get the following

Get-SmbOpenFile : The WS-Management service cannot process the request. The CIM namespace ROOT/Microsoft/Windows/SMB
is invalid.
At line:1 char:1
+ Get-SmbOpenFile -CimSession $Cim | where-object { $_.Path -like 'd:\s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MSFT_SMBOpenFile:ROOT/Microsoft/...SFT_SMBOpenFile) [Get-SmbOpenFile],
   CimException
    + FullyQualifiedErrorId : HRESULT 0x80338000,Get-SmbOpenFile
    + PSComputerName        :datasrv
Based on the error looks like it just doesn't have the namespace.
Not sure how to add that on 2008. I tested the command against a 2012 server but didn't have a 2008 to test against.

I did some searching and found this article which uses the net file and net session to do similar.
windows server 2008 r2 - Monitor shared file access using PowerShell - Stack Overflow 

Thanks for all your help on this.  OK so i moved the files to a Windows 2019 server.  Your get-smbopnefile commands work now. Thank you. My next step is to get them to close.  I add Close-SmbOpenFile -Force  to the end of the line. It seems to work because then i run the get-smbopenfile the file no longer shows as open. But when i go to my workstation, the file is still open.  Any thoughts?

ASKER CERTIFIED SOLUTION
Avatar of Robert
Robert
Flag of United States of America image

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