Solved

Powershell script

Posted on 2011-09-21
2
549 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 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

14 Experts available now in Live!

Get 1:1 Help Now