0

我是 Powershell 的新手。我希望代码将结果输出到新文件夹并用计算机名称变量命名。如果文件夹已经存在,它应该只是将结果输出到该文件夹​​中。

最好的方法是什么?

到目前为止,我有以下输出代码:

$dir = "C:\"

$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 }


$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: 0px;border-style: solid;border-color: black;background-color:#99CCFF}"
$a = $a + "TD{border-width: 1px;padding: 0px;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 TESTING :)" -body "I WAS HERE! :)" | 
Set-Content C:\inetpub\wwwroot\${Env:ComputerName}\"$env:computername-$(get-date -f dd-MM-yyyy-hh-mm)".htm
4

2 回答 2

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

也许?

于 2013-07-05T13:51:39.693 回答
0

看起来您已经创建了一个脚本,报告特定扩展名的文件占用的磁盘空间。与其自己计算这些信息,我建议使用Group-Objectcmdlet 按扩展名对所有文件进行分组,并使用Measure-Objectcmdlet 对它们的大小求和。我还将使用 a here stringfor 您的<style>块来代替字符串连接。

$dir = "C:\"

# Get all the files, ignoring errors because we won't have access to some directories
Get-ChildItem $dir -Recurse -ErrorAction SilentlyContinue |
    # We don't want any directories
    Where-Object { -not $_.PsIsContainer } |
    # Group by extension
    Group-Object Extension |
    ForEach-Object {
        # Sum the lenghts of all files in this group/extension
        $totalSize = $_.Group | 
                         Measure-Object -Sum Length | 
                         Select-Object -Expand Sum
        # Add dynamic properties Size and SizeInDB properties for our report
        $_ | 
            Add-Member NoteProperty -Name Size -Value $totalSize -PassThru |
            Add-Member ScriptProperty -Name SizeInGB -Value { [math]::round($this.Size/1GB,3) } -PassThru |
            Add-Member NoteProperty -Name HostName -Value ($env:ComputerName) -PassThru
    } |
    # Sort by size, biggest at the top
    Sort-Object Size -Descending |
    # Save the results in the $results variable
    Tee-Object -Variable results
    # Show a report in the script output
    Format-Table Name,Count,SizeInGB -AutoSize

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

$style = @'
<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: 0px;border-style: solid;border-color: black;background-color:#99CCFF}
    TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}
</style>"
'@

$results |
    # Grab the 30 extensions taking up the most space and create an HTML report
    Select-Object -First 30 |
    ConvertTo-Html Name,Count,SizeInGB,HostName "$a" -title "JUST TESTING :)" -body "I WAS HERE! :)" | 
    Set-Content (Join-Path $dirName du.html)
于 2013-07-09T09:06:12.867 回答