0

目前代码仅针对驱动器C 运行:我想将以下代码运行/应用到所有现有系统驱动器的子文件夹内容,即C:\ D:\ E:\ 等。关于如何操作的任何建议为达到这个?如果你需要我澄清我的问题,尽管问。(**我希望$dir替换所有系统驱动器根目录)。

$dir = "c:\"
#this is main client-side file scanning code for use on client computers
$count = @{}
$size = @{}
$hostname = @{}
gci $dir -recurse |%{
[int]$count[$_.extension] += 1
[int64]$size[$_.extension] += $_.length
}
$results = @()
$count.keys | sort |% {
$result = ""|select extension,count,size,hostname
$result.extension = $_
$result.count = $count[$_]
$result.size = [math]::round($size[$_] /1Gb, 3)
$result.hostname = $(get-content env:computername)
$results += $result
}
$results | ft -auto

$dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
if (!(Test-Path $dirName)) { mkdir $dirName }

$results | sort-object -property size -Descending | select-object -first 30| export-csv c:\"$env:computername-$(get-date -f dd-MM-yyyy-HH-mm)".csv

$a = "<style>"
$a = $a + "BODY{background-color:#A987CC;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}"
$a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#99CCFF}"
$a = $a + "TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"


$results | sort-object -property size -Descending | select-object -first 30 | ConvertTo-Html extension,count,size, hostname "$a" -title "JUST" -body "TOP 30 FILES" | 
Set-Content C:\inetpub\wwwroot\${Env:ComputerName}\"$env:computername-$(get-date -f dd-MM-yyyy-hh-mm)".htm
4

2 回答 2

2

您几乎拥有它,您真正需要的只是获取计算机上所有物理驱动器的列表,然后循环遍历它。

其他几点注意事项:

  • 我删除了$results |ft -auto. 你可能把它作为调试输出放在里面。
  • 你这样做两次:$results | sort-object -property size -Descending | select-object -first 30。我做过一次,将输出存储回$results. 通过相同的管道步骤多次传输相同的数据是低效的。
  • 我做$a了一个here-string。比连接字符串更容易阅读和使用。
  • 我重新格式化了输出文件中的日期字符串,以便您可以按日期对文件名进行排序。
  • 不要将文件放在c:\. 这只是草率。寻找更合适的位置。
  • 这将捕获所有文件夹(您没有将它们过滤掉)。这是想要的吗?

    $drives = Get-WmiObject win32_logicaldisk -Filter "Drivetype=3"|select -expandproperty deviceid;
    
    #this is main client-side file scanning code for use on client computers
    $results = @()
    foreach ($drive in $drives){
    $count = @{}
    $size = @{}
    $hostname = @{}
    gci $drive -recurse |%{
        [int]$count[$_.extension] += 1
        [int64]$size[$_.extension] += $_.length
    }   
    $count.keys | sort |% {
        $result = ""|select extension,count,size,hostname;
        $result.extension = $_;
        $result.count = $count[$_];
        $result.size = [math]::round($size[$_] /1Gb, 3);
        $result.hostname = $(get-content env:computername);
        $results += $result;
    }
    }
    
    $dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
    if (!(Test-Path $dirName)) { New-Item -ItemType directory $dirName }
    $results = $results|Sort-Object -property size -Descending|Select-Object -First 30;
    
    $results | export-csv c:\"$env:computername-$(get-date -f yyyy-MM-dd-HH-mm)".csv
    
    $a = @"
    <style>
    BODY{background-color:#A987CC;}
    TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}
    TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#99CCFF}
    TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod}
    </style>
    "@
    
    $results | ConvertTo-Html extension,count,size, hostname "$a" -title "JUST" -body "TOP 30 FILES" | Set-Content C:\inetpub\wwwroot\${Env:ComputerName}\"$env:computername-$(get-date -f yyyy-MM-dd-HH-mm)".htm
    
于 2013-07-11T13:44:39.457 回答
-1
$dirA = Get-WmiObject win32_logicaldisk -Filter "Drivetype=3"|select -expandproperty deviceid
$dirB ="\"
$dir = $dirA +=$dirB
于 2013-11-04T11:29:53.437 回答