Results 1 to 1 of 1

Thread: [RESOLVED] help for code executing powershell script

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012

    Resolved [RESOLVED] help for code executing powershell script

    I am wanting to build a simple app to detect failing drive and I searched and got result from google on Get-FailingDrive.ps1 powershell script.

    Problem is I dont know how to execute powershell script with authorization to execute and put the result on textbox.

    I really need help on this one. Thanks.

    what I have is below:

    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    Imports System.Management.Automation.Runspaces
    Imports System.Text
    Imports System.IO
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'run our script and put the result into our textbox
            'NOTE: make sure to change the path to the correct location of your script
            TextBox1.Text = RunScript(LoadScript("C:\Users\myname\Downloads\Get-FailingDrive.ps1"))
        End Sub
        ' helper method that takes your script path, loads up the script
        ' into a variable, and passes the variable to the RunScript method
        ' that will then execute the contents
        Private Function LoadScript(ByVal filename As String) As String
                ' 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
                    ' 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
                ' 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
        ' 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
            ' create a pipeline and feed it the script text
            Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
            ' 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.
            ' execute the script
            Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
            ' close the runspace
            ' convert the script result into a single string
            Dim MyStringBuilder As New StringBuilder()
            For Each obj As PSObject In results
            ' return the results of the script that has
            ' now been converted to text
            Return MyStringBuilder.ToString()
        End Function
    End Class
    I did set-executionpolicy unrestricted and it was a success but when I execute the script on powershell, it did not return anything.

    UPDATE: im going back and use wmi instead.
    Last edited by genlight; Jan 31st, 2018 at 03:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width