Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

How to filter for null values with Get-ADUser cmdlet

How to filter for null values with Get-ADUser cmdlet using Powershell Active Directory Module

Get-ADUser -Filter {title -notlike "*"}

Fix "System.Object[]" output when exporting to csv

Fix "System.Object[]" output when exporting to csv
export data to csv is sometimes broken because everything that is passed to Export-Csv cmdlet is casted as a string before the export.

one solution is to create a pscustomobject and use -Join operator on the troubling property:


$user = Get-ADUser username -Properties memberof
[pscustomobject]@{
    DN = $user .DistinguishedName
    GivenName = $user .GivenName
    MemberOf = $user .MemberOf -Join ','
    Name = $user .Name
    SamAccountName = $user .SamAccountName
    UserPrincipalName = $user .UserPrincipalName
} | Export-Csv -NoTypeInformation .\user.csv

for a better arrangement Out-String cmdlet can be use in replace of Join operator

$user = Get-ADUser user  -Properties memberof
[pscustomobject]@{
    DN = $user.DistinguishedName
    GivenName = $user.GivenName
    MemberOf = ($user.MemberOf | Out-String).Trim()
    Name = $user.Name
    SamAccountName = $user.SamAccountName
    UserPrincipalName = $user.UserPrincipalName
} | Export-Csv -NoTypeInformation .\user.csv

Active Directory schema attribute details



The Active Directory schema contains formal definitions of every attribute that can exist in an Active Directory object. 

$schema =[DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetCurrentSchema()

$schema.FindClass('user').optionalproperties | Where-Object {$_.name -eq 'employeeid'}

Find All Domain Controllers in an AD Domain:

a) LDAP filter:

Get-ADComputer -LDAPFilter "(&(objectCategory=computer)userAccountControl:1.2.840.113556.1.4.803:=8192))"

ex:-
PS C:\Users> Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))"

DistinguishedName : CN=IHM-CAM-DC-1,OU=Domain Controllers,DC=example,DC=local
DNSHostName       : IHM-CAM-DC-1.example.local
Enabled           : True
Name              : IHM-CAM-DC-1
ObjectClass       : computer
ObjectGUID        : c1eff660-25f9-41de-9e41-acad98908abc
SamAccountName    : IHM-CAM-DC-1$
SID               : S-1-5-21-1370450699-4128765030-226702860-1000
UserPrincipalName :

DistinguishedName : CN=IHM-CAM-DC-2,OU=Domain Controllers,DC=example,DC=local
DNSHostName       : IHM-CAM-DC-2.example.local
Enabled           : True
Name              : IHM-CAM-DC-2
ObjectClass       : computer
ObjectGUID        : 6bcee6cb-2f6f-4ea1-b7ab-842faca4bcd8
SamAccountName    : IHM-CAM-DC-2$
SID               : S-1-5-21-1370450699-4128765030-226702860-1103
UserPrincipalName :

b) "Domain controllers" group and retrieve his members:


Get-ADGroupMember 'Domain Controllers'

ex:-
PS C:\Users> Get-ADGroupMember 'Domain Controllers'
distinguishedName : CN=IHM-PORT-DC-1,OU=Domain Controllers,DC=example,DC=local
name              : IHM-PORT-DC-1
objectClass       : computer
objectGUID        : 0a591ca8-b52a-452b-b68a-629c7b16443b
SamAccountName    : IHM-PORT-DC-1$
SID               : S-1-5-21-1370450699-4128765030-226702860-1104

distinguishedName : CN=IHM-CAM-DC-2,OU=Domain Controllers,DC=example,DC=local
name              : IHM-CAM-DC-2
objectClass       : computer
objectGUID        : 6bcee6cb-2f6f-4ea1-b7ab-842faca4bcd8
SamAccountName    : IHM-CAM-DC-2$
SID               : S-1-5-21-1370450699-4128765030-226702860-1103

c) Get-ADDomainController cmdlet:


Get-ADDomainController -Filter * | Select-Object name

ex:-
PS C:\Users> Get-ADDomainController -Filter * | Select-Object name
name
----
IHM-CAM-DC-1
IHM-CAM-DC-2

Check AD property 'msRTCSIP-BaseSimpleUrl' of abcd@example.com on each domain controller.

One of the way how it can be done is by performing Lync cmdlet:

(Get-CsOnlineUser abcd@example.com -DomainController <DC>).BaseSimpleUrl

ex:
PS C:\Users> (Get-CsOnlineUser abcd@example.com -DomainController IHM-CAM-DC-1.example.com).BaseSimpleUrl
https://meet.lync.xyz.net/example.com/Meet