Solved

Powershell script

Posted on 2011-09-21
2
555 Views
Last Modified: 2012-08-13
I have users in our OU structure that do not get fed an employee ID field in their employeeid attribute. I need to run a script to fill this with a generic value of NPE, but exclude users that have that attribute currently fed through by our HR software. Im new to powershell and we have to have this done today. As much help as i can get would be great. Please assist.
0
Comment
Question by:arsenal22
2 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 250 total points
ID: 36575224
I would use the Quest AD cmdlets. You could do something like this



get-qaduser -SearchRoot "OU=USERS_OU,DC=Domain,DC=Local" -LDAPFILTER "(&(objectcategory=person)(objectclass=user)(!employeeID=*))" | Set-QADUser -ObjectAttributes @{employeeID="NPE"}

Open in new window

0
 
LVL 8

Assisted Solution

by:bchallis
bchallis earned 250 total points
ID: 36813023
I have attached some code that defines a function that you can call to update a property value for AD objects.  If you do not supply a value for the -OldValue the test will be done for that property value being empty or null.

The -PassThru switch has been added so you can get the collection of user objects that have been updated.

This function assumes that you have a Windows Seerver 2008 R2 Domain Controller (or have retro fitted the functionality to an earlier server) and have already executed an Import-Module ActiveDirectory command to make the Get-ADUser and Set-ADUser cmdlets available.

I put a test line in as well. The first time I ran the test was without a parameter for the -OldValue so fill the ones that had nothing, then updated the value a few times.
function Set-DefaultADProperty
{
    PARAM
    (
        $Filter = "*",
        [Parameter(Mandatory=$true)]
        $Property,
        $OldValue,
        [Parameter(Mandatory=$true)]
        $NewValue,
        [switch]$PassThru = $false
    )
    Import-Module ActiveDirectory
    
    $command = {Get-ADUser -filter $Filter -properties $Property}

    $users = & $command
    $selectedUsers = @()
    foreach ($u in $users)
    {
        if ([system.string]::IsNullOrEmpty($OldValue))
        {
            if ([system.string]::IsNullOrEmpty($u.$Property))
            {
                $selectedUsers += $u
            }
        }
        else
        {
            if ($u.$Property -like $OldValue)
            {
                $selectedUsers += $u
            }
        }
    }
    
    foreach ($u in $selectedusers)
    {
        $u.$Property = $newValue
        Set-ADUser -Instance $u
    }
    
    if ($PassThru)
    {
        Write-Output $selectedUsers
    }
}

#Test
Set-DefaultADProperty -Filter * -Property employeeID -OldValue NPN -NewValue NP0 -PassThru

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now