Get-FileHash无法读取文件

时间:2019-10-19 17:26:53

标签: powershell hash

我想使用Beginning configuration step: Starting the server Attempting to start service MySQL80... Successfully started service MySQL80. Waiting until a connection to MySQL Server 8.0.18 can be established (with a maximum of 10 attempts)... Retry 1: Attempting to connect to Mysql@localhost:3305 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 2: Attempting to connect to Mysql@localhost:3306 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 3: Attempting to connect to Mysql@localhost:3305 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 4: Attempting to connect to Mysql@localhost:3306 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 5: Attempting to connect to Mysql@localhost:3305 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 6: Attempting to connect to Mysql@localhost:3306 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 7: Attempting to connect to Mysql@localhost:3305 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 8: Attempting to connect to Mysql@localhost:3306 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 9: Attempting to connect to Mysql@localhost:3305 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Waiting 5 seconds before the next connection attempt... Retry 10: Attempting to connect to Mysql@localhost:3306 with user root with no password... MySQL error 1042: Unable to connect to any of the specified MySQL hosts. Failed to connect to MySQL Server 8.0.18 after 10 attempts. Ended configuration step: Starting the server 为某些目录填充一组哈希。这是代码:

try:
    resp = requests.get(url)
    resp.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)

但是它显示以下错误:

  

Get-FileHash:无法读取文件“ C:\ Intel \ Logs \ IntelCPHS.log”:   该进程无法访问文件'C:\ Intel \ Logs \ IntelCPHS.log'   因为它正在被另一个进程使用。在:2字符:22   + dir“ C:\”-递归| Get-FileHash-算法MD5 | Export-Csv-路径“ C ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:ReadError:(C:\ Intel \ Logs \ IntelCPHS.log:PSObject)[Write-Error],   WriteErrorException       + FullyQualifiedErrorId:FileReadError,Get-FileHash

对此有何帮助?还是有其他替代方法可用于填充哈希?

3 个答案:

答案 0 :(得分:0)

如@Alex_P所述,请关闭该进程,我相信它是 IntelCpHeciSvc.exe 。 如果您遇到了很多此类问题,则还可以尝试在以“安全模式”启动Windows时运行CMDlet,这样做可以摆脱很多可能触发此错误的后台进程。

如果您需要其他帮助,请随时提问。

答案 1 :(得分:0)

使用Sysinternals进程浏览器,您可以搜索文件句柄并查看哪个进程将其锁定。

答案 2 :(得分:0)

我还没有深入研究 Get-FileHash 的代码,但它可能只用 FileShare.Read 标志打开文件。如果文件已经用 FileShare.WriteFileShare.ReadWrite 打开,这将失败。后续进程指定的共享标志必须与打开文件的原始进程使用的标志兼容,有关详细信息,请参阅 this QA

一种解决方法是使用所需标志显式创建流并将其传递给 Get-FileHash 参数 -InputStream

Get-ChildItem 'C:\' -File -Recurse -PipelineVariable File | ForEach-Object {
       
    $stream = try {
        [IO.FileStream]::new( $File.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read )
    }
    catch {
        # Fallback in case another process has opened the file with FileShare.ReadWrite flag.
        [IO.FileStream]::new( $File.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite )
    }

    if( $stream ) {
        try {
            Get-FileHash -InputStream $stream -Algorithm MD5 | 
                Select-Object Algorithm, Hash, @{ Name = 'Path'; Expression = { $File.Fullname } }
        }
        finally {
            $stream.Close()
        }
    }
}

注意:

  • -PipelineVariable File 用于避免 $_ 块和 catch 调用的 Expression 脚本块中的 Get-FileHash 变量的歧义。
  • $stream = try { } catch { } 是一种无需重复变量名即可捕获 trycatch 块的输出的便捷方法。它等价于 try { $stream =[IO.FileStream]::new(...)} catch { $stream =[IO.FileStream]::new(...)}。这也适用于许多其他语句,例如 if / elseswitchfor
  • 使用 Select-Object,添加了 calculated property 以修复 Path 属性。使用 -InputStream 时,Get-FileHash cmdlet 不知道路径,因此它会输出一个空的 Path 属性。
  • 显式关闭 finally 块中的流总是一个好主意,因为垃圾收集器的不确定性最终关闭文件但可能很晚。文件等共享资源应仅在必要时保持打开状态,以避免不必要地阻塞其他进程。
  • 顺便说一句,Get-Content 读取使用 FileShare.ReadWrite 打开的文件没有问题。与 Get-FileHash 相比如何实现 Get-Content 并可能在 PowerShell GitHub 项目中产生问题,这可能值得研究。