Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Ownership of file using WMI

Status
Not open for further replies.

mevasquez

Programmer
Aug 26, 2003
75
US
Having problems with the following script. When the variable fName is a path such as, c:\Folder\subFolder, p:\folder\subfolder, the script works. When the variable fName is a path such as \\domainFileServer\folder\Subfolder, I get an automation error. Any suggestions

Code:
Function getOwner(fName)

    Dim strComputer
    Dim objWMIService
    Dim colItems
    Dim objItem
    
    strComputer = "."
             
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & fName & "'}" _
            & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
    
    For Each objItem In colItems
        getOwner = getDisplayName(objItem.AccountName)
    Next
End Function

Public Function getDisplayName(ByVal vSAN)
    
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
    ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));DisplayName;subtree"
    Set oRecordSet = oCommand.Execute

    getDisplayName = oRecordSet.Fields("DisplayName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function
 
The wmi service object should bind you to the remote (file) server. From there you can specify a (local) fully-qualified path.
[tt]
Function getOwner(domainFileServer, fName)
[green]'here fName is the (local, unescaped) fully-qualified path to file or directory[/green]

'etc etc

strComputer = domainFileServer

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & fName & "'}" _
& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
'etc etc
[/tt]
 
I still get the error, automation error, at
...
Code:
strComputer = "DomainComputerName"

GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
...
 
Associator query is not normally one starts with wmi. What simple wmi script you can do on the domainfileserver without automation error? What error number? Do you have enough credential to bind to it? Are you logged on to the domain?...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top