VBscript 获取用户文档和桌面文件夹大小

VBscript 获取用户文档和桌面文件夹大小

我一直在网上搜索一个不错的脚本,以便能够收集 Windows 7 计算机上子文件夹的大小。我想获取大约 50 台计算机上 C:\Users\username\Documents 和 C:\Users\username\Desktop 上的文件夹大小。

我没有脚本编写经验(将因此开始学习)并且没有足够的知识来编辑其他人的脚本。

下面是我所用的脚本。如果有人能给我指明正确的方向,我将不胜感激。

' Name : localprofiles.vbs
' Description : script to enumerate the local profile size of all computers and users in Active Directory
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 28-06-2011

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
strFilter = "(&(objectCategory=computer)(|(operatingSystem=Windows XP Professional)(operatingSystem=Windows 7*)))"
strAttributes = "name, operatingSystem"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF
    strHostname = adoRecordset.Fields("name").Value
    If CheckStatus(strHostname) = True Then
        If Instr(adoRecordset.Fields("operatingSystem").Value, "XP") > 0 Then
            strLocalProfilePath = "\Documents and Settings\"
        ElseIf Instr(adoRecordset.Fields("operatingSystem").Value, "7") > 0 Then
            strLocalProfilePath = "\users\"
        End If
        GetLocalProfileSize strHostname, "\\" & strHostname & "\c$" & strLocalProfilePath
    End If
    adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close

Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

Function CheckStatus(strAddress)
    Dim objPing, objRetStatus
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strAddress & "'")
    For Each objRetStatus In objPing
        If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then
            CheckStatus = False
        Else
            CheckStatus = True
        End If
    Next
    Set objPing = Nothing
End Function

Function GetLocalProfileSize(strTargetMachine, strFolder)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    For Each SubFolder in objFolder.SubFolders
        Logprint strTargetMachine & " ; " & SubFolder.Name & " ; " & SubFolder.Path & " ; " & Round(SubFolder.Size/1048576,2) & " MB"
    Next
    Set objFolder = Nothing
    Set objFSO = Nothing
End Function

Function LogPrint(Message)
Const ForAppending = 8
strDate = Replace(Date,"/","-")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = ObjFSO.OpenTextFile("c:\temp" & strDate & "-localprofiles.csv", ForAppending, True)
    objTextFile.WriteLine Message
    objTextFile.Close
Set objTextFile = Nothing
Set ObjFSO = Nothing
End Function

答案1

我知道这不会回答你关于 vbscript 的问题,但如果你愿意的话,我建议你使用 powershell。它通常更容易,只是举个例子...

以下是开始的链接:http://technet.microsoft.com/en-us/library/ff730945.aspx

以下是他们示例的修改版本:

$colItems = (Get-ChildItem C:\users\username\desktop -recurse | Measure-Object -property length -sum)
"{0:N2}" -f ($colItems.sum / 1MB) + " MB"

更改值C:\users\username\desktop并告诉我您是否喜欢结果。如果您喜欢并需要有关循环和从远程计算机获取数据的帮助,请告诉我。

脚本 v1:它并不完美,但我认为它在大多数情况下都能正常工作。您需要下载 Quest AD CMDlet 并将其安装在要运行它的系统上。此外,您需要将执行策略设置为 remotesigned。然后将此脚本复制到此系统的本地驱动器。您需要编辑三个值,$rootou(这是您要搜索的 OU)和 #Export 路径下的两行。让我知道它是如何工作的。或者如果您需要上传脚本的副本。

#Script used to gather data size of remote computers user profiles by Eric C. Singer

#a generic powershell object used to group a collection of various objects into a single object 
$data = new-object psobject
$columns = @"
ComputerName
ProfileName
DocumentsSizeMB
DesktopSizeMB
"@
$columns -split "`n" |%{$data | add-member -membertype noteproperty -name $_.trim() -value $null}

#Store a list of computers that we couldn't connect to
$failedcomputers = New-Object System.Collections.ArrayList

#Store the results of the gathered data
$foldersize = New-Object System.Collections.ArrayList

#Root OU that you want to start searching for computers
$RootOU = "yourdomain.org/your ou/ your sub-ou"

#Getting a list of all computers
$allcomputers = Get-QADComputer -SearchRoot "$rootou" -SizeLimit 0

#Export paths
$failedcomputersfiles = "c:\yourpath\yourfile.csv"
$foldersizefile = "c:\yourpath\yourfile.csv"

#Looping through each computer
Foreach ($computer in $allcomputers)
    {
#Seeing if we can connect to the computer, if not, we're going to add it to the failedcomputers array.
    If (Test-Path "\\$($computer.name)\c$")
        {
#Setting the two possiable paths based on whether its Windows 7 or XP
        $Windows7ProfileRoot = "\\$($computer.name)\c$\Users"
        $WindowsXPProfileRoot = "\\$($computer.name)\c$\Documents and Settings"
#if the computer is windows 7 run this, or go to the else statement
        If ($($computer.OSName) -like "Windows 7*")
            {
#getting a list of profiles
            $allprofiles = Get-ChildItem $Windows7ProfileRoot | Where-Object {$_.PSIsContainer -eq $true}
#Looping through each profile and running the following.
            Foreach ($user in $allprofiles)
                {
                $data.ComputerName = $computer.name
                $data.ProfileName = $user.name
                $userdesktop = (Get-ChildItem "$Windows7ProfileRoot\$($user.name)\desktop" -recurse | Measure-Object -property length -sum).sum / 1MB
                $userdocuments = (Get-ChildItem "$Windows7ProfileRoot\$($user.name)\documents" -recurse | Measure-Object -property length -sum).sum / 1MB
                $data.DesktopSizeMB = $userdesktop
                $data.DocumentsSizeMB = $userdocuments
                $data | Select-Object * | ForEach-Object {$foldersize.Add($_)}
                }
            }
        Else
            {
            $allprofiles = Get-ChildItem $WindowsXPProfileRoot | Where-Object {$_.PSIsContainer -eq $true}
            Foreach ($user in $allprofiles)
                {
                $data.ComputerName = $computer.name
                $data.ProfileName = $user.name
                $userdesktop = (Get-ChildItem "$WindowsXPProfileRoot\$($user.name)\desktop" -recurse | Measure-Object -property length -sum).sum / 1MB
                $userdocuments = (Get-ChildItem "$WindowsXPProfileRoot\$($user.name)\my documents" -recurse | Measure-Object -property length -sum).sum / 1MB
                $data.DesktopSizeMB = $userdesktop
                $data.DocumentsSizeMB = $userdocuments
                $data | Select-Object * | ForEach-Object {$foldersize.Add($_)}
                $data
                }
            }
        }
    Else
        {
        $failedcomputers.Add("$($computer.name)")
        }
    }

$failedcomputers | Export-Csv $failedcomputersfiles -NoTypeInformation
$foldersize | Export-Csv $foldersizefile -NoTypeInformation

相关内容