Avatar billede samohtrelhe Praktikant
17. oktober 2018 - 14:40

Afvikle Powershell fil med LoadScript+RunScript, MED sti til fil som parameter

Hej
Jeg vil gerne benytte MS reference Funktioner LoadScript og RunScript til at afvikle en Powershell fil med en fil som parameter.

Powershell filen skal altså have stien til en fil den skal læse med som  parameter.
Jeg kan godt regne ud at jeg skal ændre
RunScript(LoadScript(Script.ps1))
til
RunScript(LoadScript(Script.ps1),FileToRead)
men jeg kan simpelthen ikke gennemskue hvordan jeg får loaded indholdet i FileToRead som parameter til scriptet.

(Funktionerne RunScript og LoadScript er nedenfor.)

----------------------------

    Private Function LoadScript(ByVal filename As String) As String
        ' 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

        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

    Private Function RunScript(ByVal scriptText As String) As String
        ' Takes script text as input and runs it, then converts
        ' the results to a string to return to the user

        ' 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
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester