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

Tuesday, October 22, 2013

Active Directory Powershell Cmdlets in 2012 R2

Windows Server 2012 R2 was released on Oct 18, 2013.   Last Friday was a big day for everyone in the Microsoft community.   In future blog posts I'll be going over some of the new features available for Active Directory in 2012 R2.   I first want to get to know the features well before I blog about them :)

One area that most Active Directory admins are familiar with is PowerShell.   Not everyone is a PowerShell Expert but I'm seeing a lot of folks trying to learn PowerShell and this is definitely true in the Active Directory community.

Windows 2012 R2 and Windows 8.1 introduced PowerShell version 4.   This blog goes over the various versions of PowerShell and what is included for Active Directory in each version.

It is also important to know that you can run various versions of the AD cmdlets against DCs with the Active Directory Web Services running  Ashley McGlone aka GoateePFE has an excellent blog on how to use the PowerShell v 3.0 cmdlets from Windows 7.   I'd personally use a Windows 8 or 8.1 admin workstation if possible.

PowerShell was known by the Code Name "Monad" and first shown off publically in 2003.  It has come a long way since then.   In PowerShell version 1.0 there were no native Active Directory cmdlets.   Quest released PowerShell cmdlets that worked in version 1.  The Quest cmdlets are still used today and also work in versions 2-4.

Starting with PowerShell version 2 the Microsoft Active Directory team introduced a native AD module. The initial native AD module contains 76 cmdlets and deals with many common tasks that AD admins deal with including object manipulation (users, groups, computers).

For this blog I'm focusing on the ActiveDirectory module and not other modules such as ADDSDeployment, DNS, and GroupPolicy that are also heavily used by AD admins.

Getting the total number of AD cmdlets is a quick one liner:





The picture below is a snapshot of the different versions and what is included in each version.  If anyone wants the slide please let me know and I'll send you the PowerPoint.


Active Directory PowerShell Modules through the years



The 76 Active Directory cmdlets introduced in version 2.0 are listed below.  Jeffrey Snover is the inventor of PowerShell (Thanks!).  He often says his favorite cmdlet is Get-Help.  I  agree with that and find it very useful.  Linux types "man" also works.  I use the example switch the most but you can self discover and learn more about any of these cmdlets.  There is also a lot of great material on the web for learning PowerShell.  I recommend the Microsoft Virtual Academy courses on PowerShell.





ACTIVE DIRECTORY POWERSHELL CMDETS VERSION 276 TOTAL AD CMDLETS IN v2
Add-ADComputerServiceAccountNew-ADGroup
Add-ADDomainControllerPasswordReplicationPolicyNew-ADObject
Add-ADFineGrainedPasswordPolicySubjectNew-ADOrganizationalUnit
Add-ADGroupMemberNew-ADServiceAccount
Add-ADPrincipalGroupMembershipNew-ADUser
Clear-ADAccountExpirationRemove-ADComputer
Disable-ADAccountRemove-ADComputerServiceAccount
Disable-ADOptionalFeatureRemove-ADDomainControllerPasswordReplicationPolicy
Enable-ADAccountRemove-ADFineGrainedPasswordPolicy
Enable-ADOptionalFeatureRemove-ADFineGrainedPasswordPolicySubject
Get-ADAccountAuthorizationGroupRemove-ADGroup
Get-ADAccountResultantPasswordReplicationPolicyRemove-ADGroupMember
Get-ADComputerRemove-ADObject
Get-ADComputerServiceAccountRemove-ADOrganizationalUnit
Get-ADDefaultDomainPasswordPolicyRemove-ADPrincipalGroupMembership
Get-ADDomainRemove-ADServiceAccount
Get-ADDomainControllerRemove-ADUser
Get-ADDomainControllerPasswordReplicationPolicyRename-ADObject
Get-ADDomainControllerPasswordReplicationPolicyUsageReset-ADServiceAccountPassword
Get-ADFineGrainedPasswordPolicyRestore-ADObject
Get-ADFineGrainedPasswordPolicySubjectSearch-ADAccount
Get-ADForestSet-ADAccountControl
Get-ADGroupSet-ADAccountExpiration
Get-ADGroupMemberSet-ADAccountPassword
Get-ADObjectSet-ADComputer
Get-ADOptionalFeatureSet-ADDefaultDomainPasswordPolicy
Get-ADOrganizationalUnitSet-ADDomain
Get-ADPrincipalGroupMembershipSet-ADDomainMode
Get-ADRootDSESet-ADFineGrainedPasswordPolicy
Get-ADServiceAccountSet-ADForest
Get-ADUserSet-ADForestMode
Get-ADUserResultantPasswordPolicySet-ADGroup
Install-ADServiceAccountSet-ADObject
Move-ADDirectoryServerSet-ADOrganizationalUnit
Move-ADDirectoryServerOperationMasterRoleSet-ADServiceAccount
Move-ADObjectSet-ADUser
New-ADComputerUninstall-ADServiceAccount
New-ADFineGrainedPasswordPolicyUnlock-ADAccount




