Solved

Powershell script

Posted on 2011-09-21
2
559 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:Brent Challis
Brent Challis 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

821 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