Link to home
Start Free TrialLog in
Avatar of supportregistrations
supportregistrationsFlag for New Zealand

asked on

VBScript to close open file session using PSEXEC

Found this script within EE and this will help assist with closing Open Files on our Remote File Servers.
I do apologise in advance but VBScript is very limited.
Please advise how to stop this script from prompt for Input but just close this file straight away.
Thanks
'====================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
intFound = 0
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strDomain = objNetwork.UserDomain
strServerName = "FILESERVER"
strFileToClose = "File_To_Force_Closed.doc"
strPSExecPath = "\\server\share\psexec.exe"
 
Set objConnection = GetObject("WinNT://" & strDomain & "/" & strServerName & "/LanmanServer")
Set objOpenFiles = objConnection.Resources
 
WScript.Echo "Files open on " & strServerName & VbCrLf & "=============================="
For Each objFile In objOpenFiles
	On Error Resume Next
	temp = objFile.User
	If Err.Number = 0 Then
		On Error GoTo 0
		If InStr(LCase(objFile.Path), LCase(strFileToClose)) > 0 Then
			WScript.Echo objFile.Name & " || " & objFile.Path & " || " & objFile.User
			intFound = intFound + 1
		End If
	Else
		Err.Clear
		On Error GoTo 0
	End If
Next 
WScript.Echo ""
If intFound > 0 Then
	WScript.Echo "Which ID do you want to close? "
	strFileID = WScript.StdIn.ReadLine
	If Trim(strFileID) <> "" Then
		strCommand = strPSExecPath & " -accepteula -e \\" & strServerName & " cmd /c Net Files " & strFileID & " /close"
		'InputBox "About to run:", "Command to Run", strCommand
		objShell.Run strCommand, 0, True
	End If
Else
	WScript.Echo "No matching open files were found."
End If
'====================

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Avatar of supportregistrations

ASKER

Thank you very much RobSampson. Your a legend!!
No problem. Thanks for the grade.

Regards,

Rob.
Avatar of RLUNT
RLUNT

Kudos,

Great script and modification.

Thanks