Jump to content

Prevent script from crashing in case file owner cannot be determined


Recommended Posts

Hello,

this function returns the owner name of file objects, and in case there is some issue in accessing the file, it's crashing the whole script.

How to modify the script, so that it will not crash in case $oWMIService.Get() is failing? I mean to "catch" in case this should fail and to skip the file?

 

$File="M:\file03-data03\DATEN\public\TRANSFER\BilderVerkleinern\20221213_095141.jpg"

$Owner=GetOwner($File)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Owner = ' & $Owner & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console



Func GetOwner($FsObject)
    Local $objSD
    $oWMIService = ObjGet("winmgmts:")
    
    ; the next line is the one that crashed the script:
    $oFolderSecuritySettings = $oWMIService.Get("Win32_LogicalFileSecuritySetting='" & $FsObject & "'")
    $intRetVal = $oFolderSecuritySettings.GetSecurityDescriptor($objSD)
    If $intRetVal = 0 Then
        Return $objSD.Owner.Name
    Else
        Return "Cant-get-user-name"
    EndIf
EndFunc   ;==>GetOwner

 

I'm basically not really sure, what happend to these three image files to end up in this behavior:

 

  • As local Administrator of that file server I was unable to see the SID settings / Ownership of that file
  • "Take ownership" didn't work using Windows Explorer --> file properties --> tab Security (no permissions)
  • Same when using Helge Klein's SETACL.EXE, even when runned as "SYSTEM" (psexec -sid cmd)

 

Finally it turned out, that there was hanging around an opened SMB Network File Handle blocking the file that funny way. Forcibly closing these network file handles solved the issue.

 

One approach I could think of would be to try if I can open the file first, but to catch the failure of $oWMIService.Get() propably is the safe way?

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to post
Share on other sites

you need a COM error handler, see for example UEZ's example here or look in Helpfile under "ObjEvent" or "COM reference".

Link to post
Share on other sites

@RTFC thanks for pointing out the com error handler example https://www.autoitscript.com/forum/topic/135809-help-with-wmi-and-objects/?do=findComment&comment=948835 by @UEZ

 

As I currently do not have such a problem file at hand I cannot really test how to use it.

 

I guess, that this part has to to to the beginning of my script ,

Global $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")

... and the rest will need several adjustments around the postition in my script, where I expect an COM error could be thrown?

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...