none
I need to convert a VBscript to VB.Net RRS feed

  • Question

  • I have this vbscript code that tells you when if and who is logged into a remote PC. Usually in VB6 I could just paste it in and it would work just changing the input and outputs. I'm pretty new to VB.Net and I did try searching for similar code but I found nothing I could use. Can someone convert this script to a sample code.

    strComputer = InputBox("Please enter computer name to check for currently logged in user activity:")

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery("Select * from win32_ComputerSystem",,48)For Each objItem in ColItems

     MsgBox "Logged on user: " & StrComputer & ". Result:  " & objItem.username

    Next

    

    Thursday, June 29, 2017 3:06 PM

All replies

  • Yes in VB.Net that is easier

    MsgBox("Logged on user: " & environment.username)


    Success
    Cor

    • Proposed as answer by Cor Ligthert Thursday, June 29, 2017 3:23 PM
    • Unproposed as answer by Cor Ligthert Thursday, June 29, 2017 3:23 PM
    Thursday, June 29, 2017 3:19 PM
  • OT

    The proposal was just to see if the reply would show up in the thread and it did while it was unshown before. 

    Is this bug already reported?


    Success
    Cor

    Thursday, June 29, 2017 3:24 PM
  • That's great but I need it to do a remote PC.
    Thursday, June 29, 2017 3:31 PM
  • That's great but I need it to do a remote PC.

    What do you mean with a remote PC. I thought that it was a PC bound by terminal services. Then what I wrote it is the cause. 

    In other circumstances it fall in the context of code usable for Malicious intent, and then it is forbidden to give help for that in the context of this forum. 

    Be aware the later is not about what you gonna do with it, it is just that it can be used by everybody who finds it. 


    Success
    Cor


    Thursday, June 29, 2017 3:58 PM
  • For example :

    Dim sComputer As String = "your_computer_name"
    Dim cs As New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem Where Name='" & sComputer & "'")
    For Each objMgmt In cs.Get
        Dim sUsername As String = objMgmt("UserName").ToString()
        MessageBox.Show("Logged on user: " & sUsername)
    Next

    Thursday, June 29, 2017 4:06 PM
  • I'm using this to create a program that reads a list of computer names and then tells me if someone is logged on or not so I don't waste my time trying to RDC into it first. All I want is if someone is currently logged into the machine.
    Thursday, June 29, 2017 6:00 PM
  • I'm using this to create a program that reads a list of computer names and then tells me if someone is logged on or not so I don't waste my time trying to RDC into it first. All I want is if someone is currently logged into the machine.

    Yes that we understood, but how do you come on the other ones computers.

    The only one I see is if you are a LAN administrator and have free access to the Active Directory, but then you should use the given tools of an Administrator. 


    Success
    Cor

    Thursday, June 29, 2017 9:52 PM
  • All I want is if someone is currently logged into the machine.

    Start here for how to use WMI in .Net:
    https://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx
    Windows Management Instrumentation

    The command is essentially the same as you already have, but creating the object is different.

    Thursday, June 29, 2017 10:05 PM
  • Hi Acamar,

    You make me curious. Of course I know WMI, there is even a simple Microsoft tool to make code with that. But AFAIK you can only use that if you are already logged on in the computer as the current user. 

    Maybe can you enlighten this without giving code?


    Success
    Cor

    Thursday, June 29, 2017 10:12 PM
  • You make me curious. Of course I know WMI, there is even a simple Microsoft tool to make code with that. But AFAIK you can only use that if you are already logged on in the computer as the current user.

    It will fail if the caller has insufficient permissions.  But if OP can do it with the VBA script then that suggests the security, permissions and the relevant DCOM settings are in place, and it can be converted to .Net.

    This is the MS example:

    strComputer = "Computer_B"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    I don't know whether the impersonation is required or not, but I guess it can't harm.
    Thursday, June 29, 2017 10:38 PM
  • I am a deployment engineer and where I work is extremely high security. I am RDC into PC's to install the software. It cannot be pushed out due to security. When RDC into a PC, you have to login in everytime just to see if someone is on and if they are you cannot kick them off, so you have to try again.

    So far none of the scripts here worked. Nothing happens at all. The VBscript I pasted works though. No I can't use any Admin tools, cannot install ANY software on a PC or download anything.

    Friday, June 30, 2017 12:16 PM

  • The VBscript I pasted works though. 

    What is than the trouble using that?

    But maybe can you write down here your name, your last statement sounds at least for me very suspicious especially if it is done from behind a new created nick. 


    Success
    Cor


    Friday, June 30, 2017 12:27 PM
  • You know what, I'll just go somewhere else to find answers, you guys are a little suspicious and it seems your more into accusing people asking for help for a 3 line piece of code. 
    Friday, June 30, 2017 12:43 PM
  • So far none of the scripts here worked.

    The code I posted and others work, because they are adaptations from MSDN code and can be found everywhere on Google.

    (and I always test before posting (on Windows 10, VS 2015))

    Friday, June 30, 2017 12:55 PM