powershell - 如何在哈希表的末尾添加键/值对?

标签 powershell powershell-2.0

我正在尝试使用PowerShell脚本计算代码计数。

我在Internet上找到了一个脚本,并试图在最后添加总行。

我已经添加了专栏

$CountHash.Add("Total", $Total)

在最后。
Param( [string]$path,
       [string]$outputFile,
       [string]$include = "*.*",
       [string]$exclude = "")

Clear-Host

$Files = Get-ChildItem -re -in $include -ex $exclude $path
$CountHash = @{}
$Total=0
Foreach ($File in $Files) {
    #Write-Host "Counting $File.FullName"
    $fileStats = Get-Content $File.FullName | Measure-Object -line
    $linesInFile = $fileStats.Lines
    $CountHash.Add($File.FullName, $linesInFile)

    $Total += $linesInFile
}

$CountHash.Add("Total", $Total)
$CountHash

但是当我显示$ CountHash时,它在中间显示“Total”键。通过在末尾添加Add不能确保将其添加到末尾。

如何在哈希表的末尾添加键/值对?

我将此哈希表导出为CSV文件,但总行在中间。

最佳答案

假设总数仅用于显示,我想将其添加到哈希集没有意义。
删除线

$CountHash.Add("Total", $Total)

并将其添加为最后一行:
Write-Host "Total: $Total"

关于powershell - 如何在哈希表的末尾添加键/值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862361/

相关文章:

powershell - Powershell 2.0 需要什么才能在帮助页面中呈现脚本的默认参数值?

powershell - 为什么我的计算列是空的(它使用哈希表查找)

powershell - Invoke-Command 不会接受 -Path 作为变量

user-interface - Powershell 脚本上的 GUI 叠加

performance - Powershell 脚本在运行空间中的运行速度比在 shell 中慢

powershell - 将参数传递给Register-EngineEvent操作脚本 block

PowerShell、Web 请求和代理

powershell - Get-ADUser 按属性长度过滤

SharePoint 2010 - 更改 SiteCollection URL

powershell - Foreach 盘符数组