An additional 59 Active Directory cmdlets were introduce with version 3.0 bringing the total to 135.  As you would expect the new cmdlets in v3 are centered around the new features introduced for Active Directory in Windows Server 2012 such as Dynamic Access Control

There are also new cmdlets in v3 that can be used to for Replication and Topology Management   They are not a complete replacement for the powerful repadmin tool but they are another excellent resource for AD admins.




59 ADDITIONAL AD CMDLETS POWERASHELL VERSION 3.0 135 TOTAL AD CMDLETS IN v3
Add-ADCentralAccessPolicyMemberGet-ADResourcePropertyValueTypeRemove-ADReplicationSiteLinkBridge
Add-ADResourcePropertyListMemberGet-ADTrustRemove-ADReplicationSubnet
Clear-ADClaimTransformLinkNew-ADCentralAccessPolicyRemove-ADResourceProperty
Get-ADCentralAccessPolicyNew-ADCentralAccessRuleRemove-ADResourcePropertyList
Get-ADCentralAccessRuleNew-ADClaimTransformPolicyRemove-ADResourcePropertyListMember
Get-ADClaimTransformPolicyNew-ADClaimTypeSet-ADCentralAccessPolicy
Get-ADClaimTypeNew-ADDCCloneConfigFileSet-ADCentralAccessRule
Get-ADDCCloningExcludedApplicationListNew-ADReplicationSiteSet-ADClaimTransformLink
Get-ADReplicationAttributeMetadataNew-ADReplicationSiteLinkSet-ADClaimTransformPolicy
Get-ADReplicationConnectionNew-ADReplicationSiteLinkBridgeSet-ADClaimType
Get-ADReplicationFailureNew-ADReplicationSubnetSet-ADReplicationConnection
Get-ADReplicationPartnerMetadataNew-ADResourcePropertySet-ADReplicationSite
Get-ADReplicationQueueOperationNew-ADResourcePropertyListSet-ADReplicationSiteLink
Get-ADReplicationSiteRemove-ADCentralAccessPolicySet-ADReplicationSiteLinkBridge
Get-ADReplicationSiteLinkRemove-ADCentralAccessPolicyMemberSet-ADReplicationSubnet
Get-ADReplicationSiteLinkBridgeRemove-ADCentralAccessRuleSet-ADResourceProperty
Get-ADReplicationSubnetRemove-ADClaimTransformPolicySet-ADResourcePropertyList
Get-ADReplicationUpToDatenessVectorTableRemove-ADClaimTypeSync-ADObject
Get-ADResourcePropertyRemove-ADReplicationSiteTest-ADServiceAccount
Get-ADResourcePropertyListRemove-ADReplicationSiteLink


Windows Server 2012 R2 introduced an additional 12 AD cmdlets bringing the total up to 147 AD cmdlets. The 12 new cmdlets are centered around Authentication Policies and Authentication Policy Silos.   If you haven't seen them then open up the AD Admin Center on a 2012 R2 box




I'm personally still learning about these new features myself.   Authentication policies can control which hosts an account can sign into. Windows Server 2012 R2 is also being called the "CloudOS" so many of the new features are based around Azure and the cloud.


12 ADDITIONAL AD CMDLETSPOWERSHELL VERSION 4.0147 TOTAL AD CMDLETS IN v4
Get-ADAuthenticationPolicyNew-ADAuthenticationPolicySiloSet-ADAccountAuthenticationPolicySilo
Get-ADAuthenticationPolicySiloRemove-ADAuthenticationPolicySet-ADAuthenticationPolicy
Grant-ADAuthenticationPolicySiloAccessRemove-ADAuthenticationPolicySiloSet-ADAuthenticationPolicySilo
New-ADAuthenticationPolicyRevoke-ADAuthenticationPolicySiloAccessShow-ADAuthenticationPolicyExpression

Tuesday, June 25, 2013

Windows Server 2012 R2 Preview - Schema Version

I previously posted  a "quick-hitter" blog about the schema version in Windows Server 2012

