如何为所有系统驱动器运行powershell代码?

时间:2013-07-11 12:38:10

标签: powershell

目前代码仅针对驱动器运行 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

2 个答案:

答案 0 :(得分:2)

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

其他一些说明:

  • 我删除了$results |ft -auto。你可能在那里作为调试输出。
  • 你这样做了两次:$results | sort-object -property size -Descending | select-object -first 30。我做了一次,将输出存储回$results。通过相同的管道步骤多次跳闸相同的数据是低效的。
  • $ahere-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
    

答案 1 :(得分:-1)

$dirA = Get-WmiObject win32_logicaldisk -Filter "Drivetype=3"|select -expandproperty deviceid
$dirB ="\"
$dir = $dirA +=$dirB