这是我用Powershell(我是VBA专家)写的第一本书,因此如果您发现许多问题,请原谅。我真的只是想侧翼,所以非常感谢您的帮助。
我这样做是为了基本上抓住主要的子文件夹并吐出它们的大小,文件数和文件夹数。它工作得很好,输出日志以我想要的方式显示出来。我希望它在控制台以及输出日志中进行更新,因为我们经常检查大文件夹,因此可能要花一些时间,所以我想让人们看到它在每个文件夹中都进行更新。但是,我无法弄清楚。
我看到Tee-Object
可以/应该使用,但我不太了解如何将其与我要输出的所有行合并。我要输出的每一行都带有echo
命令。如果禁用了日志,写主机似乎只能工作。
这是我的代码:
While ($true){
$startDirectory = Read-Host "Enter path/directory"
if (Test-Path -Path $startDirectory) { break }
Write-Host "Wrong path. Please try again" -ForegroundColor Red
}
Write-Host ""
Write-Host "Getting information..." -ForegroundColor Green
Write-Host "This can take a while if the selected directory is large and/or contains a lot of files. An output log will be created at the end."
Write-Host ""
$directoryItems = Get-ChildItem $startDirectory | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
$ParentFileSize = 0
$TotalFiles = 0
$TotalFolders = 0
$SubFileCount = 0
$SubFolderCount = 0
$SubFileSize = 0
$TotalSize = 0
$Tab = [char]9
$(
echo "Output file generated to: $env:USERPROFILE\Desktop\Output.txt" " "
echo "List of Folders in '$startDirectory'" " "
echo "Size (GB): $Tab Files: $Tab Folders: $Tab Folder Name:" " "
$ParentFileCount = (Get-ChildItem $startDirectory -File).Count
$ParentFolderCount = (Get-ChildItem $startDirectory -Directory).Count
$ParentFileSize = Get-ChildItem $startDirectory -File | Measure-Object -property Length -sum | Select-Object Sum
$ParentFileSize = "{0:N2}" -f ($ParentFileSize.sum / 1GB)
foreach ($i in $directoryItems)
{
$subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$FolderSize = "{0:N2}" -f ($subFolderItems.sum / 1GB)
$FileCount = Get-ChildItem $i.FullName -Recurse -File | Measure-Object | %{$_.Count}
$FolderCount = Get-ChildItem $i.FullName -Recurse -Directory | Measure-Object | %{$_.Count}
echo "$FolderSize $Tab $Tab $FileCount $Tab $Tab $FolderCount $Tab $Tab $i"
$SubFileCount = $SubFileCount + $FileCount
$SubFolderCount = $SubFolderCount + $FolderCount
$SubFileSize = $SubFileSize + $FolderSize
}
$TotalFolders = $SubFolderCount + $ParentFolderCount
$TotalFiles = $SubFileCount + $ParentFileCount
$TotalSize = $SubFileSize + $ParentFileSize
$TotalSize = "{0:N2}" -f ($TotalSize)
$SubFileSize = "{0:N2}" -f ($SubFileSize)
echo " " " " "Total in '$startdirectory':" " "
echo "Size: $TotalSize GB ($SubFileSize GB in sub-folders)" "Files: $TotalFiles ($SubFileCount in sub-folders)" "Folders: $TotalFolders ($SubFolderCount in sub-folders)"
) *>&1 > $env:USERPROFILE\Desktop\Output.txt
Invoke-Item $env:USERPROFILE\Desktop\Output.txt
Tee-Object
是正确的方法。它允许您同时写入文件并将输入向下传递到管道,最后在控制台中结束。
将您想要输入的输出代码Tee-Object
移到函数或脚本块中。
使用脚本块的示例:
& {
Write-Output 'foo'
Sleep 2
Write-Output 'bar'
} *>&1 | Tee-Object log.txt
使用函数的示例:
Function FooBar {
Write-Output 'foo'
Sleep 2
Write-Output 'bar'
}
FooBar *>&1 | Tee-Object log.txt
将Sleep
在我的例子只是有证明,而脚本块或功能仍在运行输出显示。
如果您将脚本块更改为$( some code )
,则在代码完成后,您只会立即收到输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句