繁体   English   中英

powershell 不等待命令完成?

[英]powershell not waiting for command to finish?

我有一个命令可以获取一堆文件的 md5 总和,这需要一点时间 - 似乎 powershell 在继续执行脚本的 rest 之前没有等待此命令完成? 那可能吗?

脚本:

$localmd5 = Get-ChildItem "C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone" -Recurse -File | 
            select-object name, fullname, 
                @{Name = "MD5"; Expression = {(Get-FileHash $_.FullName -Algorithm MD5).Hash}}

$localmd5=$localmd5 | Select-Object -Property MD5 -ExpandProperty MD5 -First 10
Write-Output "got local md5's"
$null -eq $localmd5


$remotemd5=rclone md5sum remote:"My Pictures\Tony\Automatic Upload\Tony’s iPhone" --dry-run
$remotemd5=$remotemd5.foreach({($_ -split '\s+')[0]})
Write-Output "got remote md5's"
Write-Output $remotemd5

Write-Output "checking for diffs..."
$diffmd5=Compare-Object -ReferenceObject $localmd5 -DifferenceObject $remotemd5 -IncludeEqual
Write-Output $diffmd5 

在发现变量 localmd5 是 null 之前,它永远不会完成第一行的第一个 md5sum 操作......给出了什么?

更新 1:

PS C:\Users\Tony> Test-Path "C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone"

True

PS C:\Users\Tony> $ErrorActionPreference

Continue


PS C:\Users\Tony> $PSVersionTable | Select-Object -Property PSVersion


PSVersion
---------

错误信息:

Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.
At C:\Users\Tony\Documents\pcloud_sync.ps1:13 char:42
+ $diffmd5=Compare-Object -ReferenceObject $localmd5 -DifferenceObject  ...
+                                          ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

更新 2

PS版output

Name                           Value
----                           -----
PSVersion                      5.1.19041.610
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.610
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

第 1 行命令单独工作,但在脚本中运行时,由于某种原因,变量永远不会被设置......这里只是正在运行的命令

    PS C:\Users\Tony> Get-ChildItem "C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone" -Recurse -File | 
>>             select-object name, fullname,
>>                 @{Name = "MD5"; Expression = {(Get-FileHash $_.FullName -Algorithm MD5).Hash}}


Name                     FullName                                                                                        MD5
----                     --------                                                                                        ---
2019-11-02 09-03-25.jpeg C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone\2019-11-02 09-03-25.jpeg 80DE8D1C0D55AE3FC21279A15D29C582
2020-02-18 14-29-28.heic C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone\2020-02-18 14-29-28.heic 98745AFC6F415AEC96DD673F675F47BA

当我遇到类似问题时,在Notepad++中打开脚本并使用显示所有字符或在编码菜单中查看 state 会很有帮助。

也可以像这样进行快速测试

[int][char] 'x'

120

终于弄清楚了这个问题是什么——在 powershell 中运行调试器并将脚本与手动命令进行比较,这引起了我的注意:

DEBUG:    1+  >>>> $localmd5 = Get-ChildItem "C:\Users\Tony\Pictures\My Pictures\Tony\Automatic Upload\Tony’s iPhone"
 -Recurse -File

托尼的应该是Tony'sTony’s的问题是 BOM,而不是在我的脚本中使用 UTF8 BOM, - 解决了这个问题。 重新保存,它没有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM