gpt4 book ai didi

powershell - 使用 Powershell 提取文件的前 50 个字节

转载 作者:行者123 更新时间:2023-12-02 16:44:57 25 4
gpt4 key购买 nike

我正在 powershell 中编写脚本以从服务器提取文件信息并将数据写入 csv 文件以供审查。我的大部分脚本运行良好,但是我最初尝试提取每个文件的第一行导致意外结果,因为某些文件没有/cr,因此打印了整个文件内容。

我想将每个文件的前 50 个字节提取到 csv 中,但 -TotalCount 读取第一行,我一直在读取 .NET [IO.File] 和 [System.Text.Encoding] 但找不到工作方法。

我的代码目前是:


Select-Object FullName,Name,Directory,@{n="Owner";ex={(Get-ACL $_.Fullname).Owner}},CreationTimeUtc,LastAccessTimeUtc,LastWriteTimeUtc,@{n='Size(MB)';ex={[math]::Round($_.length/1MB, 2)}},
@{n='MD5';ex={(Get-FileHash $_.fullname).hash}},@{n="Content(UTF8)";ex={(Get-Content $_.fullname -AsByteStream -Raw -TotalCount 50)}} |

Export-Csv c:\temp\$fn-filelisting.csv -NoTypeInformation

似乎 PS 曾经使用字节作为 -TotalCount 的选择,但后来将其更改为按行读取。

由于系统限制不得不使用PS,所以任何想法将不胜感激。

最佳答案

这将返回一个字节数组。在 powershell 5 中,它是 -encoding byte

get-content file -AsByteStream -totalcount 50

使用 powershell 5 并获取字符串:

$a = get-content file -encoding byte -totalcount 50
-join [char[]]$a

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

关于powershell - 使用 Powershell 提取文件的前 50 个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com