Powershell 如何在GetFileHash中使用写入进度

Powershell 如何在GetFileHash中使用写入进度,powershell,Powershell,我想在运行get Filehash命令时获得进度, 这是我的一个代码,但是它非常慢,执行起来需要更多的时间 Set-ExecutionPolicy Unrestricted -force # Getting Filehash Write-Host " " Write-Host "----------------------------------------------------------------------" Write-Host "Before Proceed, Copy the f

我想在运行get Filehash命令时获得进度, 这是我的一个代码,但是它非常慢,执行起来需要更多的时间

Set-ExecutionPolicy Unrestricted -force
# Getting Filehash
Write-Host " "
Write-Host "----------------------------------------------------------------------"
Write-Host "Before Proceed, Copy the full Directory path or file path  " -ForegroundColor White -BackgroundColor DarkMagenta
Write-Host "----------------------------------------------------------------------"
Write-Host " "
cd\
cd .\setup
$a=hostname
$output=$a+'hash.txt'
$file = Get-ChildItem -file (Read-Host "Enter Full Directory or File path") -Recurse 
                $filecount= $file.count
                Foreach ($filecount in $file)
                    {
                        $i++
                        Write-Progress -activity "Processing file:- $filecount" -Status "MD5 CheckSum Progress file:- $i" -percentcomplete ($i/$file.count*100)
                        $file | Get-FileHash -Algorithm MD5 |Sort-Object path | Format-Table hash, Path -HideTableHeaders | Out-File $output                    
                    }
