locked
Powershell: combine two functions? RRS feed

  • Question

  • Hi,

    I got a function which checks which users are logged on to what servers on a RDS 2012 farm, what collection etc.It can run without the –user option or with. When it runs WITH it, it needs to have a samaccountname.

    F.e. Get-RDSUser user1

     

    I have another script which does look for the samaccountname in an easy way

    Get-SamAccountNameUser

    F.e. Get-SamAccountNamUser Doe Will list GivenName Surname  SamAccountName

    John              Doe          JDoe

    Sam               Doe         SDoe

    Ellen              Doeke     Edoek

    Etc

     

     

    I’d like to merge both functions so I could execute

    Get-RDSUser –SearchFor Doe

    It will then list

    Select the user you need:

    GivenName Surname  SamAccountName

    1. John              Doe          JDoe
    2. Sam               Doe         SDoe
    3. Ellen              Doeke     Edoek

     

    Please advise howto do this.

    J.


    Jan Hoedt

    Wednesday, April 20, 2016 3:52 PM

Answers

  • I really try to explain it each time but apparently fail to do correctly.
    Got something working which will clarify. It's the result when the user executes

    Get-RDSUser –SearchFor Doe

    Only items missing for me now is:
    *the numbers before the names, they should start with 0 but fail to add that in the foreach loop
    *nice formatting, it is not at this time, would like to have nice table

             Write-Host 'Please select which user you want to see the RDS sesssion for:'
             Write-Host '`'
             Write-Host 'GivenName,SurName,SamAccountName'
             Write-Host '================================='
             Foreach ($SamAccountName in $SamAccountNames)
             {
                Write-Host $x++ '' $SamAccountName.GivenName '' $SamAccountName.Surname '' $SamAccountName.SamAccountName
             }
             Write-Host '`'
             $Choice = Read-Host 'Select Account by typing the number before it'
             #Select the SamAccountName for the user selected
             $SelectedSamAccountName =  $SamAccountNames[$Choice] | Select-Object -ExpandProperty SamAccountName
             #Get the RDS user sessions details for the user selected
             Get-RDUserSession -ConnectionBroker connectionbroker | Where-Object -Property Username -Like $SelectedSamAccountName

    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 12:18 PM
    Thursday, April 21, 2016 9:59 AM
  • Used this for my AD searchfilter which then creates a nice table. That's all I needed in my script.
    Now I can select a number and it will  run the rds-command for that samaccount.

    Get-ADUser -Filter {
            name -like $NameContainsWildcard
        } | Select-Object -Property @{
            Name       = 'Number'
            Expression = {
                $global:counter; $global:counter++
            }
        }, GivenName, Surname, SamAccountName


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 12:18 PM
    Thursday, April 21, 2016 12:18 PM

All replies

  • Well this forum isn't going to write it for you, which may or may not be what you were hoping for.

    However you might want to start by saying what you actually want to get out of this script, is it using the Get-SamaccountName cmdlet to find partial matches and then use thoes in the next script, or something slightly different.

    It's also best to post what you've got so far, otherwise you won't get any notable help.

    Wednesday, April 20, 2016 4:09 PM
  • Without more information, I'm not sure we'll be able to fully help. If Get-RDSUser can accept more than one user at a time, then you should be able to do something like this:

    PS> Get-RDSUser ((Get-SamAccountNameUser Doe).SamAccountName)

    Otherwise, if it can only accept one SamAccountName at a time, you'll have to pipe each user through a ForEach-Object loop; something like this:

    PS> (Get-SamAccountNameUser Doe).SamAccountName | ForEach-Object {Get-RDSUser -SearchFor $_}



    Wednesday, April 20, 2016 5:09 PM
  • Apparently didn't explain myself correctly.
    Will try to clarify, but just this for now, the user should have a menu build like this.

    $results being the samaccountnames found by get-samaccountnameuser doe

    for each ($result in $results)
    {
     Write-Host $number <should count from 1 to x> $result
    }


    Jan Hoedt

    Wednesday, April 20, 2016 7:11 PM
  • This is what you are saying you want to do,

    Get-AdUser -Filter "surname -eq 'doe'"


    \_(ツ)_/

    Wednesday, April 20, 2016 9:43 PM
  • I really try to explain it each time but apparently fail to do correctly.
    Got something working which will clarify. It's the result when the user executes

    Get-RDSUser –SearchFor Doe

    Only items missing for me now is:
    *the numbers before the names, they should start with 0 but fail to add that in the foreach loop
    *nice formatting, it is not at this time, would like to have nice table

             Write-Host 'Please select which user you want to see the RDS sesssion for:'
             Write-Host '`'
             Write-Host 'GivenName,SurName,SamAccountName'
             Write-Host '================================='
             Foreach ($SamAccountName in $SamAccountNames)
             {
                Write-Host $x++ '' $SamAccountName.GivenName '' $SamAccountName.Surname '' $SamAccountName.SamAccountName
             }
             Write-Host '`'
             $Choice = Read-Host 'Select Account by typing the number before it'
             #Select the SamAccountName for the user selected
             $SelectedSamAccountName =  $SamAccountNames[$Choice] | Select-Object -ExpandProperty SamAccountName
             #Get the RDS user sessions details for the user selected
             Get-RDUserSession -ConnectionBroker connectionbroker | Where-Object -Property Username -Like $SelectedSamAccountName

    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 12:18 PM
    Thursday, April 21, 2016 9:59 AM
  • Found a nicer way, will post it soon.

    Jan Hoedt

    Thursday, April 21, 2016 11:36 AM
  • Used this for my AD searchfilter which then creates a nice table. That's all I needed in my script.
    Now I can select a number and it will  run the rds-command for that samaccount.

    Get-ADUser -Filter {
            name -like $NameContainsWildcard
        } | Select-Object -Property @{
            Name       = 'Number'
            Expression = {
                $global:counter; $global:counter++
            }
        }, GivenName, Surname, SamAccountName


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, April 21, 2016 12:18 PM
    Thursday, April 21, 2016 12:18 PM