我需要在兩組代碼之間添加一個(相對)較小的停頓,因爲後半部分需要在運行前成功執行前半部分 - 但PowerShell有時會比自己早一點並在前一個命令完全完成之前繼續。Powershell:Sleeping mid-pipeline
僅適用於上下文,這是我的工作代碼:
Initialize-Disk -PassThru -PartitionStyle GPT -Confirm:$false `
| New-Partition -DriveLetter D -UseMaximumSize
這往往失敗,因爲驅動器不完全的PowerShell執行New-Partition
時被初始化。
通常我會使用類似:
Initialize-Disk -PassThru -PartitionStyle GPT -Confirm:$false
Start-Sleep -Seconds 2
New-Partition -DriveLetter D -UseMaximumSize
然而,問題是,Initialize-Disk
輸出對象將丟失,New-Partition
沒有得到一個輸入對象。
我試圖把Start-Sleep
在管道:
Initialize-Disk -PassThru -PartitionStyle GPT -Confirm:$false `
| Start-Sleep -Seconds 2 `
| New-Partition -DriveLetter D -UseMaximumSize
...但它拋出一個異常,如Start-Sleep
不知道如何使輸入體感(這是不夠公平)。
Start-Sleep : The input object cannot be bound to any parameters for the command either because the command does not take pipeline
input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\Users\Administrator\Desktop\test.ps1:104 char:87
+ ... nfirm:$false | Start-Sleep -Seconds 2 `
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MSFT_Disk (Obje...11E2-93F2-0...):PSObject) [Start-Sleep], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StartSleepCommand
TL;博士:我怎樣才能在我的代碼中添加停頓,同時保持前面的cmdlet的輸出對象?
對於它的價值,這不適合我。 – Bink 2016-06-05 01:29:32