Answered by:
Invoking Powershell within default.aspx.vb

Question
-
User-851819717 posted
I'm looking for some help with my default.aspx.vb script. I have a webpage which an end user will enter an ID and a file, which uploads into a directory.
I have Powershell script which I have converted into a module, and additionally i have an module for launching Exchange Server 2010 Powershell too.
I have tested both modules within native powershell, and they work fine.
With regards to attempts via default.aspx.vb, the file submitted is uploaded correctly, The second part of the aspx.vb code is to run the powershell commmand, does not work, and this is captured by the exception on line 83 (Catch ex As Exception)
Looking for advice with regards why the latter half of the code fails
Thanks
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.Management.Automation.Hosts Imports System.Management.Automation.PowerShell 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") 'strFolder = objFSO.CreateFolder(Server.MapPath("D:\PSTMailBoxReports\uploads\" & TextBox1.Text)) 'objFolder = "D:\PSTMailBoxReports\uploads\" & TextBox1.Text & "-" & DateTime.Now.ToString("yyyyMMdd-HHmmss") objFolder = "D:\PSTMailBoxReports\uploads\" & TextBox1.Text & "-" & DateTime.Now.ToString("yyyyMMdd-HHmmss-fff") If objFSO.FolderExists(objFolder) = True Then StatusLabel.Text = "Upload status: File was not uploaded, as import folder is identical, Please try again in 5 minutes" 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!" ' Create a RunSpace to host the Powershell script environment using RunspaceFactory.CreateRunSpace. Dim Initial As InitialSessionState = InitialSessionState.CreateDefault() Initial.ImportPSModule(New String() {"MailboxPSTCheck", "NewExchangeSession"}) Dim rs As Runspace = RunspaceFactory.CreateRunspace(Initial) rs.Open() ' Dim snapInException As PSSnapInException ' Dim snapInException As PSSnapInException = Nothing ' Dim snapinInfo = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException) Dim q As String = physicalFolder.ToString Dim r As String = TextBox1.Text Dim ID As String = "-SAID" Dim TXTPATH As String = "-TXTPATH" Dim pipe As Pipeline = rs.CreatePipeline() Dim pscommand As Command = New Command("MailboxReport -SAID" & r & "-TXTPATH" & q) pipe.Commands.Add(pscommand) pipe.Invoke() pipe.Dispose() rs.Dispose() rs.Close() StatusLabel.Text = "Upload status: Wait for Report Email" End If Catch ex As Exception StatusLabel.Text = "Upload status: Cannot Accept File. Only text files under 100kb can be accepted" End Try Else StatusLabel.Text = "Upload status: Failed Please contact support" End If End If End Sub End Class
Thursday, April 24, 2014 12:39 PM
Answers
-
User-417640953 posted
Hi Bhav1973,
Thank you post the issue to asp.net forum.
As for how to call a PowerShell Script From Your .NET Code, please refer to below article.
You can use above scenes to asp.net web form.
Besides, please refer to below same thread' answer about this thread.
http://stackoverflow.com/questions/23274424/invoking-powershell-within-default-aspx-vb
Hope that helps, thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 2, 2014 10:44 AM
All replies
-
User-851819717 posted
I included error logging into this aspx.vb page, for me i need to understand my modules are not loading.
Fatal error : The term 'MailboxReport -SAID admin -TXTPATH D:\Reports\uploads\08156-20140427-145235-719' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again., please find a complete error at ErrorLog file.
Dim Initial As InitialSessionState = InitialSessionState.CreateDefault() Initial.ImportPSModule(New String() {"MailboxPSTCheck", "NewExchangeSession"}) Dim rs As Runspace = RunspaceFactory.CreateRunspace(Initial)
Sunday, April 27, 2014 10:03 AM -
User-417640953 posted
Hi Bhav1973,
Thank you post the issue to asp.net forum.
As for how to call a PowerShell Script From Your .NET Code, please refer to below article.
You can use above scenes to asp.net web form.
Besides, please refer to below same thread' answer about this thread.
http://stackoverflow.com/questions/23274424/invoking-powershell-within-default-aspx-vb
Hope that helps, thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 2, 2014 10:44 AM