簡體   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