Windows Server 2012 R2  preview was released today! The current version is 69 

I once again used adfind to quickly find the schema version.




For those that prefer to use powershell; you can also find the object version that way. 




The current (as of 25 June 2013) Active Directory Schema version table is listed below.

Windows Server 2012 R2 Preview
69
Windows Server 2012 
56
Windows 2008 R2
47
Windows 2008
44
Windows 2003 R2
31
Windows 2003
30
Windows 2000
13


You can download an evaluation copy of Windows Server 2012 R2  and go start to learn and have fun.  Thanks to all the hard work put in by the many people at Microsoft that made today happen.

Thursday, August 23, 2012

Find Inactive Users using Powershell

This is a quick hitter that came about when I was chatting with a few friends online.   We were talking about finding inactive users using powershell.   We also wanted to output their userid(samaccountname) and their last logon time.

In this case the LastLogonTimeStamp attribute was good enough for this query.  Note that this attribute is replicated but it is 9-14 days behind the current date

For full disclosure this is something I'd usually use oldcmp for but in this case the customer wasn't allowing third party tools.

The main problem I was having was the output of LastLogonTimeStamp via powershell.  The date doesn't get automatically converted from its native 64 bit format.  Luckily the Microsoft team has included the LastLogonDate which is the conversion of the LastLogonTimestamp.  MVP Richard Mueller has a great explanation of the LastLogonDate attribute in Powershell    It is important to emphasize that LastLogonDate is not an actual Active Directory attribute.  LastLogonDate was key otherwise it makes this query more complex because we would have had to include a conversion into the command.

For the query I went with the search-adaccount cmdlet.  We were looking for accounts that had not been active within 90 days

search-adaccount -usersonly -accountinactive -timespan "76" | select-object samaccountname, lastlogondate

If you want to export that to a CSV then that command can be piped into export-csv

search-adaccount -usersonly -accountinactive -timespan "76" | select-object samaccountname, lastlogondate | export-csv Users.csv

Why did I choose 76 instead of 90?  That goes back to the DS blog about lastlogontimestamp being up to 14 days behind.

Active Directory Administrative Center also has some handy built-in searches that can help if you prefer a GUI









Update  Good friend and Microsoft PFE Eric J suggested that I add a screenshot with the Windows 2012 version of ADAC and the powershell history viewer output.  Great suggestion Eric!





The powershell command in the history viewer is interesting. I like the version above a lot better :)

Get-ADObject -LDAPFilter:"(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(lastLogonTimestamp<=129888720000000000)(!lastLogonTimestamp=*)))" -Properties:allowedChildClassesEffective,allowedChildClasses,lastKnownParent,sAMAccountType,systemFlags,userAccountControl,displayName,description,whenChanged,location,managedBy,memberOf,primaryGroupID,objectSid,msDS-User-Account-Control-Computed,sAMAccountName,lastLogonTimestamp,lastLogoff,mail,accountExpires,msDS-PhoneticCompanyName,msDS-PhoneticDepartment,msDS-PhoneticDisplayName,msDS-PhoneticFirstName,msDS-PhoneticLastName,pwdLastSet,operatingSystem,operatingSystemServicePack,operatingSystemVersion,telephoneNumber,physicalDeliveryOfficeName,department,company,manager,dNSHostName,groupType,c,l,employeeID,givenName,sn,title,st,postalCode,managedBy,userPrincipalName,isDeleted,msDS-PasswordSettingsPrecedence -ResultPageSize:"100" -ResultSetSize:"20201" -SearchBase:"DC=MK2012,DC=com" -SearchScope:"Subtree" -Server:"w2012DC1.MK2012.com"


The issue I have with the ADAC method is that it doesn't allow the user to export the findings and include the   LastLogonTimeStamp date in a converted form.


I'm looking forward to other suggestions comments on how to improve this powershell command.   Remember we are talking quick-hitter one liner here.

Thursday, March 22, 2012

LastLogonTimestamp for Group Members

I was recently working in a secure environment and one of the issues was way too many domain admin accounts.  This is not a problem just in secure environment.  I've yet to encounter a federal organization that does an outstanding job of limiting the number of domain admins.  I've seen Joe Richards write about working at a Fortune 5 company where they ran with less than 5 domain administrators.  More and more organizations are trying to limit domain admins.  I doubt we will ever get to a point where less than five is the norm but things are getting better...slowly but surely.

The first step the security team took was to identify members of the domain admin group and the last time they logged in.  This is a good initial step to remove those that haven't logged on or used their accounts.  If someone hasn't used their domain admin account in 120 days or longer then I would question if they need the account.

