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

supportregistrationsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi there.  I have modified the section that asks you for the file ID, and made it just go through all of the IDs that match the strFileToClose string, and close them.

Also note that if you want the command prompt to close automatically after the script finishes, just change
    strCommand = "%comspec% /k cscript  """ & strPath & """"
to this
    strCommand = "%comspec% /c cscript  """ & strPath & """"

Regards,

Rob.
'====================
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 & "=============================="
strIDs = ""
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
			If strIDs = "" Then
				strIDs = objFile.Name
			Else
				strIDs = strIDs & ";" & objFile.Name
			End If
			intFound = intFound + 1
		End If
	Else
		Err.Clear
		On Error GoTo 0
	End If
Next 
WScript.Echo ""
If intFound > 0 Then
	arrIDs = Split(strIDs, ";")
	For Each strFileID In arrIDs
		strCommand = strPSExecPath & " -accepteula -e \\" & strServerName & " cmd /c Net Files " & strFileID & " /close"
		objShell.Run strCommand, 0, True
		WScript.Echo strFileID & " has been closed."
	Next
	'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
	'	WScript.Echo strFileID & " has been closed."
	'End If
Else
	WScript.Echo "No matching open files were found."
End If
'====================

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
supportregistrationsAuthor Commented:
Thank you very much RobSampson. Your a legend!!
0
RobSampsonCommented:
No problem. Thanks for the grade.

Regards,

Rob.
0
RLUNTCommented:
Kudos,

Great script and modification.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server OS

From novice to tech pro — start learning today.