简体   繁体   中英

vbscript filesystemobject permission denied

I'm having a problem with Trend OfficeScan Patterns filling up the C:\\ drive (no other drives available to change directories) and I'm getting a permission denied error accessing "C:\\Program Files\\Trend Micro\\OfficeScan\\PCCSRV\\WSS\\patterns" running the below script. As I'll be using this script for a few sites, and to make it easy to implement for my colleagues, I don't want to play around adding various permissions.

I tried changing: PatternLocation = (strValue & "WSS\\patterns\\") to PatternLocation = ("""" & strValue & "WSS\\patterns\\""") and I get 'Path not found'. Are there any VBScript experts that may be able to recommend an impersonate method to overcome the permissions denied?

' Variable to locate HLM.
const HKEY_LOCAL_MACHINE = &H80000002
Set fso = CreateObject("Scripting.FileSystemObject") 

' Checks if the operating system is x86 or x64
Set objShell = CreateObject("WScript.Shell")
osType = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")

' The dot refers to the computer this vbscript has been run on.
strComputer = "."

' Provides connection to the registry.
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

' Checks the bit for the operating system
If osType = "x86" Then
    ' Checks registry for Trend folder path.
    strKeyPath = "SOFTWARE\TrendMicro\OfficeScan\Service\Information"
Elseif osType = "AMD64" Then
    strKeyPath = "SOFTWARE\Wow6432Node\TrendMicro\OfficeScan\service\Information"
End if

trValueName = "Local_Path"
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

' If the registry path is empty it won't install the scheduled task and alert you.
If IsNull(strValue) Then
    msgbox("Trend Micro is not installed.")
else
    PatternLocation = (strValue  &  "WSS\patterns\") ' folder to start deleting (subfolders will also be cleaned) 
    OlderThanDate = DateAdd("d", -2, Date)  ''# 2 days (adjust as necessary)
    DeleteOldFiles PatternLocation, OlderThanDate 
end if

Function DeleteOldFiles(folderName, BeforeDate) 
    Dim folder, file, fileCollection, folderCollection, subFolder 

    Set folder = fso.GetFolder(folderName) 
    Set fileCollection = folder.Files 
    For Each file In fileCollection 
        If file.DateLastModified < BeforeDate Then 
            fso.DeleteFile(file.Path) 
            End If 
    Next 

    Set folderCollection = folder.SubFolders 
    For Each subFolder In folderCollection 
        DeleteOldFiles subFolder.Path, BeforeDate 
    Next 
End Function

This is the working script with a few changes for anyone who might find it useful:

    'Variable to locate HLM.
const HKEY_LOCAL_MACHINE = &H80000002
Set fso = CreateObject("Scripting.FileSystemObject") 

'Checks if the operating system is x86 or x64
Set objShell = CreateObject("WScript.Shell")
osType = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")

'The dot refers to the computer this vbscript has been run on.
strComputer = "."

'Provides connection to the registry.
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

 'Checks the bit for the operating system
If osType = "x86" Then
                'Checks registry for Trend folder path.
                strKeyPath = "SOFTWARE\TrendMicro\OfficeScan\Service\Information"
Elseif osType = "AMD64" Then
                strKeyPath = "SOFTWARE\Wow6432Node\TrendMicro\OfficeScan\service\Information"
End if
                strValueName = "Local_Path"
                objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue



'If the registry path is empty it won't install the scheduled task and alert you.
If IsNull(strValue) Then
                msgbox("Trend Micro is not installed.")

else

                PatternLocation = (strValue  &  "WSS\patterns") ' folder to start deleting (subfolders will also be cleaned) 
                'msgbox(PatternLocation)        
end if


startFolder = PatternLocation
OlderThanDate = DateAdd("d", -1, Date) ' 1 days  
DeleteOldFiles startFolder, OlderThanDate  
DeleteEmptyFolders startFolder

Function DeleteOldFiles(folderName, BeforeDate)  
Dim folder, file, fileCollection, folderCollection, subFolder  
Set folder = fso.GetFolder(folderName)  
Set fileCollection = folder.Files  
For Each file In fileCollection     
If file.DateLastModified < BeforeDate Then          
fso.DeleteFile(file.Path)   
End If  
Next  
Set folderCollection = folder.SubFolders  
For Each subFolder In folderCollection      
DeleteOldFiles subFolder.Path, BeforeDate  
Next  
End Function   
Function DeleteEmptyFolders(foldername)  
For Each Folder In fso.GetFolder(foldername).SubFolders     
DeleteEmptyFolders(Folder.Path)     
If Folder.Files.Count = 0 and Folder.SubFolders.Count = 0 Then          
fso.DeleteFolder(Folder.Path)   
End If  
Next  
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM