Getting Domain for User with DSQuery | DSGet - batch-file

Using DSQuery and DSGet to get user attributes that are needed to make accounts (not associated with their Windows account). The current command I've got is:
dsquery * -filter "(&(objectCategory=Person)(objectclass=User)(mail=first.last#email.com))" | dsget user -samid -fn -ln -office -email > user.txt
which gets me all the information I need except the domain the user is associated with. There's the attribute in AD called User logon name (pre-Windows 2000) (first box), but as far as I can tell DSGet doesn't return that. I know the DSQuery can get me the groups the user is in but I'm not sure if there's an easy (one-liner) that can get the info needed. Any guidance or help is appreciated.

There is no attribute User logon name (pre-Windows 2000) in Attribute Editor so there is another way we can get the user logon name using distinguish name (DN) and UPN(User logon Name)
please use the below command to get the DN
This is for user
dsquery * -filter "(&(objectCategory=Person)(objectclass=user)(mail=first.last#microsoft.com))" | dsget user "CN=RahulShaw,CN=Users,DC=microsoft,DC=com" -samid -fn -ln -office -email -DN -UPN > Ansuman.txt
This is under Domain Users
dsquery * -filter "(&(objectCategory=Group)(objectclass=group)(mail=first.last#microsoft.com))" | dsget group "CN=Domain Users,CN=Users,DC=microsoft,DC=com" -samid -DN > ipsita.txt

Related

Create a hidden AAD group

How do I create a hidden AAD group (number of users should be listed as 0) in a demo tenant?
I created a hidden group via
New-UnifiedGroup -DisplayName "group-name" HiddenGroupMembershipEnabled
but I still see all the members in the group. What am I missing?
To create hidden Azure AD Group, you can make use of below command:
New-AzureADMSGroup -DisplayName "Group_Name" -groupTypes "Unified" -Visibility "HiddenMembership" -SecurityEnabled $False -MailEnabled $True -MailNickname "mail_name"
Make sure to have AzureADPreview module installed before running the above command.
If you created Azure AD group with "HiddenMembership", the valid users who can see that group members are:
Group Owner.
Group Members.
Users who have admin roles.
Other than the above, if anyone tried to fetch the group members, they won't get any list of group members (same as 0 users).
I tried to reproduce the same in my environment and got the below results:
I created one Azure AD group with "HiddenMembership" and added few members using below script:
Connect-AzureAD
New-AzureADMSGroup -DisplayName "HiddenMem_Group" -Description "Hidden Members Group" -groupTypes "Unified" -Visibility "HiddenMembership" -SecurityEnabled $False -MailEnabled $True -MailNickname "hide_mem"
Add-AzureADGroupMember -ObjectId "GroupID" -RefObjectId "User_ObjectID"
Response:
Please note that, the user who created group will be Group Owner of that group by default like below:
As Testdemo is Group Owner, he can get the list of group members using command like below:
Get-AzureADGroupMember -ObjectId <Group_ObjectID>
Response:
When a normal user with no admin roles and not a member of that group runs the same command, he won't get any response (same as 0 users) like below:
Get-AzureADGroupMember -ObjectId <Group_ObjectID>
Response:
Reference:
New-AzureADMSGroup (AzureAD) | Microsoft Docs

Get List of servers part of AD group

How is it possible to get the list of servers which are assigned to a group or nested group?
Example:
Group A is assigned to the server A.Group B is assigned to server B.
User1 and user2 is a member of Group12. Then group12 is added as a member of Group A and Group B.
Now I want to get the list of servers user1 and user2 has access.
I am able to retrieve the groups each user is member of. But still I do not know which servers are added to Group12 for example.
Expected Outcome
Some command -GroupID Group12
Output --> ServerA and Server2
Some command -UserName user1
Output --> ServerA and ServerB
From what I could understand from your query, you want to retrieve the members of groups in AD.
You can use the PowerShell cmdlet Get-ADGroupMember to query the members of a group, as shown below:
# you need to have RSAT (Remote Server Administration Tools) module installed
# on the system where you're going to run below cmdlets in PowerShell
Import-Module ActiveDirectory
Get-ADGroupMember -Identity "GroupA" -Recursive # to get members of Group A
Get-ADGroupMember -Identity "GroupB" -Recursive # to get members of Group B
Get-ADGroupMember -Identity "Group12" -Recursive # to get members of Group 12

LDAP query to get account name from SID

So I have a SID of a FSP: S-1-5-21-2127521184-1604012920-1887927527-72713.
Translation worked in powershell but I would like to do the ldap query by myself, like here but have a little trouble with proper SID conversion.
Could you help me with query that give me a corresponding account name based on SID ?
You can bind directly to an object using the SID using LDAP://<SID=S-1-5-21-2127521184-1604012920-1887927527-72713>. Then get the username after that.
In PowerShell, it would look something like:
$account = [adsi]"LDAP://<SID=S-1-5-21-2127521184-1604012920-1887927527-72713>"
$username = $account.Properties["sAMAccountName"]
If the computer you run this from is on a different domain than the account, you may have to specify the domain:
$account = [adsi]"LDAP://domain.com/<SID=S-1-5-21-2127521184-1604012920-1887927527-72713>"
If you have Java available you can query the ObjectSID directly.
We show an Example with code
I am able to use an ldapsearch like:
ldapsearch -h example.net -D "EXAMPLE\myID" -b "OU=Accounts,DC=EXAMPLE,DC=NET" -s sub -a search -z 1000 "(ObjectSID=S-1-5-21-333675845-1535931152-1111140340-22234762)" "objectClass"
And get results.
# extended LDIF
# LDAPv3
# base <OU=Accounts,DC=EXAMPLE,DC=NET> with scope subtree
# filter: (ObjectSID=S-1-5-21-333675845-1535931152-1111140340-22234762)
# requesting: objectClass samAccountName
#
# userid, sales, Accounts, EXAMPLE.NET
dn: CN=userid,OU=sales,OU=Accounts,DC=EXAMPLE,DC=NET
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
sAMAccountName: userid
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
This search is done from a Linux machine and done by a user that is not represented by the ObjectSID.

