1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with 'Complicated' VBScript

Discussion in 'Meet and Greet' started by rstruble, Oct 7, 2009.

  1. rstruble

    rstruble New Member

    Joined:
    Oct 7, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Windows Desktop Support
    Location:
    In a LinuxBox
    Hi,
    I need help creating a script that will go through a computer, look at all the user profiles in C:\Documents and Settings, then for each profile it finds, it lists the size of the My Documents folder in MB, preferably without the My Music and My Pictures folders. i need it to write the computer name, the profile, and the size of the folder into a CSV file on my computer.

    is this possible with VBScript?

    -R.Struble
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Yes, it is possible in VBScript :)

    But, how far have you reached ?
    We won't write the whole code for you. We can "help" you make your code better.

    Hint : Use VBScript's Folder object.
     
  3. rstruble

    rstruble New Member

    Joined:
    Oct 7, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Windows Desktop Support
    Location:
    In a LinuxBox
    to tell da truth, i dont know much about VB script at all. my boss is trying to help me, but we cant figure out how to make our script able to run through Altiris, right now i have to type in the computer name in a input box.

    If there is a special way to post the code we have, please let me know so i can show you what we have so far :)

    -RS
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Yeah, post your code here.

    Remember to enclose it within [noparse]
    Code:
    .
    .
    <your code here>
    .
    .
    
    [/noparse]
     
  5. rstruble

    rstruble New Member

    Joined:
    Oct 7, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Windows Desktop Support
    Location:
    In a LinuxBox
    Code:
    Dim strComputer,strFileName,strCSV,TotalSize
    
    strFileName = GetPathOfVBS & "\MyDocsReport.csv"
    strComputer = InputBox ("Enter the computer name to scan","My Documents Scan", strComputer)
    
    If strComputer = "" Then
      WScript.Echo "Script cancelled"
      WScript.Quit
    Else
      GetFolderSize
      OutputToFile
    End If
    
    'get file paths to examine, skipping system profiles and My Music & My Pictures folders
    Sub GetFolderSize 
      On Error Resume Next
      Dim fso, StrDocandSettings, aryUser,aryFolders, strUserName, strFolderName
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set StrDocandSettings = fso.getFolder("\\" & strComputer & "\c$\documents and settings")
      Set aryUser = StrDocandSettings.SubFolders
      'Skip these profiles:
      For Each strUserName in aryUser
        If strUserName.name = "All Users" Then
        ElseIf strUserName.name = "Default User" Then
        ElseIf strUserName.name = "LocalService" Then
        ElseIf strUserName.name = "NetworkService" Then
        ElseIf strUserName.name = "Administrator" Then
        Else
          TotalSize = 0
          Set StrDocandSettings = fso.getFolder("\\" & strComputer &_
             "\c$\Documents and Settings\" & strUserName.name & "\My Documents")
          Set aryFolders = StrDocandSettings.SubFolders
          For Each strFolder in aryFolders
            'skip these folders:
            If instr(strFolder,"My Music") = 0 And instr(strFolder,"My Pictures") = 0 Then
              GetSize(strFolder)
              SpaceMB = Round(TotalSize/1048576,1) & " MB" 'format bytes into MB
            End If
          Next
          strCSV = strCSV & strComputer & "," & strUserName.name & "," & SpaceMB & VbCrLf
        End If
      Next
      WScript.Echo "Script complete. View " & strFileName & " to see results."
    End Sub
    
    'Gets folder size in bytes. accumulates total size.
    Sub GetSize(FolderPath)  
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFolder = objFSO.GetFolder(FolderPath)
      TotalSize = TotalSize + objFolder.Size
    End Sub
    
    'returns file path to where this script is located
    Function GetPathOfVBS 
      Set objShell = CreateObject("Wscript.Shell")
      strPath = Wscript.ScriptFullName
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.GetFile(strPath)
      strFolder = objFSO.GetParentFolderName(objFile) 
      GetPathOfVBS = strFolder
    End Function
    
    'appends output to results file
    Sub OutputToFile 
      Const ForWriting = 2  
      Const ForAppending = 8  
      strDelimter = ","  'separates each data element for CSV file   
      Set FileSys = createobject("Scripting.FileSystemObject") 
      If FileSys.FileExists(strFileName) Then 'just put results in existing file
        Set objFile = FileSys.OpenTextFile(strFileName,ForAppending)   
        objFile.Write strCSV
        objFile.Close   
      Else  'if no results file, create new CSV and describe header
        Set objFile = FileSys.CreateTextFile(strFileName,ForWriting)   
        strText = "Computer Name" & strDelimter & "Profile Name" & strDelimter & "Size of My Documents" & VbCrLf
        strText = strText & strCSV
        objFile.Write strText
        objFile.Close 
      End If
    End Sub
    
    Like that?

    -RS
     
    Last edited by a moderator: Oct 9, 2009
  6. jazscript

    jazscript New Member

    Joined:
    Oct 21, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I'm trying to get the script to save to \\remote_server\D$\Logs\%computername%" instead of \\remote_server\D$\Logs\".

    I'm new to VB and I've been trying for about 2 weeks. I'm doing something wrong. Can you stear me in the right direction?

    ===========================================================
    Code:
    Dim DestServer, strComputer
    ' Put in the UNC path for where you want the logs to be stored
    
    DestServer = "\\remote_server\D$\Logs\"
    
    
    'Create the Time variables
    sDate=Right("0" & Month(Date),2) _
    & "-" & Right("0" & Day(Date),2) _
    & "-" & Right(Year(Date),2)
    
    sTime = DatePart("h", Now) & DatePart("n", Now)
    
    set oFSO = CreateObject("Scripting.FileSystemObject")
    
    
    'If correct folder doesn't exist, make it
    if Not oFSO.FolderExists(DestServer & sDate) then
       set oFolder = oFSO.CreateFolder(DestServer & sDate )
    end if
    
    'Gets the log files for this machine
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
            & strComputer & "\root\cimv2")
    
    Set colLogFiles = objWMIService.ExecQuery _
        ("Select * from Win32_NTEventLogFile")
    
    
    'This section goes out and gets the hostname this is run on for us.
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
      strHOSTNAME = objItem.Name
    NEXT
    
    
    'Now archive the logs and clear them
    if oFSO.FolderExists(DestServer & sDate) then
      For Each objLogfile in colLogFiles
        strBackupLog = objLogFile.BackupEventLog _
            (DestServer & strComputer & sDate & "\" & strHOSTNAME & "_" & objLogFile.LogFileName & "_" & sDate & "_" & sTime & ".evt")
        objLogFile.ClearEventLog()
      Next
    end if
     
    Last edited by a moderator: Oct 22, 2009
  7. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    You need to moify the below line, and add the computername to it
    Code:
    DestServer = "\\remote_server\D$\Logs\"
    You can fetch it from environment variables, using the below code
    Code:
    Set wshShell = WScript.CreateObject( "WScript.Shell" )
    strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
    and then you can modify the code to
    Code:
    DestServer = "\\remote_server\D$\Logs\" & strComputerName
    Few more ways to get computername: here
     
    Last edited: Oct 22, 2009

Share This Page