Hi Friends, I am new here and to VB script too. I am working on a VB script and getting stuck. I have to write a script wich will display and log the owner and the group to which it belongs, for the each folder when a path is provided to the script. Here is a script I have written ,
strFileName = wscript.arguments(0) Set objWMIService = GetObject("winmgmts:") Set objFileSecuritySettings = _ objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'") intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD) If intRetVal = 0 Then WScript.Echo "Owner: " & objSD.Owner.Domain & "\" & objSD.Owner.Name Else WScript.Echo "Couldn't retrieve security descriptor." End If
Its working fine when run through command line with file path provided. But I want to modify it to accept user input as path(folder location) and then display its owner for every folder residing at that path. So please help me with this
For this you'll need the FSO object and the GetFolders method. That returns a collection which you can then iterate through.
You'll need to put your code that gets the owner into a Sub which you will call and pass it the folder name as
Here's some simple code that demonstrates how to iterate subfolders. This won't do recursion (i.e. going more than one subfolder down). I'll let you figure that one out ;)
Thanks Ginolard, But as I mentioned before, I need to process user input in the VB script. In the solution provided by you, you have mentioned folder path as "C:\temp". But I want to get that value from user. I have tried like this objWMIService.ExecQuery("Select * From Win32_Directory Where Drive = 'C:' and path = But I want to remove the dependency on Drive i.e. I want my script to work on value provided for folder path as "C:\HI" by user. I have tried that but its giving error. Actually I want to run this script on share. So hopefully waiting for your suggestions/guidelines for this problem also.
I have done that already. What I want to know is how can I process that input in a query. Please have a look at the following script. This will make it clear strComputer = "." strFolderPath = InputBox("Enter Folder path:") Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim oOutPut Set oOutPut = objFSO.CreateTextFile("C:\log.txt")
Set objWMIService = GetObject("winmgmts:") '& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFolders = _ objWMIService.ExecQuery("Select * From Win32_Directory Where path = '\\strFolderPath\\'")'Drive = 'C:' and path = '\\HI\\'") WScript.Echo colFolders.count For Each objFolder in colFolders WScript.Echo objFolder.name 'WScript.Echo objFolder.Count 'WScript.Echo objFolder.owner 'Set objWMIService = GetObject("winmgmts:") Set objFileSecuritySetting=_ objWMIService.get("Win32_LogicalFileSecuritySetting='" & objFolder.name &"'") intRetVal = objFileSecuritySetting.GetSecurityDescriptor(objSD) If intRetVal = 0 Then WScript.Echo "Owner: " & objSD.Owner.Domain & "\" & objSD.Owner.Name Else WScript.Echo "Couldn't retrieve security descriptor." End If oOutPut.WriteLine ("Folder Name:")& objFolder.name oOutPut.WriteLine ("Owner:")& objSD.Owner.Domain Next
Now suppose user enters value in Box as "C:\HI", then I want to display as well as log the owner of the folder in the text file. But I am getting error in the query. So I want to know how to process that value in a query so that it would be independent of drive parameter. One more thing. I want to run this script on share also. Like, if m running script on \\myshare\myfolders, and user inputs value as \\myshare\myfolders\ then I want my script to display owner of all the folders residing at previously mentioned location. So I need your help to run my script on both local machine and share.
Then you are going to have to some parsing of the input.
This will work for local paths but you'll need to find out how to parse UNC paths. I'm not sure Win32_Directory can handle UNC paths though I might be wrong.