none
CMD using VB RRS feed

  • Question

  • How to run this command which is can only run as administrator by click the button. So far this is my code which was error.

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

    Shell("cmd.exe /c netsh advfirewall firewall add rule name=""IP Block"" dir=in interface=any action=block remoteip=ipaddress")

    End Sub

    and how to replace the value for ipaddress with the value in TextBox1 ? Please help me. 



    • Edited by tommywick Wednesday, October 4, 2017 5:23 PM
    Wednesday, October 4, 2017 12:51 PM

Answers

  • Simple looking at running a command line operations see the following

    https://stackoverflow.com/questions/16804103/run-another-program-as-administrator

    In regards to what you are looking to execute, that has nothing to do with any .NET language so let's keep focus on how to run something.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by tommywick Wednesday, October 4, 2017 3:28 PM
    • Unmarked as answer by tommywick Wednesday, October 4, 2017 5:22 PM
    • Marked as answer by tommywick Thursday, October 5, 2017 1:45 PM
    Wednesday, October 4, 2017 1:35 PM
    Moderator

  • Hi tommywick,

    As far as I know, You can try to use ProcessStartInfo and Process.Start() to run your application with Administrator access.

     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
          
            Dim textvalue As String
            textvalue = "111.111.111.111"
            Dim vmdtxt As String
            vmdtxt = "netsh advfirewall firewall add rule name=""IP Block Test2"" dir=in interface=any action=block remoteip=" + textvalue + ""
            MyUtilities.RunCommandCom(vmdtxt, "", True)
        End Sub
    
    
    Public Class MyUtilities
        Shared Sub RunCommandCom(command As String, arguments As String, permanent As Boolean)
            Dim p As Process = New Process()
            Dim pi As ProcessStartInfo = New ProcessStartInfo()
            pi.Verb = "runas" ' runas = Run As Administrator
            pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
            pi.FileName = "cmd.exe"
            p.StartInfo = pi
            p.Start()
        End Sub
    End Class
    


    The following links for your reference.

    How to run DOS/CMD/Command Prompt commands from VB.NET:
    https://stackoverflow.com/questions/10261521/how-to-run-dos-cmd-command-prompt-commands-from-vb-net

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by tommywick Thursday, October 5, 2017 1:45 PM
    Thursday, October 5, 2017 3:37 AM
    Moderator

