Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Powershell script

Posted on 2011-09-21
2
560 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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