Not able to get MyDocument size of the Domain Users

Answered Not able to get MyDocument size of the Domain Users

  • Saturday, January 26, 2013 9:31 AM
     
     

    I create a script to get the size of the mydocument folder of those users which are not redirected to file-server. I put that script into logon, when user login into the machine it writes the size of the myDocument folder into the shared text file. But users whose myDocument is not redirected i always get the path of myDocument but size = 0. Any suggestions

    Script :

    '------------------------
    Const dataFolder = "\\Server1\Test\" ' Path to folder where clients will write inventory XML files
    Const ForWriting = 2
    ' Constants for special folders
    Const MY_DOCUMENTS = &H5&
    Const MY_VIDEOS = &He&
    Const MY_PICTURES = &H27&
    Const MY_MUSIC = &Hd&
    Const DESKTOP = &H10&
    '------------------------------
    On Error Resume Next
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Shell.Application")
    Set objNetwork = WScript.CreateObject("WScript.Network")

    ' Quit if data folder does not exist
    If Not objFSO.FolderExists(dataFolder) Then
    WScript.Quit(1)
    End If

    ' Determine computer name and current user account
    strComputer = objNetwork.ComputerName
    strUser = getCurrentUsername()
    strTimeStamp = getTimeStamp(Now())
    strOutputFile = dataFolder & "\" & objNetwork.UserName & "@" & strComputer & ".xml"

    ' My Documents
    Set objDocuments = objShell.Namespace(MY_DOCUMENTS)
    Set objDocumentsItem = objDocuments.Self
    strDocumentsPath = objDocumentsItem.Path
    Set objDocuments = objFSO.GetFolder(strDocumentsPath)

    ' My Pictures
    Set objPictures = objShell.Namespace(MY_PICTURES)
    Set objPicturesItem = objPictures.Self
    strPicturesPath = objPicturesItem.Path
    Set objPictures = objFSO.GetFolder(strPicturesPath)

    ' My Videos
    Set objVideos = objShell.Namespace(MY_VIDEOS)
    Set objVideosItem = objVideos.Self
    strVideosPath = objVideosItem.Path
    Set objVideos = objFSO.GetFolder(strVideosPath)

    ' My Music
    Set objMusic = objShell.Namespace(MY_MUSIC)
    Set objMusicItem = objMusic.Self
    strMusicPath = objMusicItem.Path
    Set objMusic = objFSO.GetFolder(strMusicPath)

    ' Desktop
    Set objDesktop = objShell.Namespace(DESKTOP)
    Set objDesktopItem = objDesktop.Self
    strDesktopPath = objDesktopItem.Path
    Set objDesktop = objFSO.GetFolder(strDesktopPath)

    ' Calculate sizes in KB
    mydocumentsSize = objDocuments.Size / 1024
    mypicturesSize = objPictures.Size / 1024
    myvideosSize = objVideos.Size / 1024
    mymusicSize = objMusic.Size / 1024
    desktopSize = objDesktop.Size / 1024

    ' Output to XML...
    Err.Clear
    ' Create an instance of the DOM
    Set objXML = CreateObject("Microsoft.XMLDOM")
    objXML.async = False
    objXML.insertBefore objXML.createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'"), objXML.firstChild

    ' Create the DataItem element
    Set objDataItem = objXML.createElement("DataItem")
    ' Create all child elements
    objDataItem.appendChild objXML.createElement("Username")
    objDataItem.appendChild objXML.createElement("ComputerName")
    objDataItem.appendChild objXML.createElement("MyDocuments")
    objDataItem.appendChild objXML.createElement("MyPictures")
    objDataItem.appendChild objXML.createElement("MyVideos")
    objDataItem.appendChild objXML.createElement("MyMusic")
    objDataItem.appendChild objXML.createElement("Desktop")
    objDataItem.appendChild objXML.createElement("TimeStamp")
    objDataItem.appendChild objXML.createElement("MyDocumentPath")
    ' Add values to the child elements
    objDataItem.childNodes(0).text = strUser
    objDataItem.childNodes(1).text = strComputer
    objDataItem.childNodes(2).text = Round(mydocumentsSize, 0)
    objDataItem.childNodes(3).text = Round(mypicturesSize, 0)
    objDataItem.childNodes(4).text = Round(myvideosSize, 0)
    objDataItem.childNodes(5).text = Round(mymusicSize, 0)
    objDataItem.childNodes(6).text = Round(desktopSize, 0)
    objDataItem.childNodes(7).text = strTimeStamp
    objDataItem.childNodes(8).text = strDocumentsPath
    ' Add UserProfile element to root
    objXML.appendChild objDataItem.cloneNode(true)
    objXML.Save(strOutputFile)

    Set objDataItem = Nothing
    Set objXML = Nothing

    If err.Number <> 0 Then
    WScript.Quit(err.Number)
    Else
    WScript.Quit(0)
    End If


    Function getCurrentUsername()
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
    currentUser = objItem.UserName
    Next
    Set objItem = Nothing
    Set colItems = Nothing
    Set objWMIService = Nothing
    getCurrentUsername = currentUser
    End Function

    Function getTimestamp(t)
    ' Generate universal timestamp
    yyyy = Year(t)
    mm = Month(t)
    dd = Day(t)
    hh = Hour(t)
    nn = Minute(t)
    ss = Second(t)

    If Len(mm) = 1 Then
    mm = "0" & mm
    End If
    If Len(dd) = 1 Then
    dd = "0" & dd
    End If
    If Len(hh) = 1 Then
    hh = "0" & hh
    End If
    If Len(nn) = 1 Then
    nn = "0" & nn
    End If
    If Len(ss) = 1 Then
    ss = "00" & ss
    End If
    getTimestamp = yyyy & mm & dd & hh & nn & ss
    End Function

