DougInCanada
Technical User
does anyone know of WMI object.Item or API call to find the owner info of any documents/folders in a given folder using the 'Owner' attribute, much like the details view in Windows?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
'outputs the owner name of a file passed as a command-line
'parameter, or gives an error if it cannot
Option Explicit
On Error Resume Next
Dim refWMI
Dim strQuery
Dim colOwner
Dim refItem
'check command-line argument seems sane
If WScript.Arguments.Count <> 1 Then
WScript.Echo"Usage: checkowner.vbs <path>"
WScript.Quit
End If
'connect to WMI or quit with error
set refWMI = GetObject("winMgmts:")
If Err <> 0 Then
WScript.Echo "Oh dear. Could not connect to WMI. Error: " & Err.Description
WScript.Quit
End If
'get reference
strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
"'" & WScript.arguments.item(0) & "'} WHERE " & _
"AssocClass=Win32_LogicalFileOwner " & _
"ResultRole=Owner"
set colOwner = refWMI.ExecQuery(strQuery)
'finally, read the owner info from Win32_SID
For Each refItem In colOwner
If Err = 0 Then
WScript.Echo WScript.arguments.item(0) & " is owned by " & refItem.AccountName
Else
WScript.Echo "Cannot find owner for " & WScript.arguments.item(0)
End If
Next
Set colOwner = Nothing
set refWMI = Nothing
Option Explicit
'On Error Resume Next
Dim refWMI
Dim strQuery
Dim colOwner
Dim refItem
Dim FSO
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists ("C:\FileOwner.txt") then
FSO.DeleteFile "C:\FileOwner.txt"
End If
Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)
Do Until txtFileName.atendofstream=true
set refWMI = GetObject("winMgmts:")
filespec=txtFileName.readline
strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
"'" & filespec & "'} WHERE " & _
"AssocClass=Win32_LogicalFileOwner " & _
"ResultRole=Owner"
set colOwner = refWMI.ExecQuery(strQuery)
For Each refItem In colOwner
If Err = 0 Then
txtFileOwner.writeline filespec & " is owned by " & refItem.AccountName
End If
Next
Set colOwner = Nothing
set refWMI = Nothing
loop
Option Explicit
'On Error Resume Next
Dim FSO
Dim WSH
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Dim refSetting
Dim refSD
Dim refOwner
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")
If FSO.FileExists ("C:\FileOwner.txt") then
FSO.DeleteFile "C:\FileOwner.txt"
End If
Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)
Do Until txtFileName.atendofstream=true
Set refSetting = GetObject("winMgmts:Win32_LogicalFileSecuritySetting='" & filespec & "'")
If refSetting.GetSecurityDescriptor(refSD) = 0 Then
'operation was successful
Set refOwner = refSD.Owner
txtFileOwner.writeline refSetting.Path & " is owned by " & refOwner.Name
Set refOwner = Nothing
Set refSD = Nothing
End If
Set refSetting = Nothing
loop
Option Explicit
'On Error Resume Next
Dim FSO
Dim WSH
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Dim refSetting
Dim refSD
Dim refOwner
DIM SERVER
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")
If FSO.FileExists ("C:\FileOwner.txt") then
FSO.DeleteFile "C:\FileOwner.txt"
End If
Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)
Do Until txtFileName.atendofstream=true
Set SERVER = FileServ1
Set refSetting = getobject("winmgmts:\\"& SERVER &"\root\cimv2 Win32_LogicalFileSecuritySetting='" & filespec & "'")
If refSetting.GetSecurityDescriptor(refSD) = 0 Then
'operation was successful
Set refOwner = refSD.Owner
txtFileOwner.writeline refSetting.Path & " is owned by " & refOwner.Name
Set refOwner = Nothing
Set refSD = Nothing
End If
Set refSetting = Nothing
loop
Do While Not txtFileName.atendofstream=true
Option Explicit
'On Error Resume Next
Dim FSO
'Dim WSHSHELL
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
'Dim refSetting
'Dim refSD
'Dim refOwner
Dim SERVER
Dim refWMI
Dim strQuery
Dim refItem
Dim colOwner
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set WSHSHELL = CreateObject("WScript.Shell")
If FSO.FileExists ("C:\FileOwner.txt") then
FSO.DeleteFile "C:\FileOwner.txt"
End If
Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt", true)
txtFileOwner.close
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)
SERVER = "FileServ1"
Do Until txtFileName.atEndOfStream=true
FileSpec = txtFileName.readline
Set refWMI = GetObject("winmgmts:\\"& SERVER &"\root\cimv2")
strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
"'" & filespec & "'} WHERE " & _
"AssocClass=Win32_LogicalFileOwner " & _
"ResultRole=Owner"
Set colOwner = refWMI.ExecQuery(strQuery)
On Error Resume Next
For Each refItem In colOwner
If Err = 0 Then
'WScript.Echo FileSpec & " is owned by " & refItem.AccountName
txtFileOwner.writeline FileSpec & " is owned by " & refItem.AccountName
Err.clear
Else
'WScript.Echo "Cannot find owner for " & FileSpec
txtFileOwner.writeline "Cannot find owner for " & FileSpec
End If
Next
loop
txtFileOwner.close
txtFileName.close
Set txtFileOwner = Nothing
Set txtFileName = Nothing
Set ColOwner = Nothing
Set refWMI = Nothing