Solved

VB.net and WMI logoff remote user

Posted on 2014-10-19
9
481 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
Comment Utility
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
Comment Utility
WMI is not required, i tried your solution and get a "expression expected" over the @ symbol...
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Sorry that was C#. Try

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

Author Comment

by:derek7467
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
String.Format arguments are 0-based:

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

Author Comment

by:derek7467
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This was the best option i was able to find.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now