2014-09-23 35 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