How to get lastLogonTimestamp from all DCs for specific users

I'm trying to make a script for query all the users from specific users in my domain and get the last "lastLogonTimestamp" from each user , and export the result to a csv file . eg : "SOL*" for users beginning SOL
I obtain the results for most users for "last logon date" the date 31/12/1600 06:00:00 p.m but i dont know why for some users get the correct date and for other for users the date of the year 1600, regardless if they are in the same OU or not.
Get-ADDomainController -filter * |
% {Get-ADUser -Filter "Enabled -eq 'True' -and SamAccountName -like 'SOL*'" -server $_.name -Properties Name,SamAccountName,Description,EmployeeID,EmployeeNumber,EmailAddress,LastLogon,Manager,Title,Department,Organization,Enabled -SearchBase "OU=users,DC=contoso,DC=local" |
Select Name,SamAccountName,Description,EmployeeID,EmployeeNumber,EmailAddress,#{N='Last‌​Logon'; E={[DateTime]::FromFileTime($_.LastLogon)}},Manager,Title,Department,Organizatio‌​n,Enabled}|
Group samaccountname |
ForEach{$_.Group | Sort LastLogon -Descending | Select -First 1} |
Export-Csv "C:\export\AD_Export.csv" -NoTypeInformation
That means that user has never logon using that particular DC.
For example, an employee in US will probably never contact the Europe DC during logon. In this case the lastlogon on Europe DC will be "31/12/1600 06:00:00 p.m" (min value of lastlogon is January 1, 1601 (UTC), the time you saw is "time-zoned").
One more thing, lastLogonTimestamp and lastLogon are 2 different attributes.
lastLogon record the exact logon time but is not replicated among DC.
lastLogonTimestamp is replicated, but it is only an approximation.
(by default it can be <= 14 days earlier than actual value, only useful for finding idle account...)
In your case, you are actually talking about lastLogon.

How to filter AD with a concatenated variable in Powershell

I'm using PowerShell v2 and Microsoft's AD module to search our AD for accounts whose EmployeeID match a particular ID. The ID is usually stored in AD as "00000123456" but the value i have to search with is only the "123456" part. problem is i cannot figure out how to do a -like search in AD. here's my current code
$EmpInfo = Import-csv "PSfile.csv"
$EmplID = EmpInfo.ID
$EmpAD = get-aduser -Filter {employeeId -like "*$EmplID"} -Properties * -EA Stop
At this point, EmpAD is always empty
I can work around this by modifying EmpID to contain "*123456" before I call Get-ADUser and this works. But I can't help but think there is a syntax problem preventing the obvious approach. Research to resolve it has been fruitless.
If your string is really in employeeID attribute you can test :
$EmpAD = get-aduser -LDAPFilter "(employeeId=*$EmplID)" -SearchBase 'DC=dom,DC=fr' -Properties *
you can use LDP.EXE (or ADSI.EXE) to verify what exactly exists your Directory.
-----Edited-----
For me it works, if I test with LDIF:
C:\temp>ldifde -f eid.ldf -d "dc=dom,dc=fr" -r "(employeeId=*)"
Connexion à « WM2008R2ENT.dom.fr » en cours
Connexion en tant qu'utilisateur actuel en utilisant SSPI
Exportation de l'annuaire dans le fichier eid.ldf
Recherche des entrées...
Création des entrées...
3 entrées exportées
There are 3 objects
In PowerShell with AD Cmdlets it gives the following :
PS C:\> get-aduser -LDAPFilter "(employeeID=*)" | Measure-Object
Count : 3
And
$var = "123456"
PS C:\> get-aduser -LDAPFilter "(employeeID=*$var)" -properties employeeID
DistinguishedName : CN=user1 Users,OU=MonOu,DC=dom,DC=fr
EmployeeID : 00000123456
Enabled : True
GivenName : user1
Name : user1 Users
ObjectClass : user
ObjectGUID : b5e5ea59-93a6-4b24-9c3e-043a825c412e
SamAccountName : user1
SID : S-1-5-21-3115856885-816991240-3296679909-1107
Surname : Users
UserPrincipalName : user1#dom.fr
Be carefull : I don't understand why, but it took some time between the modification in the directory with MMC and the result in the PowerShell prompt. I reload a new PowerShell interpreter and re import AD module.
From a performance perspective, if you know that the IDs are always a certain number of digits (with leading zeroes), you're going to be WAY better off just formatting the ID ahead of time.
If your ID is supposed to be 11 digits, do something like this $EmplID.ToString("D11") to get it padded out.

Resources