Skip to main content

Posts

Showing posts from May, 2013

Great example of Using PowerShell to modify AD properties

Today in class I took a question from the Active Directory Forums on PowerShell.com where I am a moderator.  The user had an Excel spreadsheet containing the first and last names of his users, and a field for their pager numbers.  He needed a way to test to see if the pager value in the spreadsheet matched the one in active directory.  If not, he needed the number changed.  Here is what I sent him: Import-Module ActiveDirectory   ForEach ( $User in ( Import-CSV -Path C:\PS\Users.CSV )) {     $Last = $User . Last     $First = $User . first     Get-ADUser -Filter '(Surname -eq $Last) -and (GivenName -eq $First)' |     ForEach { If ( $User . OtherPager -ne $_ . Pager)     {         $_ | Set-ADObject -Replace @{OtherPager = " $( $User . OtherPager) " }     }     } }   Assuming that he was using PowerShell V2, I had him import the PowerShell module for Active Directory.   I then had him load the excel spreadsheet (which was saved as a CS

Get a US Navy Date Time Group (DTG) with PowerShell

This past week I’ve been at Naval Station Great Lakes training for a mission later this year.  As I’m training up for this mission, I’m seeing some procedures that may benefit from PowerShell.  Many of the procedures that I will be executing involves utilizing a Navy Date Time Group (DTG).  The problem with DTGs is that they are not exactly logical.  Here is the format. 2 digit day 2 digit hour 2 digit minute Z for ZUL (GMT for the civilians out there) 3 Character month 2 digit year. So, the procedure that I will be utilizing this code with will require that I either figure this out on my own under a high tempo/stress situation, or I simply ask PowerShell to do this.  Since we do not utilize PowerShell for any of our tasks, I also needed a way to copy and paste this information into an application.  The code below will generate the DTG and also place it on the clipboard.  Relying on a manual copy and paste or simply getting the DTG from PowerShell and manually typing it would ha

Data Deduplication Demo

Data Deduplication can save valuable amounts of hard drive space.  In today’s cost conscious environments, saving hard drive space can translate into budgets that can be utilized elsewhere.  The question that often pops up is “How much space will data dedup save me?”  Unfortunately, there is no way to make a accurate prediction.  Data dedup works best with static data.  That is because there is no reason to dedup data that changes often.  The PowerShell code below will generate a few thousand text files that will share a lot of common bit patterns.  This will help to demonstrate some space savings with dedup. $String $NewLineIndex = 0 For ( $X = 0 ; $X -lt 10000 ; $X ++ ) {     $C1 = [ Char ] (( Get-Random ( 35 )) + 65 )     $C2 = [ Char ] (( Get-Random ( 35 )) + 65 )     $C3 = [ Char ] (( Get-Random ( 35 )) + 65 )     $C4 = [ Char ] (( Get-Random ( 35 )) + 65 )     $String + = " $( $C1 )$( $C2 )$( $C3 )$( $C4 ) "     If ( $Ne

Retrieve a list of all Disabled Computer Accounts

I just helped a user with a big headache.  The user is new to PowerShell and was working way to hard.  First of all, I am impressed that this administrator was dipping into the .NET framework.  Take a look at his code. $strfilter = "(&(objectClass=user)(objectCategory=person))"   $objDomain = New-Object System.DirectoryServices.DirectoryEntry   $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher . SearchRoot = $objDomain $objSearcher . PageSize = 1000 $objSearcher . Filter = $strFilter $objSearcher . SearchScope = "Subtree" $objSearcher . PropertiesToLoad . Add( "cn" ) | Out-Null $objSearcher . PropertiesToLoad . Add( "member" ) | Out-Null $objSearcher . PropertiesToLoad . Add( "proxyAddresses" ) | Out-Null $objSearcher . PropertiesToLoad . Add( "displayName" ) | Out-Null $objSearcher . PropertiesToLoad . Add( "distinguishedname" ) | Out-Null $objSearc