Solved

VB.net and WMI logoff remote user

Posted on 2014-10-19
9
498 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 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

14 Experts available now in Live!

Get 1:1 Help Now