2014-09-23 66 views
1

當我的腳本啓動時,它將文件從一個目錄移動到另一個目錄。文件完全下載後,我啓動一個應用程序。
這一切都有效,但我想要的是在文件被移動時出現的彈出窗口(大文件)。
當我調試我的代碼後,它碰到Move-Item Cmdlet,它會一直等到該命令完成後再繼續。我想要做的是在Move-Item Cmdlet正在運行時,彈出一個信息窗口。 我知道如何做彈出窗口和移動項目,我只是不知道如何讓它按我想要的方式工作。有任何想法嗎?

彈出代碼正在移動文件時Powershell彈出

#pop up window letting mechanic know we are waiting for the files to be downloaded before opeing the SMT application 
      $wshell = New-Object -ComObject Wscript.Shell 
      $wshell.Popup("The EAFR file is still being moved to the correct directory, please wait.",0,"SMT Status",0) 

#Move-Item 
    $MLMoveDir = "C:\move\data\AutoUpload\" 
    Move-Item -LiteralPath ($filePath) $MLMoveDir 
+0

爲什麼要彈出?爲什麼不直接寫入控制檯,然後在完成時寫入? – 2014-09-23 21:54:18

+0

因爲用戶正在點擊圖標來啓動應用程序而不是PowerShell。 – 2014-09-23 21:59:11

+0

不清楚什麼*如何讓它按我想要的方式工作*的意思。你想以什麼方式工作? – 2014-09-23 22:04:02

回答

1

一個選項是使用WinForms顯示請稍等對話框,而不是必須由用戶解僱的Popup。例如:

Add-Type -AssemblyName System.Windows.Forms 
$Form = New-Object system.Windows.Forms.Form 
$Label = New-Object System.Windows.Forms.Label 
$Form.Controls.Add($Label) 
$Label.Text = "Copying file, please wait." 
$Label.AutoSize = $True 
$Form.Visible = $True 
$Form.Update() 

#Move-Item 
$MLMoveDir = "C:\move\data\AutoUpload\" 
Move-Item -LiteralPath ($filePath) $MLMoveDir 

#Hide popup 
$Form.Close() 
+0

這工作,謝謝邁克! – 2014-09-24 16:15:59

0

所以,你可以做的是開始布展項目的作業,然後做了一段時間((送崗位「工作名」)。聲明-ne完成){做彈出}。我會是這個樣子:

#Move-Item 
    $MLMoveDir = "C:\move\data\AutoUpload\" 
    $MoveJob = Start-Job -scriptblock {Move-Item -LiteralPath ($filePath) $MLMoveDir} 
#Show Popup 
    While($movejob.state -ne "Completed"){ 
     $wshell = New-Object -ComObject Wscript.Shell 
     $wshell.Popup("The EAFR file is still being moved to the correct directory, please wait.",1,"SMT Status",0) 
    } 

這樣的彈出窗口顯示1秒,如果此舉仍發生再次顯示它。我不知道它甚至會消失/重新顯示,所以它可能是無縫的。

+0

我喜歡這個@TheMadTechnician,我只需要知道我是否可以在要運行的機器上升級到PowerShell 3.0。 – 2014-09-24 13:41:54