TUTOS.EU

Lister les fichiers ouverts et par qui

Sur un serveur, liste les fichiers ouverts depuis d'autres machines et via quel compte

Call DetectExeType()

'Inspiré de http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0216.mspx

Dim ComputerName 'Nom du poste

Dim objFSO 'Objet FSO pour l'accès au système de fichiers
Dim objTextFile 'Représente le fichier texte qui contient les réponses
Dim MaLigne
Dim WSHShell

'Déclaration des constantes pour la lecture et l'ecriture dans les fichiers
Const ForReading = 1
Const ForWritting = 2
Const ForAppending = 8

'Création des objets
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set WSHShell = CreateObject("WScript.Shell")
vCOMPUTERNAME = WSHShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set WSHShell = Nothing

ComputerName = InputBox("Entrez le nom du Pc","Nom du poste",vCOMPUTERNAME)
If (Len(ComputerName) > 0) Then

	CheminScriptActuel = Left(wscript.scriptfullname,Len(wscript.scriptfullname)-Len(wscript.scriptname)-1)
	CheminFichierResultat = CheminScriptActuel & "\" & "Fichiers ouverts sur " & ComputerName & ".txt"
	CheminFichierResultat = InputBox("Entrez le chemin du fichier contenant le resultat","Chemin du fichier de reponse",CheminFichierResultat)

	Set objTextFile = objFSO.OpenTextFile(CheminFichierResultat, ForWritting, True)

	Set objConnection = GetObject("WinNT://" & ComputerName & "/LanmanServer")
	Set colResources = objConnection.Resources

	For Each objResource in colResources
		MaLigne = objResource.Path & VbTab & objResource.User
		objTextFile.WriteLine(MaLigne)
	Next
	
	Set colResources = Nothing
	Set objConnection = Nothing

	objTextFile.Close
	Set objTextFile = Nothing

End If

Set objFSO = Nothing

Wscript.Echo "Done."

Sub DetectExeType()
	'Version du 10 juillet 2008

	Dim ScriptHost
	Dim ShellObject

	Dim CurrentPathExt
	Dim EnvObject

	Dim RegCScript
	Dim RegPopupType ' This is used to set the pop-up box flags.
											' I couldn't find the pre-defined names
	RegPopupType = 32 + 4

	On Error Resume Next

	ScriptHost = WScript.FullName
	ScriptHost = Right(ScriptHost, Len(ScriptHost) - InStrRev(ScriptHost, "\"))

	If (UCase(ScriptHost) = "WSCRIPT.EXE") Then
		WScript.Echo ("This script does not work with WScript.")

		' Create a pop-up box and ask if they want to register cscript as the default host.
		Set ShellObject = WScript.CreateObject("WScript.Shell")
		' -1 is the time to wait.  0 means wait forever.
		RegCScript = ShellObject.PopUp("Would you like to register CScript as your default host for VBscript?", 0, "Register CScript", RegPopupType)
		                                                    
		If (Err.Number <> 0) Then
			ReportError ()
			WScript.Echo "To run this script using CScript, type: ""CScript.exe " & WScript.ScriptName & """"
			WScript.Quit (GENERAL_FAILURE)
			WScript.Quit (Err.Number)
		End If

		' Check to see if the user pressed yes or no.  Yes is 6, no is 7
		If (RegCScript = 6) Then
			ShellObject.RegWrite "HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
			ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
			' Check if PathExt already existed
			CurrentPathExt = ShellObject.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT")
			If Err.Number = &H80070002 Then
				Err.Clear
				Set EnvObject = ShellObject.Environment("PROCESS")
				CurrentPathExt = EnvObject.Item("PATHEXT")
			End If

			ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT", CurrentPathExt & ";.VBS", "REG_SZ"

			If (Err.Number <> 0) Then
				ReportError ()
				WScript.Echo "Error Trying to write the registry settings!"
				WScript.Quit (Err.Number)
			Else
				WScript.Echo "Successfully registered CScript"
			End If
		Else
			WScript.Echo "To run this script type: ""CScript.Exe adsutil.vbs <cmd> <params>"""
		End If

		Dim ProcString
		Dim ArgIndex
		Dim ArgObj
		Dim Result

		ProcString = "Cscript //nologo " & WScript.ScriptFullName

		Set ArgObj = WScript.Arguments

		For ArgIndex = 0 To ArgCount - 1
				ProcString = ProcString & " " & Args(ArgIndex)
		Next

		'Now, run the original executable under CScript.exe
		Result = ShellObject.Run(ProcString, 0, True)

		WScript.Quit (Result)
	End If

End Sub
Lien vers le fichier : cliquez ici Copier le code

J'ai tenté un équivalent en powershell sans vraiment avoir quelque-chose d'aussi bien :

$colResources = openfiles /Query /S NomServeurDeFichiers
foreach ($UneLigne in $colResources){
	if ($UneLigne -like "*motrecherche*"){
		Write-Host $UneLigne
	}
}
Lien vers le fichier : cliquez ici Copier le code

2