#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    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.

    [code}

    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    Imports System.Management.Automation.Runspaces
    Imports System.Text
    Imports System.IO





    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

    If DropDownList1.Text = "ABCD" Then
    Label1.Text = " \\ServerB"
    End If

    If DropDownList1.Text = "EFGH" Then
    Label1.Text = " \\ServerC"
    End If

    If DropDownList1.Text = "IJKL" Then
    Label1.Text = " \\ServerD"
    End If

    If DropDownList1.Text = "MNOP" Then
    Label1.Text = " \\ServerE"
    End If

    '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
    End If

    'Sets it for all users
    If RadioButton2.Checked = True Then
    Label6.Text = Label2.Text & " /ga" & Label1.Text & "\" & TextBox3.Text
    End If

    'Powershell commands
    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)



    Shell(networkpath, AppWinStyle.Hide)


    Shell(networkdefault, AppWinStyle.Hide)


    Shell(allusers, AppWinStyle.Hide)


    '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 = ""


    End Sub




    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

    [code]
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    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.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    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?
  6. #4
  7. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    Originally Posted by jethro45
    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?
    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.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    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?
  10. #6
  11. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    That won't work. You just can't start the powershell process. No way, no how. End of story.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn

IMN logo majestic logo threadwatch logo seochat tools logo