Visual Basic Powershell查询服务器挂起重新启动状态

时间:2016-01-29 14:22:34

标签: vb.net powershell status reboot

我是VB.net的新手。电源外壳。 我正在编写一个查询Win2012服务器的PendingReboot状态的应用程序。 有一个很棒的脚本,我在我的程序中实现了这样做: https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542

这是我在Zainnab中使用的代码,要从VB2010.net访问Powershell,请将结果作为字符串返回。

'Takes script text as input and runs it, then converts
    'the results to a string to return to the user
    Private Function RunScript(ByVal scriptText As String) As String
    '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()

    ‘ convert the script result into a single string
    Dim MyStringBuilder As New StringBuilder()

    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next

    ‘ return the results of the script that has
    ‘ now been converted to text
    Return MyStringBuilder.ToString()

End Function
    Try

        ‘ Create an instance of StreamReader to read from our file.
        ‘ The using statement also closes the StreamReader.
        Dim sr As New StreamReader(filename)

        ‘ use a string builder to get all our lines from the file
        Dim fileContents As New StringBuilder()

        ‘ string to hold the current line
        Dim curLine As String = ""

        ‘ loop through our file and read each line into our
        ‘ stringbuilder as we go along
        Do
            ‘ read each line and MAKE SURE YOU ADD BACK THE
            ‘ LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
            curLine = sr.ReadLine()
            fileContents.Append(curLine + vbCrLf)
        Loop Until curLine Is Nothing

        ‘ close our reader now that we are done
        sr.Close()

        ‘ call RunScript and pass in our file contents
        ‘ converted to a string
        Return fileContents.ToString()

    Catch e As Exception
        ‘ Let the user know what went wrong.
        Dim errorText As String = "The file could not be read:"
        errorText += e.Message + "\n"
        Return errorText
    End Try

End Function

...

Dim ausgabe2 As String
            ausgabe2 = RunScript(LoadScript("c:\FHServerStat\PS_PR.stat\60.2.ps1"))
------
  • 我设法让它部分工作。

我遇到的问题是,通过执行powershell中的脚本,rusult是正确的。

通过visual basic执行脚本,SOME返回的值不正确,但请亲自看看: enter image description here

请帮我解决这个问题

此致

奥肯

0 个答案:

没有答案