All Replies

  • Saturday, January 26, 2013 1:28 PM
    Moderator
     
     

    Hi,

    Step 1 is to remove the line On Error Resume Next from your code. Do not use that line of code unless you understand exactly what it does and how it works.

    Bill

  • Sunday, January 27, 2013 8:11 AM
     
     

    remove the on eror resume next. 

    There is not issue with the script. its running fine. issue is that when i configure this script to run from startup or from the logon it did not collect the mydocument data to only those user which mydocuments are not redirected to file server. Rest the user which are migrated to file server it collects there size and location (i.e. on which server there mydocuments are redirected.)


    Malik

  • Sunday, January 27, 2013 8:54 AM
     
     

    remove the on eror resume next. 

    There is not issue with the script. its running fine. issue is that when i configure this script to run from startup or from the logon it did not collect the mydocument data to only those user which mydocuments are not redirected to file server. Rest the user which are migrated to file server it collects there size and location (i.e. on which server there mydocuments are redirected.)


    Malik

    Your question is very hard to understand.

    You must remove the on Error line to see where the script is erroring and it is.

    How can you tell how this works.  Redirection is pretty much invisible.  How do you plane to discover this?

    Are you asking how to determine when a folder is redirected?  If it is redirected it will not be on the "C" drive.

    Please try to clarify you question.


    ¯\_(ツ)_/¯

  • Sunday, January 27, 2013 9:00 AM
     
     Answered Has Code

    remove the on eror resume next. 

    There is not issue with the script. its running fine. 


    Malik

    It is likely that your script tries to process a folder without sufficient access rights. The "On Error" statement that Bill pointed out would hide any resulting error message. Try the version below - it will probably reveal the cause of the problem.

    '------------------------
    Const dataFolder = "\\Server1\Test\" ' Path to folder where clients will write inventory XML files
    Const ForWriting = 2
    ' Constants for special folders
    Const MY_DOCUMENTS = &H5&
    Const MY_VIDEOS = &He&
    Const MY_PICTURES = &H27&
    Const MY_MUSIC = &Hd&
    Const DESKTOP = &H10&
    '------------------------------
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Shell.Application")
    Set objNetwork = WScript.CreateObject("WScript.Network")
    
    ' Quit if data folder does not exist
    If Not objFSO.FolderExists(dataFolder) Then WScript.Quit(1)
    
    ' Determine computer name and current user account
    strComputer = objNetwork.ComputerName
    strUser = getCurrentUsername()
    strTimeStamp = getTimestamp(Now())
    strOutputFile = dataFolder & "\" & strUser & "@" & strComputer & ".xml"
    
    ' My Documents
    Set objDocuments = objShell.Namespace(MY_DOCUMENTS)
    Set objDocumentsItem = objDocuments.Self
    strDocumentsPath = objDocumentsItem.Path
    Set objDocuments = objFSO.GetFolder(strDocumentsPath)
    
    ' My Pictures
    Set objPictures = objShell.Namespace(MY_PICTURES)
    Set objPicturesItem = objPictures.Self
    strPicturesPath = objPicturesItem.Path
    Set objPictures = objFSO.GetFolder(strPicturesPath)
    
    ' My Videos
    Set objVideos = objShell.Namespace(MY_VIDEOS)
    Set objVideosItem = objVideos.Self
    strVideosPath = objVideosItem.Path
    Set objVideos = objFSO.GetFolder(strVideosPath)
    
    ' My Music
    Set objMusic = objShell.Namespace(MY_MUSIC)
    Set objMusicItem = objMusic.Self
    strMusicPath = objMusicItem.Path
    Set objMusic = objFSO.GetFolder(strMusicPath)
    
    ' Desktop
    Set objDesktop = objShell.Namespace(DESKTOP)
    Set objDesktopItem = objDesktop.Self
    strDesktopPath = objDesktopItem.Path
    Set objDesktop = objFSO.GetFolder(strDesktopPath)
    
    ' Calculate sizes in KB
    MsgBox "Location of My Documents " & objDocuments.Path
    mydocumentsSize = objDocuments.Size / 1024
    mypicturesSize = objPictures.Size / 1024
    myvideosSize = objVideos.Size / 1024
    mymusicSize = objMusic.Size / 1024
    desktopSize = objDesktop.Size / 1024
    
    ' Output to XML...
    Err.Clear
    ' Create an instance of the DOM
    Set objXML = CreateObject("Microsoft.XMLDOM")
    objXML.async = False
    objXML.insertBefore objXML.createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'"), objXML.firstChild
    
    ' Create the DataItem element
    Set objDataItem = objXML.createElement("DataItem")
    ' Create all child elements
    objDataItem.appendChild objXML.createElement("Username")
    objDataItem.appendChild objXML.createElement("ComputerName")
    objDataItem.appendChild objXML.createElement("MyDocuments")
    objDataItem.appendChild objXML.createElement("MyPictures")
    objDataItem.appendChild objXML.createElement("MyVideos")
    objDataItem.appendChild objXML.createElement("MyMusic")
    objDataItem.appendChild objXML.createElement("Desktop")
    objDataItem.appendChild objXML.createElement("TimeStamp")
    objDataItem.appendChild objXML.createElement("MyDocumentPath")
    ' Add values to the child elements
    objDataItem.childNodes(0).text = strUser
    objDataItem.childNodes(1).text = strComputer
    objDataItem.childNodes(2).text = Round(mydocumentsSize, 0)
    objDataItem.childNodes(3).text = Round(mypicturesSize, 0)
    objDataItem.childNodes(4).text = Round(myvideosSize, 0)
    objDataItem.childNodes(5).text = Round(mymusicSize, 0)
    objDataItem.childNodes(6).text = Round(desktopSize, 0)
    objDataItem.childNodes(7).text = strTimeStamp
    objDataItem.childNodes(8).text = strDocumentsPath
    ' Add UserProfile element to root
    objXML.appendChild objDataItem.cloneNode(True)
    objXML.Save(strOutputFile)
    
    WScript.Quit(Err.number)
    
    Function getCurrentUsername()
    	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    	Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    	For Each objItem In colItems
    		getCurrentUsername = objItem.UserName
    	Next
    End Function
    
    'Generate universal timestamp
    Function getTimestamp(t)
    	getTimestamp = Year(t) & Pad(Month(t)) & Pad(Day(t)) & Pad(Hour(t)) & Pad(Minute(t)) & Pad(Second(t))
    End Function
    'Pad single-digit numbers with a loeading "0"
    Function Pad(x)
    	Pad = Right("0" & x, 2)
    End Function