Some folks on the security team were manually going and using a box that had the additional account info tab from the acctinfo.dll.  They were then looking at lastlogon box within the tab and manually entering that into a spreadsheet.  I knew there were easier ways to do this so I stepped in to help out.

For this exercise I keyed off the LastLogonTimeStamp (LLTS) The lastlogontimestamp can be off by 9-14 days.  The link to the askds blog entry on LLTS does a great job of explaining it.  If 9-14 days is not acceptable then you would have to query lastlogon on every DC.  Lastlogon does not replicate and that is why every DC would have to be queried.

For the examples I'm in my lab domain which is mkw2k8R2.com and I only have three users in the domain admin group.  I've only logged in with one of those users.

Method 1 - Using ADFIND

Regular blog users will not be surprised to find out that I used adfind from Joe Richards for method 1.  

adfind -default -f "memberof=cn=domain admins,cn=users,dc=mydomain,dc=mysuffix" samaccountname lastlogontimestamp -tdc -nodn -csv 


Method 2 - Using Quest AD Powershell Cmdlets

Many people that started with powershell and AD years ago are probably familiar with the free AD cmdlets from Quest.  

get-qaduser -memberof "domain admins" | select-object samaccountname, lastlogontimestamp


Method 3 - Using Microsoft's AD Powershell v2 Cmdlets

With the introduction of Windows 2008 R2 and Windows 7 Microsoft introduced the AD module for Windows Powershell.  There is already a lot of good information about the AD Module for Powershell so I won't go over that here.   I also admit I'm not a powershell master/guru.

get-aduser -LDAPFilter "(memberof=cn=domain admins,cn=users,dc=mkw2k8r2,dc=com)" -property lastlogondate | ft samaccountname, lastlogondate


If you noticed I used lastlogondate which is not an actual AD attribute.  My friend Richard Mueller had a good writeup on lastlogondate.    See the link and Richard's answer for more info on lastlogondate which is essentially the same as lastlogontimestamp


Method 4 - Using CSVDE

CSVDE is what you call an old school tool.  Those that have been around AD for years have definitely used the tool at some point.  It was around before adfind and powershell. 


csvde -f c:\userslogon.csv -r "(memberof=cn=domain admins,cn=users,dc=mkw2k8r2,dc=com)" -l samaccountname, lastllogontimestamp  


One problem with the CSVDE method is how it handles the output.   LastLogonTimeStamps are Integer8 (64-bit numbers) that CSVDE can't handle.  You will notice in methods 1-3 those tools did a good job of decoding the attribute.

Elizabeth Greene has a really good blog entry that has a formula you can use in excel to convert it into a readable date.

Notice in the screenshot the difference between the native format in cell C2 and what it looks like after I applied the formula




Method 5 - Using Repadmin

This method I first saw used in the blog from the askds team that I linked to earlier and I'll link to again here


repadmin /showattr dc1root dc=mkw2k8r2,dc=com /subtree /filter:"(memberof=cn=domain admins,cn=users,dc=mkw2k8r2,dc=com)" /attrs:lastlogontimestamp



Other Methods


I really like methods 1-3 the best.  There are other methods that I have not included here but I figured five is a good start for anyone.  Some other things you might see out there


  • VBScript - Richard is the king in this category and if you want to use VBScript I recommend testing his scripts out.
  • Powershell v1 without AD cmdlets - remember when I said I was not a powershell guru yet.  I'm guessing that is something that can be done but haven't tried to do it yet.   The AD cmdlets from Microsoft and Quest both work for me so I try to stick to them.

You can use these examples and modify them if you are looking for other groups.  There are other/better ways to identify old/stale accounts in a domain if you want to do it domain wide.  More to come on that.

I'm really looking forward to hearing from readers and the community on other methods for doing this.  If there are better ways to do it in Powershell please leave a comment and I'll definitely update the blog.

Inactive Domain Admins beware....you will be removed :)

Tuesday, March 30, 2010

New DNS PowerShell Module

My friend Chris Dent has realeased a new DNS module for powershell.

DnsShell - Alpha Release

Chris is one of my favorite people on the boards and he knows a lot about Power Shell, Active Directory, and DNS and he is once again proving why he is such an asset to the community.

If you have some time test it out, I know Chris would like the feedback.

Thanks

Mike

Wednesday, June 24, 2009

Find Nested Group Members

I've run into a few questions recently where someone wanted to find the members of a security group. That in itself is fairly straight forward.

