Kaseya Community

Script to log off machines

  • Procedure Force User Logoff.xml

    Hello,

    We are using an agent procedures (Please find attached) for logoff machines but this is not working even the result shows (script has been successfully run on machine). Please have a look at the procedure and let us know what exact issue that we are getting due to which script is not working on machines.

    We tried the below procedure as well but this is also not working for us. Please guide us in right direction.



    Added Attachment
    [edited by: Mohit at 5:42 AM (GMT -8) on Jan 12, 2016]
  • Procedure USP - Logoff Current Console Session.zip

    Try these (see attached zip file), they will only work on Windows machines and on console sessions only.

    There is a known issue around logged on user detection if the "KaUsrTsk.exe" kaseya agent process is not running, to get around it I used WMI to check what user is logged in. The attached scripts uses both the inbuilt Kaseya procedure logoffCurrentUser command and the WMI logged in user check as a failover.


    Hope it works for you.

  • Hi Team,

    Thank you for the sharing the details. Provided script is working for us although this is only working on console session and not working in case we have a RDP session logged in.

    Do we have similar script available through which we can log off our specific user even if we are logged in through RDP?

    We also wanted to confirm if we can go ahead and remove notification option and go straight to logging off user without a waiting period?

    Looking forward for your response. Thank you

  • Hi Team,

    Thank you for the sharing the details. Provided script is working for us although this is only working on console session and not working in case we have a RDP session logged in.

    Do we have similar script available through which we can log off our specific user even if we are logged in through RDP?

    We also wanted to confirm if we can go ahead and remove notification option and go straight to logging off user without a waiting period?

    Looking forward for your response. Thank you

  • Hello Mohit,

    How about this? (if windows is all you need)

    First copy this PowerShell script (Get-LoggedOnUser.ps1) from here

    gallery.technet.microsoft.com/.../Get-LoggedOnUser-Gathers-7cbe93ea

    Then assuming the script has been copied in E:\ you can add your ExecutePowerShell step in your procedure with this as command:

    e:\Get-LoggedOnUser.ps1 -ComputerName yourserver| Where-Object {$_.UserName -eq 'Administrator'} | ForEach-Object { rwinsta $_.id /server:yourserver}

    Replace "Administrator" and "yourserver" with your own user or server (or have it parametrized by passing relevant Kaseya View Variables)

    You can also log off Everybody in one shot:

    e:\Get-LoggedOnUser.ps1 -ComputerName yourserver| ForEach-Object { rwinsta $_.id /server:yourserver}

    Regards

  • Hello Mohit,

    Since we ended up needing something similar for our purposes, I managed to optimize the whole disconnection by removing the need of upload of the Powershell script:

    You can now add a simple Execute Powershell step in a Kaseya Procedure and do the following:

    Add this to disconnect all users of the server where you run the procedure:

    quser /server:#vMachine.ComputerName# | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:xavda01} else {rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#}}

    This disconnects only "disconnected" sessions (useful for Citrix session that remain stuck which is what we use for).

    quser /server:#vMachine.ComputerName# | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:xavda01}}

    Regards

  • Hi Alessandro,

    Thank you for your inputs.

    This is sufficient in case we would like to logoff all the users.

    In this case we would only like to logoff only a specific user like earlier suggested from your end. Can we do something similar directly in Kaseya?

  • Hello Mohit,

    Just pass to the quser function a single user you want to disconnect (whether connected or left in disconnected state) such as below:

    quser /server:#vMachine.ComputerName# #WhateverUserVariable# | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#} else {rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#}}

    As you can see the quser is now accepting 2 parameters:

    The server name (taken from the Kaseya View)

    The user name (I have used a sample variable but you could add a constant or take the value from whatever you like).

    For a constant based example:

    quser /server:#vMachine.ComputerName# alessandrodimarco | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#} else {rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#}}

    You could also get the "current user name" as follows:

    quser /server:#vMachine.ComputerName# [Environment]::UserName | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#} else {rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#}}

    And in this case you would want to run the one liner with the Agent Procedure as USER:

    ExecutePowerShellCommand32User / ExecutePowerShellCommand64User

    in a similar fashion you were doing at the beginning

    Best Regards

  • Hi Alessandro,

    We are not able to execute the following command.

    Do we only have to change the variable #WhateverUserVariable# with #Username#(that we need to log off) or do we need to have any other variable a well.

    Can i just execute this with execute powershell directly onto a machine??

  • Hello Mohit,

    Without a detail of the error it is difficult for me to guess what went wrong in your environment.

    You can run this in Powershell directly but you have to replace:

    #vMachine.ComputerName# with a Computer Name you want the command to run (a View Variable is only usable in Kaseya obviously)

    [Environment]::UserName with the user name you want to log off. Note this is a Powershell global variable so if you are happy to logoff the current user of a remote server you can also leave that as is.

    quser /server:#vMachine.ComputerName# [Environment]::UserName | Select-Object -Skip 1 | ForEach-Object { if (($_.Trim() -Split '\s+')[2] -eq "Disc"){rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#} else {rwinsta ($_.Trim() -Split '\s+')[1] /server:#vMachine.ComputerName#}}

    If you are still in trouble I would need an error to help you further.

    Best Regards