Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Script advice when using Runspace to invoke Powershell

$
0
0

Hi, I'm in my first attempt in creating an aspx.vb script which accepts inputs from the front end and executes a powershell script.  I have a couple of questions

  1. The Powershell script requires input parameters prior to script execution e.g. “./testscript.ps1 –parameter1 [input] –parameter2 [input]"
  2. How do I close the Runspace session? / use the system.dbnull etc…
  3. What does this error mean "The thread 'Pipeline Execution Thread' (0x2844) has exited with code 0 (0x0).
    A first chance exception of type 'System.Management.Automation.CommandNotFoundException' occurred in System.Management.Automation.dll
    The program '[11076] iisexpress.exe: Managed (v4.0.30319)' has exited with code 0 (0x0)."

Thanks for the help in advance :o)

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Web
Imports System.Web.UI.Page
Imports System.Web.UI.WebControls
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.IO
Imports System.Text

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs)

        Dim fileOK As Boolean = False
        Dim SizeOK As Boolean = False

        If FileUploadControl.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                    GetExtension(FileUploadControl.FileName).ToLower()
                Dim allowedExtensions As String() = _
                    {".txt"}
                For i As Integer = 0 To allowedExtensions.Length - 1

                'Dim Filesize As Integer = .PostedFileContent
                If fileExtension = allowedExtensions(i) Then
                    fileOK = True
                Else
                    StatusLabel.Text = "Upload status: Only text files are accepted"
                End If
            Next


If fileOK Then
Try
Dim objFSO, objFolder
objFSO = CreateObject("Scripting.FileSystemObject")
objFolder = "D:\Reports\uploads\" & TextBox1.Text & "-" & DateTime.Now.ToString("yyyyMMdd-HHmmss-fff")
	If objFSO.FolderExists(objFolder) = True Then
        StatusLabel.Text = "Upload status: File was not uploaded"
          Else
          objFSO.CreateFolder(objFolder)
          System.IO.Directory.SetCurrentDirectory(objFolder.ToString)
          StatusLabel.Text = "Upload status: Entering New Upload folder please wait"
          StatusLabel.Text = "Upload status: Creating Upload folder " & objFolder & ", Please now wait"
          Dim physicalFolder = System.IO.Directory.GetCurrentDirectory().ToString
          Dim fileName As String = Guid.NewGuid().ToString
          Dim extension As String = System.IO.Path.GetExtension(FileUploadControl.FileName)
          FileUploadControl.PostedFile.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
          StatusLabel.Text = "Upload status: File uploaded!"
          End If
Dim runSpace As Runspace = RunspaceFactory.CreateRunspace() 
runSpace.Open()
Dim q As String = System.IO.Directory.GetCurrentDirectory().ToString
Dim r As String = TextBox1.Text
Dim pipeLine As Pipeline = runSpace.CreatePipeline()					
Dim PSStarted As New Command("D:\Reports\Check.ps1")
PSStarted.Parameters.Add("SAID", "r")
PSStarted.Parameters.Add("CSVPATH", "q")
pipeLine.Commands.Add(PSStarted)
Pipeline.Invoke()
pipeLine.Dispose()
runSpace.Dispose()
Catch ex As Exception

End try
End If
End IF
End Sub
End Class


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>