2013-07-05 24 views
0

我是Powershell的新手。我希望代碼將結果輸出到一個新文件夾中,並將其命名爲計算機名稱變量。如果該文件夾已經存在,它應該只輸出結果到該文件夾​​中。如何創建一個文件夾並使用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 
+0

而你的代碼以什麼方式不能正常工作? –

+0

如果該代碼正在做你需要它做的事情,那就去吧。你有特別的關注嗎? – alroc

+0

感謝您的回覆。是的,我只需要Joey建議的代碼,以便生成新文件夾並使用主機名命名,然後在該文件夾中生成輸出。我更新了我的問題代碼,以便您可以看到整個代碼。 – Gazel

回答

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

可能?

+0

謝謝!我已經將您的代碼ID納入其中,效果很好!我還在輸出字符串中使用了$ dirName,以便在該文件夾中生成.htm文件。 – Gazel

0

看起來您已經創建了一個報告特定擴展的文件佔用磁盤空間的腳本。我不推薦自己計算這些信息,而是建議使用Group-Object cmdlet將所有文件按其擴展名分組,然後使用Measure-Object cmdlet將其大小相加。我還會使用here string代替<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) 
+0

感謝您的建議。 – Gazel

相關問題