locked
Login to Remote System using vb.net windows application RRS feed

  • Question

  • Hi,

        I have a requirement that i want to kill the process in the remote server and then delete some log files and update the registry editor and then finally log off and log in to the remote System.

     Now, I log off the remote system from my application. But i want to login to the system with valid user credentials from my application . Can anyone please help me how to do?

    Regards,
    Balaji r
    Balaji
    Wednesday, March 18, 2009 9:26 AM

Answers

  • Thank you Amal for your friendly help.

    Hi Balaji,

    Welcome to MSDN forums!

    1.  You can use WMI to login remote computer with credential users.

    Code sample: How to u
    se Windows Management Instrumentation (WMI)
    to connect/login remote computer and retrieve hardware info (e.g. check available space).
    Trackback:http://forums.msdn.microsoft.com/en-US/vblanguage/thread/d25ba4b3-57cd-4127-999e-525810961634/


    Imports
     System.Management  
     
    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            CalculateFreeUsed("SeverName1")       
            CalculateFreeUsed("SeverName2")     
        End Sub 
     
        Private Sub CalculateFreeUsed(ByVal srvname As String)  
            Try 
                'Connection credentials to the remote computer, not needed if the logged account has access  
                Dim oConn As ConnectionOptions = New ConnectionOptions  
                oConn.Username = "AdminUsername" 
                oConn.Password = "AdminPassword" 
                Dim strNameSpace As String = "\\" 
                If (srvname <> ""Then 
                    strNameSpace = (strNameSpace + srvname)  
                Else 
                    strNameSpace = (strNameSpace + ".")  
                End If 
                strNameSpace = (strNameSpace + "\root\cimv2")  
                Dim oMs As ManagementScope = New ManagementScope(strNameSpace, oConn)  
     
                'get Fixed disk state  
                Dim oQuery As ObjectQuery = New ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")  
                'Execute the query  
                Dim oSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(oMs, oQuery)  
     
                'Get the results  
                Dim oReturnCollection As ManagementObjectCollection = oSearcher.Get 
     
                'loop through found drives and write out info  
                Dim D_Freespace As Double = 0  
                Dim D_Totalspace As Double = 0  
                For Each oReturn As ManagementObject In oReturnCollection  
     
                    ' Disk name  
                    ListBox1.Items.Add(("Disk: " + oReturn("Name").ToString))  
     
                    ' Free Space in bytes  
                    ListBox1.Items.Add("Free Space: " + oReturn("FreeSpace").ToString)  
     
                    'Total Space in bytes  
                    ListBox1.Items.Add("Total Space: " + oReturn("Size").ToString)  
                Next 
     
            Catch ex As Exception  
                MessageBox.Show(ex.Message)  
            End Try 
        End Sub 
     
    End Class 




    2. You can connect to remote desktop via running mstsc.exe command in VB.NET like this:

    Detail: http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/f316f409-2e1f-4789-be6d-dd19c7edefec/

    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            'Execute command with arguments in Dos command line  
            Process.Start("Cmd.exe""/C mstsc.exe C:\RemoteServer.rdp")  
            ' /C parameter means exiting Cmd.exe after executing command     
        End Sub 
     
    End Class 




    3. You can use the remote desktop control named msrdp.ocx (that is Microsoft Terminal Sevices control) to connect to remote desktop.

    Right-click on Toolbox in Visual Studio 2005/2008 -> Choose Items -> COM Components tab -> Locate the "Microsoft Terminal Services Control" and add it.

    Then the control named "
    Microsoft Terminal Services Control
    " will be added onto the Toolbox. If you drag&drop it onto Form, the AxMsTscAxNotSafeForScripting1 object will be generated automatically. 
    You can see these properties of the control in Properties pane and 
    play with them to see if they can achieve your goal.

    Code sample: Call "Microsoft Terminal Services Control" to connect remote workstation desktop in VB.NET.

    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            AxMsTscAxNotSafeForScripting1.Server = "MartinXie" 
            AxMsTscAxNotSafeForScripting1.Domain = "fareast" 
            AxMsTscAxNotSafeForScripting1.UserName = "v-maxie" 
            AxMsTscAxNotSafeForScripting1.Connect()  
        End Sub 
     
    End Class 

    Here is the illustration:
     

    Trackback: 
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ee5e2b11-e36f-477c-8476-a88c11e79242/



    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 24, 2009 1:42 PM

All replies

  • http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

    http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22683178.html

    Thanks, A.m.a.L
    Wednesday, March 18, 2009 9:40 AM
  • Hi,

        Thanks for your reply. Actually, the remote system was logged off. I want to login (start) the system from my windows application.

     This is similar to restart the machine with user credentials, but i dont want to restart the machine, i just want to logoff the system and login(like restart) the system.


    Balaji
    Wednesday, March 18, 2009 2:08 PM
  • Thank you Amal for your friendly help.

    Hi Balaji,

    Welcome to MSDN forums!

    1.  You can use WMI to login remote computer with credential users.

    Code sample: How to u
    se Windows Management Instrumentation (WMI)
    to connect/login remote computer and retrieve hardware info (e.g. check available space).
    Trackback:http://forums.msdn.microsoft.com/en-US/vblanguage/thread/d25ba4b3-57cd-4127-999e-525810961634/


    Imports
     System.Management  
     
    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            CalculateFreeUsed("SeverName1")       
            CalculateFreeUsed("SeverName2")     
        End Sub 
     
        Private Sub CalculateFreeUsed(ByVal srvname As String)  
            Try 
                'Connection credentials to the remote computer, not needed if the logged account has access  
                Dim oConn As ConnectionOptions = New ConnectionOptions  
                oConn.Username = "AdminUsername" 
                oConn.Password = "AdminPassword" 
                Dim strNameSpace As String = "\\" 
                If (srvname <> ""Then 
                    strNameSpace = (strNameSpace + srvname)  
                Else 
                    strNameSpace = (strNameSpace + ".")  
                End If 
                strNameSpace = (strNameSpace + "\root\cimv2")  
                Dim oMs As ManagementScope = New ManagementScope(strNameSpace, oConn)  
     
                'get Fixed disk state  
                Dim oQuery As ObjectQuery = New ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")  
                'Execute the query  
                Dim oSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(oMs, oQuery)  
     
                'Get the results  
                Dim oReturnCollection As ManagementObjectCollection = oSearcher.Get 
     
                'loop through found drives and write out info  
                Dim D_Freespace As Double = 0  
                Dim D_Totalspace As Double = 0  
                For Each oReturn As ManagementObject In oReturnCollection  
     
                    ' Disk name  
                    ListBox1.Items.Add(("Disk: " + oReturn("Name").ToString))  
     
                    ' Free Space in bytes  
                    ListBox1.Items.Add("Free Space: " + oReturn("FreeSpace").ToString)  
     
                    'Total Space in bytes  
                    ListBox1.Items.Add("Total Space: " + oReturn("Size").ToString)  
                Next 
     
            Catch ex As Exception  
                MessageBox.Show(ex.Message)  
            End Try 
        End Sub 
     
    End Class 




    2. You can connect to remote desktop via running mstsc.exe command in VB.NET like this:

    Detail: http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/f316f409-2e1f-4789-be6d-dd19c7edefec/

    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            'Execute command with arguments in Dos command line  
            Process.Start("Cmd.exe""/C mstsc.exe C:\RemoteServer.rdp")  
            ' /C parameter means exiting Cmd.exe after executing command     
        End Sub 
     
    End Class 




    3. You can use the remote desktop control named msrdp.ocx (that is Microsoft Terminal Sevices control) to connect to remote desktop.

    Right-click on Toolbox in Visual Studio 2005/2008 -> Choose Items -> COM Components tab -> Locate the "Microsoft Terminal Services Control" and add it.

    Then the control named "
    Microsoft Terminal Services Control
    " will be added onto the Toolbox. If you drag&drop it onto Form, the AxMsTscAxNotSafeForScripting1 object will be generated automatically. 
    You can see these properties of the control in Properties pane and 
    play with them to see if they can achieve your goal.

    Code sample: Call "Microsoft Terminal Services Control" to connect remote workstation desktop in VB.NET.

    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            AxMsTscAxNotSafeForScripting1.Server = "MartinXie" 
            AxMsTscAxNotSafeForScripting1.Domain = "fareast" 
            AxMsTscAxNotSafeForScripting1.UserName = "v-maxie" 
            AxMsTscAxNotSafeForScripting1.Connect()  
        End Sub 
     
    End Class 

    Here is the illustration:
     

    Trackback: 
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ee5e2b11-e36f-477c-8476-a88c11e79242/



    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 24, 2009 1:42 PM
  • Hi Martin,

        Thanks for your reply. Please find the requirements below,

     Consider the System A was logged off. I want to start the system from my vb.net application which resides in System B. Is it possible to start the system from other system.

     From the code above, we can collect the information of System A from system B. But i want to start the system. Also, it is an automation application and i dont want any GUI interface. Actually my application looks like a service. This service is used to kill some process in System A , delete some log files , update some registry and logoff and login the system . This service is running from System B.

    Please let me know that did you got my application requirements.

    Thanks & Regards,
    Balaji R 
    Balaji
    Wednesday, March 25, 2009 1:54 PM
  • Hi Balaji,

    You can shut down or restart a remote machine from local machine via executing the command line tool shutdown.exe with arguments in VB.NET. 

    http://support.microsoft.com/kb/317371

    Code sample:

    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            'Execute command with arguments in Dos command line  
            Process.Start("Cmd.exe""/C shutdown.exe \\RemoteComputerName /r")  
            ' /C parameter means exiting Cmd.exe after executing command     
        End Sub 
     
    End Class 


    Tip:
    Start menu -> Run -> Type "cmd", then press Enter key to Command Line Prompt -> Execute "shutdown ?", you will gain more help about parameters.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, March 26, 2009 11:33 AM
  • Hi Martin,

        Thanks for your reply. Actually, i dont want to restart the machine, i want to logoff and login the system from vb.net application. 

       Also, can you please help me about how to delete the files in remote system but i dont want to use share path in my code. Is it possible to execute DOS commands in remote machine.

    Regards,
    Balaji R   

    Balaji
    Wednesday, April 1, 2009 6:24 AM
  • hi,

         I used RDP file to login to the server remotely. I want to hide the windows login dialog box. If i used to connect the server through code with credentials, it will automatically connect to the system. 

     My clear requirement is logoff and login to the remote system from my application. Can you please guide me how to do this? This is very urgent requirement.
    Balaji
    Monday, April 20, 2009 9:07 AM
  • I am also having this exact same problem, I have made a windows service with VB.Net, I need a way to login to the user desktop account (just like if a user was at the terminal), I also need to be able to logoff and switch user if possible.

    Has no one come up with a way to do this????

    Please help!
    Wednesday, July 1, 2009 10:05 PM
  • Public Class Form1  
     
      Private MyPassword as String = "A_Remote_User_Password_1"
      Private MyUser as String = "myuseraccount@mydotteddomain"
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
        'Execute command with arguments in Dos command line  
        Process.Start("Cmd.exe", " /C NET USE X: <a href="file://\\RemoteComputerName\RemoteShareName">\\RemoteComputerName\RemoteShareName</a> " & Mypassword & "/USER:" & MyUser )  
        ' /C parameter means exiting Cmd.exe after executing command   
      End Sub 
     
    End Class 
    
    

    After you connected a vistual disk to a remote computer share name, tou can see an open session on the remote computer.

    Use The NET command to connect/disconnect to/from the remote computer.

    Mybe this helps.

    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            'Execute command with arguments in Dos command line  
            Process.Start("Cmd.exe""/C shutdown.exe \\RemoteComputerName /r")  
            ' /C parameter means exiting Cmd.exe after executing command     
        End Sub 
     
    End Class

    Saturday, July 17, 2010 5:04 PM
  • Public Class Form1 
     
     Private MyPassword as String = "A_Remote_User_Password_1"
     Private MyUser as String = "myuseraccount@mydotteddomain"
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     'Execute command with arguments in Dos command line 
     Process.Start("Cmd.exe", " /C NET USE X: "\\RemoteComputerName\RemoteShareName" & Mypassword & "/USER:" & MyUser ) 
     ' /C parameter means exiting Cmd.exe after executing command 
     End Sub 
     
    End Class 
    
    

    After you connected a vistual disk to a remote computer share name, tou can see an open session on the remote computer.

    Use The NET command to connect/disconnect to/from the remote computer.

    Mybe this helps.

     

    Saturday, July 17, 2010 7:36 PM
  • Hi Martin,

    I am using Windows Management Instrumentation (WMI) to connect to a remote system. I am connecting to a remote system and executing an exe on the remote system. This works if it is a normal exe.

    But when I use the same code in a Windows Service, I get an error which says "Access is Denied." My requirement is to invoke the exe (every 2 hours) on client system from a windows service on the server.

    Looking for some help on this.

    Ravi


    Ravi
    Wednesday, October 6, 2010 7:01 AM
  • Hey Ravimama

    Could you please tell me how you wrote a the program to excute a file on a remote system.

    Saturday, May 14, 2011 6:10 AM
  • Hi Ravi ,

    will you help me about the vb.net code to access the browser history of a remote computer connected in Lan "192.168.x.x "

    Friday, May 11, 2012 4:06 AM
  • Hi Martin

    will you help me about the vb.net code to access the browser history of a remote computer connected in Lan "192.168.x.x "

    Friday, May 11, 2012 4:08 AM