Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Meet and Greet (http://www.go4expert.com/community/meet-and-greet/)
-   -   Need help with 'Complicated' VBScript (http://www.go4expert.com/community/help-complicated-vbscript-t19698/)

rstruble 7Oct2009 23:16

Need help with 'Complicated' VBScript
 
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

SaswatPadhi 8Oct2009 06:02

Re: Need help with 'Complicated' VBScript
 
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.

rstruble 8Oct2009 19:19

Re: Need help with 'Complicated' VBScript
 
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

SaswatPadhi 8Oct2009 21:06

Re: Need help with 'Complicated' VBScript
 
Yeah, post your code here.

Remember to enclose it within
[code=VB]
.
.
<your code here>
.
.
[/code]

rstruble 9Oct2009 20:00

Re: Need help with 'Complicated' VBScript
 
Code: VB

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

jazscript 22Oct2009 04:02

Re: Need help with 'Complicated' VBScript
 
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


nimesh 22Oct2009 21:26

Re: Need help with 'Complicated' VBScript
 
You need to moify the below line, and add the computername to it
Code: vb

DestServer = "\\remote_server\D$\Logs\"


You can fetch it from environment variables, using the below code
Code: vb

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )


and then you can modify the code to
Code: vb

DestServer = "\\remote_server\D$\Logs\" & strComputerName


Few more ways to get computername: here


All times are GMT +5.5. The time now is 13:33.