[英]How to check when a powershell script is done running with Visual Studio
我正在從我的應用程序中運行Powershell腳本。 我可以啟動它並且毫無問題地完成它,但是如何檢查它完成了呢? 我需要執行其他操作, 僅完成一次。 這是我所擁有的,以供參考:
Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If requiredEnd = True And requiredPass = True And requiredPath = True And requiredSheet = True And requiredStart = True And requiredUser = True Then
For I = 0 To 7
objWriter.WriteLine(aryText(I))
Next
objWriter.Close()
Dim p As Process
p.Start("powershell", "-ExecutionPolicy ByPass -windowstyle hidden -file .\\Excel.ps1")
p.WaitForExit()
If p.HasExited = True Then
MsgBox("The Process Has Been Completed!")
Application.Exit()
End If
Else
If requiredEnd = False Or requiredPass = False Or requiredSheet = False Or requiredStart = False Or requiredUser = False Then
MessageBox.Show("You Have Missing Required Fields!")
Else
MessageBox.Show("That Is Not A Valid File!")
End If
End If
End Sub
謝謝您的幫助! 另外,我還是一個初學者,所以如果它不是超級簡單的話,您可以對它的工作方式做一些解釋嗎? 再次感謝。
編輯:我意識到我忘記了關鍵點:我可以關閉腳本,但是我需要知道它是否成功退出。 因此,基本上是一種檢查錯誤的方法。
為什么不使用運行空間在進程內執行您的Powershell代碼,而不是炮轟外部應用程序? 您看到的大多數代碼都在C#中,但您當然也可以在VB.net中進行:
' create Powershell runspace
Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
' open it
MyRunSpace.Open()
' create a pipeline and feed it the script text
Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript(scriptText)
' add an extra command to transform the script output objects into nicely formatted strings
' remove this line to get the actual objects that the script returns. For example, the script
' "Get-Process" returns a collection of System.Diagnostics.Process instances.
MyPipeline.Commands.Add("Out-String")
' execute the script
Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
' close the runspace
MyRunSpace.Close()
該代碼取自該MSDN博客 , 該博客具有更詳盡,完整的示例應用程序,但是這里的基本思想是在應用程序中本機運行代碼,然后您可以通過任何想要測試錯誤的方式獲得輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.