October 23rd, 2012, 09:42 AM
Code behind runs in VS 2010 but not when published on IIS
I have an ASP.NET site I built for our users to map a network printer. When I test it, it maps the printer without any probelms. When I publish it to IIS, it acts like it's going to run, but never tries to map it. Any suggestions or answers will be greatly appreciated. I have checked all the permissions, I have tried it as a web app and a web site, I have tried to publish both and I have tried to copy and paste the folders in the root folder. I have added references to the Microsoft.Powershell.XXXX. I have changed it from "CodeBehind" to "CodeFile" and I have changed the "AutoEventWireup" to "true". There are no errors in the event log and none on screen. It clears the fields, like it should at the end of the code and that's it. I am not sure what else to do now. Thank you.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If DropDownList1.Text = "ABCD" Then
Label1.Text = " \\ServerB"
If DropDownList1.Text = "EFGH" Then
Label1.Text = " \\ServerC"
If DropDownList1.Text = "IJKL" Then
Label1.Text = " \\ServerD"
If DropDownList1.Text = "MNOP" Then
Label1.Text = " \\ServerE"
'Maps the printer from the selections above.
Label2.Text = "rundll32 printui.dll,PrintUIEntry /q /in /c\\"
Label3.Text = " /n"
Label4.Text = Label2.Text & Label3.Text & Label1.Text & "\" & TextBox3.Text
'Sets it as default
If RadioButton1.Checked = True Then
Label5.Text = Label2.Text & " /y" & Label1.Text & "\" & TextBox3.Text
'Sets it for all users
If RadioButton2.Checked = True Then
Label6.Text = Label2.Text & " /ga" & Label1.Text & "\" & TextBox3.Text
Dim printerserver As String = CStr(Label1.Text)
Dim printer As String = CStr(TextBox3.Text)
Dim printcmd As String = CStr(Label2.Text)
Dim networkpath As String = CStr(Label4.Text)
Dim networkdefault As String = CStr(Label5.Text)
Dim allusers As String = CStr(Label6.Text)
'Clears the screen so that a new printer can be added.
TextBox3.Text = ""
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
DropDownList1.Text = ""
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
October 23rd, 2012, 09:36 PM
I see that the "magic" happens in a series of three calls to the Shell() function. You have the mistaken notion that this function is running on the end user's computer, where the browser this. That is wrong. Your Shell() calls run on your web server. The code seems to work in Visual Studio because in the case, the server and browser are on the same computer.
So the question remains, how do you get this code to run from the web browser? The answer is that you don't. Web browsers are designed as sandboxes for web content specifically to prevent running this kind of arbitrary code on the local system. There is now work-around that will allow you to use the Shell() command the way you want. Otherwise, for example, everyone's desktop printer would collect the same kind of junk a public fax machine attracts.
So what can you do? Unfortunately, you're only option if you keep going down the same path is to build a browser add on, and that's not likely to gain much traction from your users or your IT dept. Thankfully, there is a feature built into Windows Print Servers that allows them to publish printers via the web. You should be able to activate this feature in IIS, find the address for each printer, and then provide a link your users can click on. And that's the best you'll likely be able to do.
October 26th, 2012, 01:06 AM
Is there any other way around it? What about building a text box that inputs the code, and from there the powershell is called to run?
October 29th, 2012, 10:34 PM
You can't call powershell from the browser without a custom add-on. Period. Again, preventing this kind of access is a core part of what browsers do.
Originally Posted by jethro45
December 12th, 2012, 12:24 AM
Ugg Fox Fur Boots
Is there any other way around it?What about building a text box that inputs the code, and from there the powershell is called to run?
December 16th, 2012, 10:23 PM
That won't work. You just can't start the powershell process. No way, no how. End of story.