if (Test-Path $output) { 
 if((Get-Item $output).length -gt 0kb){
get-Content $output
Write-Host " "
Write-Host "CheckSum Completed Successfully " -ForegroundColor White -BackgroundColor Darkgreen
Write-Host 'Press any key to return to main menu...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
} else {
Write-Host " "
Write-Host "Error Occured" -ForegroundColor red 
Write-Host " "
Write-Host "please check the file name or directory path "
Write-Host " "
Write-Host 'Press any key to return to main menu...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}
}
此代码正在工作,但处理速度非常慢。请帮助加快脚本的编写进度

第二种方法:

$file = Get-ChildItem -file (Read-Host "Enter Full Directory or File path") -Recurse 
$filecount= $file.count
                For($i = 0; $i -le $filecount; $i++) {
                        Write-Progress -activity "MD5 Check Processing " -Status "Progress file:- $i of $filecount" -percentcomplete ($i/$file.count*100)
                        $file | Get-FileHash -Algorithm MD5 | Sort-Object path | Format-Table hash, Path -HideTableHeaders -AutoSize | Out-File $output -Width 200                                         
                    }

您的代码正在对整个文件集对每个文件运行一次
Get-FileHash
cmdlet。[grin]对于每个循环,您的
for
foreach
版本都将整个文件集合通过管道传输到cmdlet

这里有一种更简洁的方法,可以让你每次只处理一次文件

# save the old Information preference
$OldInfoPref = $InformationPreference
# enable Information display
$InformationPreference = 'Continue'


$TargetDir = Read-Host 'Please enter the full path to the target Directory '

$FileList = Get-ChildItem -LiteralPath $TargetDir -Recurse -File

$Counter = 0
$Results = foreach ($FL_Item in $FileList)
    {
    $Counter ++
    Write-Information ('Processing file {0} of {1} ...' -f $Counter, $FileList.Count)

    # this will _silently_ skip files that are locked for whatever reason
    $FileHash = Get-FileHash -LiteralPath $FL_Item.FullName -Algorithm MD5 -ErrorAction SilentlyContinue

    # if this is empty, then the "else" block will show "__Error__" in the Hash column
    if ($FileHash)
        {
        [PSCustomObject]@{
            Hash = $FileHash.Hash
            Path = $FileHash.Path
            }
        }
        else
        {
        [PSCustomObject]@{
            Hash = '__Error__'
            Path = $FL_Item.FullName
            }
        } # end >> if ($FileHash)
    } # end >> foreach ($FL_Item in $FileList)

# on screen display
$Results

# send to CSV file    
$Results |
    Export-Csv -LiteralPath "$env:TEMP\FileHashListing.csv" -NoTypeInformation

# restore Information preference
$InformationPreference = $OldInfoPref
部分屏幕活动

Please enter the full path to the target Directory : c:\temp
Processing file 1 of 566 ...
Processing file 2 of 566 ...
Processing file 3 of 566 ...
Processing file 4 of 566 ...
Processing file 5 of 566 ...
Processing file 6 of 566 ...
Processing file 7 of 566 ...
[*...snip...*] 
部分屏幕结果

Hash                             Path
----                             ----
D41D8CD98F00B204E9800998ECF8427E C:\temp\2018-11-17_22-00-00_-_TimeTest.txt
DD9972A59154D439B807217B40F7B569 C:\temp\au-descriptor-1.8.0_191-b12.xml
358D74CA3FB4A8DB01D2C1AB8BD7A0B5 C:\temp\CleanedVersion.log
33F2B09E2D9DFB732FA16B5F05A5A8D1 C:\temp\Enable1_WordList_File.txt
96B2DBFAE3F0353BF9FE5D87DD922C11 C:\temp\Func_Get-Name.ps1
__Error__                        C:\temp\FXSAPIDebugLogFile.txt
2E31A9B8BE6203D7636ED9E6A2B7D8CB C:\temp\Genre-List_2018-10-09.log
[*...snip...*] 
部分CSV文件内容

"Hash","Path"
"D41D8CD98F00B204E9800998ECF8427E","C:\temp\2018-11-17_22-00-00_-_TimeTest.txt"
"DD9972A59154D439B807217B40F7B569","C:\temp\au-descriptor-1.8.0_191-b12.xml"
"358D74CA3FB4A8DB01D2C1AB8BD7A0B5","C:\temp\CleanedVersion.log"
"33F2B09E2D9DFB732FA16B5F05A5A8D1","C:\temp\Enable1_WordList_File.txt"
"5E6D32360C0A701B7A211C2AED5DD928","C:\temp\FileHashListing.csv"
"96B2DBFAE3F0353BF9FE5D87DD922C11","C:\temp\Func_Get-Name.ps1"
"__Error__","C:\temp\FXSAPIDebugLogFile.txt"
"2E31A9B8BE6203D7636ED9E6A2B7D8CB","C:\temp\Genre-List_2018-10-09.log"
[*...snip...*] 

我刚刚在现有脚本上添加了编写进度,请看一看

$Counter = 0
    $Results = foreach ($FL_Item in $FileList)
        {
        $Counter ++
        Write-Progress -Activity "MD5 Check Processing :- $FL_Item " -Status "Processed $Counter " -PercentComplete (($Counter / $FileList.Count) * 100)
        # this will _silently_ skip files that are locked for whatever reason
        $FileHash = Get-FileHash -LiteralPath $FL_Item.FullName -Algorithm MD5 -ErrorAction SilentlyContinue

        # if this is empty, then the "else" block will show "__Error__" in the Hash column
        if ($FileHash)
            {
            [PSCustomObject]@{
                Hash = $FileHash.Hash
                Path = $FileHash.Path
                }
            }
            else
            {
            [PSCustomObject]@{
                Hash = '__Error__'
                Path = $FL_Item.FullName
                }
            } # end >> if ($FileHash)
        } # end >> foreach ($FL_Item in $FileList)
    # send to text file

我张贴了完整的脚本,这是工作与写作进展良好

Set-ExecutionPolicy Unrestricted -force
# Getting Filehash
Write-Host " "
Write-Host "----------------------------------------------------------------------"
Write-Host "Before Proceed, Copy the full Directory path or file path  " -ForegroundColor White -BackgroundColor DarkMagenta
Write-Host "----------------------------------------------------------------------"
Write-Host " "
cd\
cd .\script
$a=hostname
$output=$a+'hash.txt'
$TargetDir = Read-Host 'Please enter the full path to the target Directory '
$FileList = Get-ChildItem -LiteralPath $TargetDir -Recurse -File 
$filecount= $FileList.count
$Counter = 0
$Results = foreach ($FL_Item in $FileList)
    {
    $Counter ++
    Write-Progress -Activity "MD5 Check Processing :- $FL_Item " -Status "Processed $Counter of $filecount" -PercentComplete (($Counter / $FileList.Count) * 100)
    # this will _silently_ skip files that are locked for whatever reason
    $FileHash = Get-FileHash -LiteralPath $FL_Item.FullName -Algorithm MD5 -ErrorAction SilentlyContinue

    # if this is empty, then the "else" block will show "__Error__" in the Hash column
    if ($FileHash)
        {
        [PSCustomObject]@{
            Hash = $FileHash.Hash
            Path = $FileHash.Path
            }
        }
        else
        {
        [PSCustomObject]@{
            Hash = '__Error__'
            Path = $FL_Item.FullName
            }
        } # end >> if ($FileHash)
    } # end >> foreach ($FL_Item in $FileList)
# send to text file    
$Results |
   Format-Table -HideTableHeaders -AutoSize | Out-File -FilePath $output -Width 200 

[1] 您的
$filecount=
行没有任何作用,因为您在其后的
foreach
中使用了相同的$Var[2] 每次foreach循环迭代时,您都在每个文件上运行hash cmdlet!
$file |
东西一次又一次地发送整个文件列表。。。[grin]//[3]cmdlet的
写入进度
一点也不快。如果您只需要一个常规指示器来指示已完成的操作,请使用计数器或仅显示文件名。@Lee_Dailey,我现在添加了计数器,它看起来快了一点,但如果文件大小越大,它占用的时间就越多,请帮我编辑脚本。我是PowerShell的初学者。您仍在对每个文件多次运行哈希cmdlet。[grin]让我试试代码……脚本开头的设置ExecutionPolicy毫无意义,因为在运行.PS1文件之前会对执行策略进行评估感谢您的时间,上面的脚本可以很好地处理写入信息,而且这需要Windows PowerShell 5.0,@karhtik-非常欢迎您!我喜欢写它。我没有注意到PS版本的限制。哎呀!这对写入进度很好,而且与写入信息一样更快。thanks@Lee_Dailey谢谢你们的提示和脚本,我用你们的想法来做这个答案,T。阿南,谢谢你们的时间,答案是你们提供的两个脚本的组合,你们是最受欢迎的!很高兴能帮上一点忙。。。[咧嘴笑]