However what if your security group has nested groups and users. Then those nested groups may also have additional nested groups and users. What does that query look like? How do you find all the members?

Suppose I have the following Example


  • TopLevelGroup -- Global Security Group

    • TopLevel -- User
    • TopLevel2 - User2
    • Nested1 - Global Security Group
        Nested1 Members
      • Nested User
      • Nested User 2
      • InsideNested - Global Security Group
          InsideNested Members
        • InsideNested1







There are several ways to do this, I'm not saying these are the only methods but these are three examples that work.

The first method is to use the PowerShell. For this example you will need the Quest AD Cmdlets. Thanks to MVP Dmitry Sotnikov for the Quest cmdlets.

Get-QADGroupMember "Group Name" -indirect



The second method is using ADFIND by MVP Joe Richards

adfind -default -bit -f "memberof:1.2.840.113556.1.4.1941:=DN of Group" samaccountname -nodn





More on that query here

Big Thanks to Chris Dent for that part. He was also involved in the questions. Chris was an MVP and should be an MVP again. One of the best and most knowledgeable guys around.


Now on to method three. Some people (especially in classified networks) can't install the Quest cmdlets or adfind (or any third party tool)

The Microsoft DStools can be used. For this example I'll use dsquery and dsget

dsquery group -samid "group name" | dsget group -members -expand



I hope that helps someone out there. Please let me know via comments if there are any questions.

Thanks

Mike

Tuesday, June 9, 2009

Find Users Who are Not in Specific Groups

I know everyone has been wondering what happened to the quick hitter series...well it is back :)

This question has come up twice over the last few weeks on the AD section at Experts Exchange so that means time for an entry.

The question is suppose I have some groups and I want to find out if users are not members of any of the groups.

Example:

GroupA, GroupB, GroupC, GroupD - So how do I find out what users are not members of those groups?



Two quick ways that I like to use are ADFIND and Powershell. I know there are other methods.

The first is to use ADFIND by MVP Joe Richards

adfind -default -f "&(objectcategory=person)(objectclass=user)(!memberof=DN of groupA)(!memberof=DN of groupB)(!memberof= DN of group C)(!memberof= DN of groupD)" samaccountname memberof -nodn




The other method is to use PowerShell. For this example you will need the Quest AD cmdlets. Thanks to Dmitry Sotnikov for those

get-qaduser -sizelimit 0 -notmemberof groupa, groupb, groupc, groupd | ft -wrap samaccountname, memberof




In my examples I've outputted the memberof field just so you can verify the commands do what you want and don't have users that are members of those groups, you can take that out if you want.

UPDATE:
Joe Richards wrote a great blog entry about DN Formats in AD

As you can see from Joe's post you can also use the GUID of the group instead of the DN in the adfind/LDAP query.

Want to quickly find the GUID of your group...ADFIND once again :)

adfind -sc g:GroupName objectGUID

Thanks

Mike

Friday, April 24, 2009

Force Certain Users to change passwords via Command Line

There was a question that recently came up where the poster wanted to force some of his users whose login name started with B to change their passwords. He wanted to do this using the command line.

This Friday quick hitter post will show two ways to do this (there are other ways also)

What I like for this sort of task are adfind and admod by Joe Richards

The command I used was:

adfind -default -f "&(objectcategory=person)(objectclass=user)(samaccountname=b*)" -dsq | admod pwdLastSet::0



That will set "User must change password at next logon" for logon names that begin with B.

Some notes about this command:


  • Joe puts a lot of safety nets in his tools (good thing).
  • You can use the -unsafe switch with admod if you don't want a safety or you can use the -safety switch and specify how many objects you want to modify (by default the safety kicks in at 10)
  • You can also specify -upto xx if you want it to do xx object mods and then stop...thanks Joe for that one :)


Brandon Shell also came in with a powershell command to do this. If you don't know Brandon check out his blog . Brandon is very knowledgeable but also a really cool guy who is always willing to help and a huge asset to the community.

The powershell command takes advantage of Quest's Active Directory cmdlets. Big thanks to Dmitry Sotnikov and everyone at Quest for those.

The command is

Get-QADUser -SamAccountName b* | Set-QADUser -UserMustChangePassword $true



As you can see both commands worked and met the requirements. I hope you can also see how you can manipulate these commands to set other attributes for example. Comment or contact me for more info.

For those that are more comfortable with the GUI you can run the same LDAP query I used in adfind in Active Directory Users and Computers and find them and highlight them all at once and check the box to force them to change their password.

Hope everyone has a great weekend, spring is finally here on the East Coast of the USA so it should be nice.