locked
Extend a Technet RDS 2012 Powershell script? RRS feed

  • Question

  • Hi,

    I found a greate script here: https://gallery.technet.microsoft.com/scriptcenter/Remotely-log-users-off-377c848d

    but would like to add the servername in the GUI, so you can see which server a user logged onto.

    Please advise howto.

    J.


    Jan Hoedt

    Thursday, June 18, 2015 8:54 AM

Answers

  • This is all you need:

    $loggedonusers = Get-RDUserSession -ConnectionBroker $connectionBroker -CollectionName $SessionHostCollection
    ForEach ($user in $loggedonusers) {
    	Write-Host ($user.username + " - " + $user.HostServer) -fore green
    	[void]$objListBox.Items.Add($user.username + " - " + $user.HostServer)
    }

    What you need to do is to learn how to use PowerShell and how to test what you are doing. Using Write-Host provides pretty good way to trace things.


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, June 22, 2015 12:30 PM
    Friday, June 19, 2015 6:01 PM

All replies

  • That is not a question it is a request.  Post Requests here: https://gallery.technet.microsoft.com/scriptcenter/site/requests

    The bottom of the page has a script request form.


    \_(ツ)_/


    • Edited by jrv Thursday, June 18, 2015 9:41 AM
    Thursday, June 18, 2015 9:41 AM
  • Hi Jan,

    As suggested by jrv, also post the script request link so that we can post the updated script there.

    Replace this section on the script. It should show the "users - servername" format inside the section.

    #Find logged in users and display them in the form  
        $loggedonusers = Get-RDUserSession -ConnectionBroker "$connectionBroker" -CollectionName "$SessionHostCollection" 
    
        ForEach ($user in $loggedonusers) 
    
        { 
    
        $UsrSrv = $user.username.ToString() +" - " + $user.HostServer.ToString()
    
    #    [void] $objListBox.Items.Add($user.username) 
    #    [void] $objListBox.Items.Add($user.HostServer)
    
        [void] $objListBox.Items.Add($UsrSrv)
        } 


    Same updated on the gallery post.

    https://gallery.technet.microsoft.com/scriptcenter/Remotely-log-users-off-377c848d/view/Discussions

    References:

    ListBox.ObjectCollection.Add Method

    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Thursday, June 18, 2015 10:43 AM
    Thursday, June 18, 2015 10:34 AM
  • Thanks, however, server still doesn't show up :-(

    Jan Hoedt

    Thursday, June 18, 2015 11:57 AM
  • Hi Jan,

    Do you see the "-" atleast after the username. Run it again without any changes.

    If no result try these out.

    Make this change just to test, let me know what you see.

    #Find logged in users and display them in the form  
        $loggedonusers = Get-RDUserSession -ConnectionBroker "$connectionBroker" -CollectionName "$SessionHostCollection" 
    
        ForEach ($user in $loggedonusers) 
    
        { 
    
    #    $UsrSrv = $user.username.ToString() +" - " + $user.HostServer.ToString()
    
        [void] $objListBox.Items.Add($user.username) 
        [void] $objListBox.Items.Add($user.HostServer)
    
    #    [void] $objListBox.Items.Add($UsrSrv)
    
        } 

    Its all about capturing the servername from the Usersession object and concatenating it with the username to display.

    So lets test if you can pull the servername correctly or not using simple cmdlets.

    Update the ConnectionBroker ,CollectionName and User1 with valid values and see if it gives you the Servername.

    $UserSession = Get-RDUserSession -ConnectionBroker "connectionbroker" -CollectionName "sessionhostcollection" | Where-Object {$_.username -eq "User1"} 
    Write-Host $UserSession.HostServer 

    NOTE:- The script has a bug, you need to run it twice for the changes to take effect. To fix this move the below section to end of the script.

    #Runs the script
    SetupForm



    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    Friday, June 19, 2015 6:47 AM
  • The scripts don't give the servername, even no -.

    However, the $Usersession works fine, it shows the server to which the user is logged on!


    Jan Hoedt

    Friday, June 19, 2015 3:13 PM
  • This is all you need:

    $loggedonusers = Get-RDUserSession -ConnectionBroker $connectionBroker -CollectionName $SessionHostCollection
    ForEach ($user in $loggedonusers) {
    	Write-Host ($user.username + " - " + $user.HostServer) -fore green
    	[void]$objListBox.Items.Add($user.username + " - " + $user.HostServer)
    }

    What you need to do is to learn how to use PowerShell and how to test what you are doing. Using Write-Host provides pretty good way to trace things.


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, June 22, 2015 12:30 PM
    Friday, June 19, 2015 6:01 PM
  • Thanks, I'll try asap.

    > What you need to do is to learn how to use PowerShell and how to test what you are doing.

    I know, I'm working on that, but you have to start somewhere right? By modifying existing scripts, I do learn a lot. The tip about write-host is appreciated.


    Jan Hoedt

    Sunday, June 21, 2015 6:36 AM
  • Hi Jan,

    $Usersession works fine would mean there is something else not running quite right on the script.

    Please post the last script you ran for review. Along with the Output screenshot, you may cutout the names or replace them with something else to let us get a feel of what you are seeing.


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Monday, June 22, 2015 3:56 AM
  • Thanks!

    Changing

    [void]$objListBox.Items.Add($user.username + " - " + $user.HostServer)

    is indeed the only thing that needs to be changed!


    Jan Hoedt

    Monday, June 22, 2015 12:31 PM
  • Note: would there be an easy way to sort on $user.HostServer?

    Jan Hoedt

    Monday, June 22, 2015 12:34 PM
  • If you need more complex behavior you should be using a ListView control.  That is in part, why it exists.  you can also bind a sorted CSV as data and it will do what  you need.


    \_(ツ)_/

    Monday, June 22, 2015 1:21 PM