Skip to main content

How to change the logon hours of all the members of a security group with PowerShell

For this exercise, we are going to have to use the Active Directory module of PowerShell.  You will need to execute this in a PowerShell session or ISE running on a Windows Server 2008 R2 server or a Windows 7 client with RSAT installed.

Import-Module ActiveDirectory


I have already set up a security group called Astronauts_GG.  The members of this group are Neil Armstrong, Gus Grissom, Sally Ride, and David Wolf

Out next step is to be able to enumerate all the members of this group.  To do this, type:


Get-ADGroupMember –Identity Astronauts_GG

You will see each user object listed for each member of the group.

Specifying the logon hours is going to be a bit more complex.  Let’s take a look at the logon hours for a user from Active Directory Users and Computers. 

Right click a user account and select Properties 

Click the Account tab. 

Now click the Logon Hours… button.  Below is an image of the logon hours graphic:

image

In order to use PowerShell to configure the logon hours, we need to break the each of the 7 days down into 3 blocks of 8 hours.  We then need to divide each block into 8.

image

The above image represents the division of each day into 3 blocks of 8 hours.  The numbers represent how we will address each block.  Notice that the final block is labeled as '0’ and not ‘21’. 
As for breaking down each block into 8 separate hours, we are going to have to turn to binary math.  In this case each block is equal to one binary number.  The set of 8 blocks is equal to 1 byte.  In this scenario, the lowest order bit will be to the left.  for example, let set the hours of block #1 to be 12AM, 4AM, 5AM  and 7AM.

Time 12AM 1 AM 2 AM 3 AM 4 AM 5 AM 6 AM 7AM
Set ##### ##### ##### #####
Binary 1 2 4 8 16 32 64 128
Add 1 16 32 128

We can see here that if we want to assign this block the times of 12AM, 4AM, 5AM, and 7AM, we will need to add the numbers 1 + 16 + 32 + 128 = 177.  The number 177 is what we will submit to block #1.  Below is the code to do this.  I have to credit the help file for the cmdlet Set-ADUser for the code.  Take a look at example #6 in the help file got Set-ADUser..

$hours = New-Object byte[] 21
$hours[1] = 177
$ReplaceHashTable = New-Object HashTable
$ReplaceHashTable.Add(“logonHours”, $hours)
Set-ADUser “username"” –Replace $ReplaceHashTable

The original task was to set the logon hours by security group.  Here is the code to do it.


$hours = New-Object byte[] 21
$hours[1] = 177
$ReplaceHashTable = New-Object HashTable
$ReplaceHashTable.Add(“logonHours”, $hours)
Get-ADGroupMember –Identity Astronauts_GG | Set-ADUser –Replace $ReplaceHashTable

Line 1 creates a variable holding a new object of the type byte.  A byte is a computer term meaning 8 bits or a binary number that has 8 numerical places.  It also creates an array of byte with 21 cells in the array.

Line 2 set the number we calculated, 177, into the first time set.  We can add additional logon hours by adding extra lines.  For example, we can add $hour[3] = 255.  This will enable the user to log in from 4PM – 12AM on Sunday.

Line 3 creates a new object called a hash table.  A hash table allows you to create a table that will be the values of a property.

Line 4 adds the hash table to the property logonHours

Line 5 first enumerates all the user objects who are members of the group Astronauts_GG. It then passes this output to the next command using the pipe ‘|’ character.  Now the output becomes the input.  We use the –Replace function to completely remove the current logon hours and replace them with the contents of $ReplaceHashTable.

The final Logon Hours table looks like this:

image

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.