Solved

Help with Get-ADUser PowerShell Command querying for TsAllowLogon

Posted on 2013-01-29
12
2,846 Views
Last Modified: 2013-01-30
Greetings!

I'm trying to query users from an AD OU specifically looking for attribute TsAllowLogon.  My attempts at running a command in PowerShell have been unsuccessful.  Any help were I'm messing up this command would be greatly appreciated.  Usually it just generates an empty CSV file.

Get-ADUser -ResultSetSize $null -SearchBase $ou -SearchScope SubTree -LdapFilter "(TsAllowLogon=$False)" | Select-Object Name,samAccountName | Export-Csv -NoTypeInformation $("C:\results\" + $name + "TsAllowLogonFalse.csv")

Open in new window

0
Comment
Question by:tonydotigr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 40

Expert Comment

by:footech
ID: 38832399
I don't know about the ldapfilter syntax, but this return the right users for me.
Get-ADuser -filter {msTSAllowLogon -eq $true}

Open in new window

0
 
LVL 5

Expert Comment

by:coraxal
ID: 38832499
This will get you users with the msTSAllowLogon not set
Get-ADuser -Properties msTSAllowLogon -Filter { mstsallowlogon -notlike "*" } -SearchBase $ou -SearchScope SubTree -ResultSetSize $null |
Select-Object Name,samAccountName |
Export-Csv -NoTypeInformation $("C:\results\" + $name + "TsAllowLogonFalse.csv")

Open in new window

And this will get you users with the msTSAllowLogon set to $false
Get-ADuser -Properties msTSAllowLogon -Filter { mstsallowlogon -eq $false } -SearchBase $ou -SearchScope SubTree -ResultSetSize $null |
Select-Object Name,samAccountName |
Export-Csv -NoTypeInformation $("C:\results\" + $name + "TsAllowLogonFalse.csv")

Open in new window

Obviously you can combine the filter if you want to get both user types exported
-Filter { mstsallowlogon -notlike "*" -or mstsallowlogon -eq $false }

Open in new window

0
 

Author Comment

by:tonydotigr
ID: 38832507
The command runs, but does not return an results with "$true" or "$false"...
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:tonydotigr
ID: 38832526
mstsallowlogon -eq $false

Open in new window

does not return results either way (even with true)
mstsallowlogon -notlike "*" 

Open in new window

seems to return all records

Hmmmm
0
 
LVL 40

Expert Comment

by:footech
ID: 38832548
I found the right syntax for the LDAPfilter is
Get-ADUser -ldapfilter "(msTSAllowLogon=FALSE)"
In this case "FALSE" (or "TRUE") is case-sensitive.  So changing your supplied command to
Get-ADUser -ResultSetSize $null -SearchBase $ou -SearchScope SubTree -LdapFilter "(msTsAllowLogon=FALSE)" | Select-Object Name,samAccountName | Export-Csv -NoTypeInformation $("C:\results\" + $name + "TsAllowLogonFalse.csv")

Open in new window

works for me.
0
 
LVL 5

Expert Comment

by:coraxal
ID: 38832551
Can you edit this line
Select-Object Name,samAccountName

to this:

Select-Object Name,samAccountName,msTSAllowLogon

Open in new window

Run the command again, and take a look at the value of msTSAllowLogon. Works for me.
0
 

Author Comment

by:tonydotigr
ID: 38832707
Footech:
The command you supplies runs but nothing is returned...


Coraxal:
Nothing is return when I look at the msTsAllowLogon parameter.  I'm guessing there should be?
0
 
LVL 40

Expert Comment

by:footech
ID: 38832755
If that's true, then the problem must be with your environment or I'm misunderstanding what you want.  Do any of your users have the attribute set to FALSE?  If the attribute is not set, then accordingly the file should be blank.

What exactly are you trying to check?  Whether the attribute is set to FALSE, whether it is set at all, or what?
0
 

Author Comment

by:tonydotigr
ID: 38832773
Trying to see what users have the option selected (Remote Desktop Service Profile > Deny this user permissions to log on to Remote Desktop Session Host server).  

Results come back blank when set to either TRUE or FALSE.
0
 
LVL 40

Expert Comment

by:footech
ID: 38832977
OK, we're talking about a different attribute here.  From what I recall, that doesn't exist as an actual attribute of a user in AD, but is pulled from some other source.  You can access it through the userParameters attribute which is basically a blob of information that includes the TerminalServicesHomeDirectory and TerminalServicesProfilePath.  I'm not certain how I'd access it using straight PowerShell (I know it involves using the adsi accelerator).  From what I understand, the Quest cmdlets include the functionality to change this ( http://ss64.com/ps/set-qaduser.html ), but I don't use them so I can't give you specific guidance.  If I have time I'll look into the straight PS method.
0
 

Author Comment

by:tonydotigr
ID: 38833056
Thanks footech... really appreciate it!!
0
 
LVL 40

Accepted Solution

by:
footech earned 420 total points
ID: 38833548
This will return all users in an OU that have the box checked for "Deny this user permissions to log on to Remote Desktop Session Host server".  It will output their name and sAMAccountName into a single .CSV.
Import-Module ActiveDirectory
$out = @()
$searchOU = "OU=Users,OU=whatever,DC=domain,DC=com"
(Get-ADuser -filter * -searchbase $searchOU -properties DistinguishedName) | ForEach `
  {
    $userDN = $_ | Select -expandProperty DistinguishedName
    $user = [adsi]"LDAP://$userDN"
    If (($user.psbase.invokeget("AllowLogon")) -eq "0" )
    {
      $Properties = @{
        Name = "$($_.Name)"
        sAMAccountName = "$($_.sAMAccountName)"
      }
      $custom = New-Object PSObject -property $Properties
      $out += $custom
    }
  }
$out | Select Name,sAMAccountName | Export-Csv -NoTypeInformation "C:\results\TsAllowLogonFalse.csv"

Open in new window

0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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