All replies

  • Hi

    Not that I know very much about it, but, I wouldn't like some programmer having such a simple way to interact with my firewall.

    The security implications could suggest malicious intent - maybe you have some valid reasons, but I would hope such things are not broadcast to the world.


    Regards Les, Livingston, Scotland

    Wednesday, October 4, 2017 1:05 PM
  • What is the error?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, October 4, 2017 1:35 PM
  • Simple looking at running a command line operations see the following

    https://stackoverflow.com/questions/16804103/run-another-program-as-administrator

    In regards to what you are looking to execute, that has nothing to do with any .NET language so let's keep focus on how to run something.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by tommywick Wednesday, October 4, 2017 3:28 PM
    • Unmarked as answer by tommywick Wednesday, October 4, 2017 5:22 PM
    • Marked as answer by tommywick Thursday, October 5, 2017 1:45 PM
    Wednesday, October 4, 2017 1:35 PM
    Moderator
  • Hi leshay , I am doing because it for my project as a student and thanks for the advice. 
    Wednesday, October 4, 2017 3:09 PM
  • The rule was not created in the firewall even i had clicked the button.  
    Wednesday, October 4, 2017 3:11 PM
  • The rule was not created in the firewall even i had clicked the button.  

    I'm not sure whether you answered my question. You said there was an error. What do you mean by that?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, October 4, 2017 3:24 PM
  • Hi Karen , Thanks for the link. It help me with the privilege problem. 
    Wednesday, October 4, 2017 3:28 PM

  • Hi tommywick,

    As far as I know, You can try to use ProcessStartInfo and Process.Start() to run your application with Administrator access.

     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
          
            Dim textvalue As String
            textvalue = "111.111.111.111"
            Dim vmdtxt As String
            vmdtxt = "netsh advfirewall firewall add rule name=""IP Block Test2"" dir=in interface=any action=block remoteip=" + textvalue + ""
            MyUtilities.RunCommandCom(vmdtxt, "", True)
        End Sub
    
    
    Public Class MyUtilities
        Shared Sub RunCommandCom(command As String, arguments As String, permanent As Boolean)
            Dim p As Process = New Process()
            Dim pi As ProcessStartInfo = New ProcessStartInfo()
            pi.Verb = "runas" ' runas = Run As Administrator
            pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
            pi.FileName = "cmd.exe"
            p.StartInfo = pi
            p.Start()
        End Sub
    End Class
    


    The following links for your reference.

    How to run DOS/CMD/Command Prompt commands from VB.NET:
    https://stackoverflow.com/questions/10261521/how-to-run-dos-cmd-command-prompt-commands-from-vb-net

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by tommywick Thursday, October 5, 2017 1:45 PM
    Thursday, October 5, 2017 3:37 AM
    Moderator
  • @Yohann L

    Hi

    Am I wrong in thinking that this can be used in a malicious manner?

    Should an application be allowed to manipulate a user FireWall silently?

    Maybe I'm off track in my thinking, but I feel there are security implications in doing this kind of thing.

    Maybe the OP has no such malicious intent, but is it right to publish this?


    Regards Les, Livingston, Scotland

    Thursday, October 5, 2017 3:46 AM
  • Thanks Yohann Lu for replied and answered my questions. It worked :) 
    Thursday, October 5, 2017 1:45 PM
  • @Yohann L

    Hi

    Am I wrong in thinking that this can be used in a malicious manner?

    Should an application be allowed to manipulate a user FireWall silently?

    Maybe I'm off track in my thinking, but I feel there are security implications in doing this kind of thing.

    Maybe the OP has no such malicious intent, but is it right to publish this?


    Regards Les, Livingston, Scotland

    The user is attempting to executed a fully documented command.

    If the user does not have sufficient privilege the program will not achieve its objective.  This is no different than executing the command from a command-line prompt.

    There are other documented API's that are available to programatically control the windows firewall.  For example, see Windows Firewall with Advanced Security Interfaces

    I don't see the "malicious manner" issue here.

    Thursday, October 5, 2017 2:22 PM
  • @Yohann L

    Hi

    Am I wrong in thinking that this can be used in a malicious manner?

    Should an application be allowed to manipulate a user FireWall silently?

    Maybe I'm off track in my thinking, but I feel there are security implications in doing this kind of thing.

    Maybe the OP has no such malicious intent, but is it right to publish this?


    Regards Les, Livingston, Scotland

    The user is attempting to executed a fully documented command.

    If the user does not have sufficient privilege the program will not achieve its objective.  This is no different than executing the command from a command-line prompt.

    There are other documented API's that are available to programatically control the windows firewall.  For example, see Windows Firewall with Advanced Security Interfaces

    I don't see the "malicious manner" issue here.

    Hi

    OK, thanks. Maybe a bit paranoid here. As stated, I don't know much about the topic, just a little taken aback at thinking of some 'innocent' programme being able to manipulate my FireWall without my permission/knowledge.

    For all I know,maybe this is a common place thing to happen?


    Regards Les, Livingston, Scotland

    Thursday, October 5, 2017 2:52 PM
  • OK, thanks. Maybe a bit paranoid here. As stated, I don't know much about the topic, just a little taken aback at thinking of some 'innocent' programme being able to manipulate my FireWall without my permission/knowledge.

    For all I know,maybe this is a common place thing to happen?


    Regards Les, Livingston, Scotland

    The code isn't circumventing any security features since you still need to pop the UAC dialog for elevating privileges. This process is not uncommon, which is why .NET provides a way to do so.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, October 5, 2017 3:26 PM