简体   繁体   English

powershell 不等待命令完成?

[英]powershell not waiting for command to finish?

I have a command which gets md5 sum's of a bunch of files which takes a bit - it seems like powershell is NOT waiting for this command to finish before moving on to the rest of the script??我有一个命令可以获取一堆文件的 md5 总和,这需要一点时间 - 似乎 powershell 在继续执行脚本的 rest 之前没有等待此命令完成? is that possible?那可能吗?

Script:脚本:

$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 

It never completes the first md5sum action on the first line before finding that the variable localmd5 is null... what gives?在发现变量 localmd5 是 null 之前,它永远不会完成第一行的第一个 md5sum 操作......给出了什么?

UPDATE 1:更新 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
---------

ERROR MSG:错误信息:

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

Update 2更新 2

PS Version output 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

Line 1 command works alone but when running in the script the variable NEVER gets set for some reason... here it is just the command running第 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

When I've had similar issues, opening the script in Notepad++ and using either Show All Characters or viewing the state in the Encoding menu has been helpful.当我遇到类似问题时,在Notepad++中打开脚本并使用显示所有字符或在编码菜单中查看 state 会很有帮助。

It is also possible to do a quick test like this也可以像这样进行快速测试

[int][char] 'x'

120

FINALLY figured out what this issue was - running the debugger in powershell and comparing the script vs the manual command this like caught my eye:终于弄清楚了这个问题是什么——在 powershell 中运行调试器并将脚本与手动命令进行比较,这引起了我的注意:

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

Tony’s should be Tony's the freaking issue was BOM and not using UTF8 BOM in my script, - fixed that up.托尼的应该是Tony'sTony’s的问题是 BOM,而不是在我的脚本中使用 UTF8 BOM, - 解决了这个问题。 re-saved and it worked without issue.重新保存,它没有问题。

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

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