LennyGray
asked on
Can someone suggest a command line utility to retrieve Windows file ownership?
The DIR command can return file ownership when one uses the /q argument. But it seems to truncate long user names.
While "dir /q myfile.txt" successfully returns the owner with a short name:
10/13/2011 01:38 PM 355,398 CITNET\Ritu Mishra myfile.txt
It does not work for a name like Lenny Van Duyvendijk
10/05/2011 12:59 PM 162 CITNET\Lenny Van DuyvenCONTROL_TOTALS_BANK_ 20110930.T XT
I have not been able to find any combination of DIR command switches to successfully return such a long (and multi-part) last name.
I am looking for a command line/non-GUI utility, since this information will be pulled into another program (Microsoft SQL Server actually).
If there is any way to do this with basic Windows commands that would be preferred as this will be on a production server. We have a lot of controls over the software that we can install in our production environment.
This would be running on Windows Server 2003 at the moment. Although we will be converting to Windows Server 2008 R2 soon.
While "dir /q myfile.txt" successfully returns the owner with a short name:
10/13/2011 01:38 PM 355,398 CITNET\Ritu Mishra myfile.txt
It does not work for a name like Lenny Van Duyvendijk
10/05/2011 12:59 PM 162 CITNET\Lenny Van DuyvenCONTROL_TOTALS_BANK_
I have not been able to find any combination of DIR command switches to successfully return such a long (and multi-part) last name.
I am looking for a command line/non-GUI utility, since this information will be pulled into another program (Microsoft SQL Server actually).
If there is any way to do this with basic Windows commands that would be preferred as this will be on a production server. We have a lot of controls over the software that we can install in our production environment.
This would be running on Windows Server 2003 at the moment. Although we will be converting to Windows Server 2008 R2 soon.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can also use vbscript to do just that.
'Retrieve owner of a file
Dim strFilename
Set args = WScript.Arguments
Set objWMIService = GetObject("winmgmts:")
strFilename = args.Item(0)
Set objFileSecuritySettings = objWMIService.Get("Win32_L ogicalFile SecuritySe tting=" & strFilename & """)
intRetVal = objFileSecuritySettings.Ge tSecurityD escriptor( objSD)
If intRetVal = 0 Then
WScript.Echo "File: " & strFilename
WScript.Echo "Owner: " & objSD.Owner.Domain & "\" & objSD.Owner.Name
Else
WScript.Echo "Error retrieving security descriptor for file: " & strFilename"
End if
Usage: cscript Owner.vbs c:\temp\somefile.exe
Results:
File: c:\temp\somefile.exe
Owner: SomeDomain\SomeOwner
'Retrieve owner of a file
Dim strFilename
Set args = WScript.Arguments
Set objWMIService = GetObject("winmgmts:")
strFilename = args.Item(0)
Set objFileSecuritySettings = objWMIService.Get("Win32_L
intRetVal = objFileSecuritySettings.Ge
If intRetVal = 0 Then
WScript.Echo "File: " & strFilename
WScript.Echo "Owner: " & objSD.Owner.Domain & "\" & objSD.Owner.Name
Else
WScript.Echo "Error retrieving security descriptor for file: " & strFilename"
End if
Usage: cscript Owner.vbs c:\temp\somefile.exe
Results:
File: c:\temp\somefile.exe
Owner: SomeDomain\SomeOwner
ASKER
I can parse output if needed. But it is not clear how one would parse the output of the calcs command and directly get the file owner. So how can I directly determine the file owner from the CACLS output?