Solved

VB.net and WMI logoff remote user

Posted on 2014-10-19
9
509 Views
Last Modified: 2014-10-25
I am attempting to logoff a remote user using wmi via vb.net. I created the below code in the wmi code creator but it doesn't do anything  it doesn't even error out:

Dim scope As New ManagementScope( _
                "\\192.168.1.105\root\CIMV2")
            scope.Connect()

            Dim classInstance As New ManagementObject(scope, _
                New ManagementPath("Win32_OperatingSystem.ReplaceKeyPropery='ReplaceKeyPropertyValue'"), _
                Nothing)

            ' Obtain [in] parameters for the method
            Dim inParams As ManagementBaseObject = _
                classInstance.GetMethodParameters("Win32Shutdown_OperatingSystem")

            ' Add the input parameters.
            inParams("Flags") = 0

            ' Execute the method and obtain the return values.
            Dim outParams As ManagementBaseObject = _
                classInstance.InvokeMethod("Win32Shutdown_OperatingSystem", inParams, Nothing)

Open in new window

0
Comment
Question by:derek7467
  • 5
  • 2
  • 2
9 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40391722
Is WMI a requirement? If not, you can use following

System.Diagnostics.Process.Start("shutdown.exe", String.Format(@"/l /f /m \\{1}", remoteComputerName));


Also check

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22084990.html
0
 

Author Comment

by:derek7467
ID: 40391826
WMI is not required, i tried your solution and get a "expression expected" over the @ symbol...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40391877
Sorry that was C#. Try

System.Diagnostics.Process.Start("shutdown.exe", String.Format("/l /f /m \\{1}", remoteComputerName))
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:derek7467
ID: 40391943
I get th following when trying to run it:

 Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40392036
String.Format arguments are 0-based:

System.Diagnostics.Process.Start("shutdown.exe", String.Format("/l /f /m \\{0}", remoteComputerName))
0
 

Author Comment

by:derek7467
ID: 40392272
ehh that fixes the error but it doesnt run, it acts like i didnt type any command line switches in.  I thought logoff doesnt work remote using the shutdown command?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40392412
I haven't used the Shutdown command to log off a remote user, but I would have thought that it could work.

Shutdown
http://technet.microsoft.com/en-us/library/bb491003.aspx

-l   : Logs off the current user, this is also the default. -m ComputerName takes precedence.
-m [ \\ ComputerName ] : Specifies the computer that you want to shut down.

You can't use -l and -m together, since -m takes precedence over -l.

It looks like Win32_OperatingSystem.Win32Shutdown method has that capability to log off a user:

Win32Shutdown method of the Win32_OperatingSystem class
http://msdn.microsoft.com/en-us/library/aa394058(v=vs.85).aspx
0
 

Accepted Solution

by:
derek7467 earned 0 total points
ID: 40392517
Thanks for the info, i saw that on google searches and couldnt figure out how to make it for remote use, it looks like its local only the way its written.  Im using psexec now, i open a CMD window locally into the remote user, query session and then logoff the users ID...  That will work for me.
0
 

Author Closing Comment

by:derek7467
ID: 40403707
This was the best option i was able to find.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question