Wait for PowerShellContext.ExecuteScript

Apr 14, 2014 at 3:56 PM
Hello,

has anybody an example for Waiting for completion when executing

PowerShellContext.ExecuteScript ?


My Code:
Public Overrides Sub PerformAction(contextObjects As IList(Of ContextObject))

        If Not IsNothing(contextObjects) Then

            For Each _vm As VMContext In contextObjects.OfType(Of VMContext)()

                CleanShutdownVM(_vm.ID.ToString, "", "", "")

                System.Threading.Thread.Sleep(2000)

                StartVM(_vm.ID.ToString, "", "", "")

            Next

        End If

    End Sub



  Private Function CleanShutdownVM(ByVal _vmID As String, ByVal _scvmm_server As String, ByVal _username As String, ByVal _password As String) As Boolean

        Dim _rt As Boolean = False

        Try

            Me.PowerShellContext.ExecuteScript(String.Format("$vm = Get-SCVirtualMachine -ID {0}; Stop-SCVirtualMachine -VM $vm", _vmID))

            _rt = True

        Catch ex As Exception
            _rt = False
        End Try

        Return _rt


    End Function

    Private Function StartVM(ByVal _vmID As String, ByVal _scvmm_server As String, ByVal _username As String, ByVal _password As String) As Boolean

        Dim _rt As Boolean = False

        Try

            Me.PowerShellContext.ExecuteScript(String.Format("$vm = Get-SCVirtualMachine -ID {0}; Start-SCVirtualMachine -VM $vm", _vmID))

            _rt = True

        Catch ex As Exception
            _rt = False
        End Try

        Return _rt

    End Function

And sorry for my bad english....

Seji