Only update accounts with no data in certain fields

Hi EE

Can someone help me modify this so if there is already data in the office or description field , its skips those users ? and it shows in the console the accounts that
were skipped ?  

$Users = Import-Csv .\UpdateOfficeDesc.csv
foreach ($user in $users) {Set-ADUser -identity $user.user_id -office $user.office -description $user.description }

Open in new window

LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
Import-Csv .\UpdateOfficeDesc.csv | %{
    $user = Get-ADUser -identity  $($_.user_id) -Properties Description

    if([String]::IsNullOrEmpty($user.Description)){
        $user | Set-ADUser -office $user.office -description $user.Description 
    }
}

Open in new window

0
MilesLoganAuthor Commented:
Hi Jose , thank you for helping ..  it is not updating any accounts now...

1) tested account with a blank office and description and it did not make the update .
2) tested account with data in the office and description fields .. it did not as it should .. but #1 is not working so I cant
say this is correct ..can you review ?
0
Jose Gabriel Ortega CastroCEOCommented:
The request was "description" empty only, not "description" and "office".
Now let me check those :)

What is the version of Powershell you're using or what OS?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

MilesLoganAuthor Commented:
5.1 on a Windows 10 machine.

here is a quick recap .

so if there is something already in the office or description .. then it just needs to skip those accounts
otherwise it should update them only if those fields are empty.
0
Jose Gabriel Ortega CastroCEOCommented:
This would work.
Import-Csv .\UpdateOfficeDesc.csv | %{
    $user = Get-ADUser -identity  $($_.user_id) -Properties Description,Office

    if([String]::IsNullOrEmpty($user.Description) ){
         Set-ADUser -identity  $($_.user_id) -office $_.office -description $_.Description
    }
}

Open in new window

0
oBdACommented:
This will report to the console and update only empty properties.
It's in test mode and will only pretend to change the user objects. Remove the -WhatIf in line 18 to run it for real.
$Users = Import-Csv .\UpdateOfficeDesc.csv
$Users | ForEach-Object {
	"Processing '$($_.user_id)' ..." | Write-Host -ForegroundColor White
	If ($ADUser = Get-ADUser -Filter "samAccountName -eq '$($u.user_id)'" -Properties Description, Office) {
		$splat = @{'Identity' = $_.user_id}
		ForEach ($Property In 'Description', 'Office') {
			If ($ADUser.$Property) {
				"  - $($Property): '$($ADUser.$Property)'" | Write-Host -ForegroundColor White
			} Else {
				"  - $($Property): <empty>, will update" | Write-Host -ForegroundColor Yellow
				$splat[$Property] = $_.$Property
			}
		}
		If ($splat.Count -eq 1) {
			"... no update required." | Write-Host -ForegroundColor Green
		} Else {
			Try {
				Set-ADUser @splat -ErrorAction Stop -WhatIf
				"... updated." | Write-Host -ForegroundColor Green
			} Catch {
				"Update failed: $($_.Exception.Message)" | Write-Host -ForegroundColor Red
			}
		}
	} Else {
		"... not found!" | Write-Host -ForegroundColor Red
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi oBdA

Thank you .. but I received the error below .

Get-ADUser : The search filter cannot be recognized
At C:\TESTING\UpdateTest.ps1:4 char:16
+ ...  ($ADUser = Get-ADUser -Filter "samAccountName -eq '$($u.user_id)'" - ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
oBdACommented:
Sorry, copy and paste error from testing; in line 4, just replace the "$u" in "samAccountName -eq '$($u.user_id)'" with "$_":
	If ($ADUser = Get-ADUser -Filter "samAccountName -eq '$($_.user_id)'" -Properties Description, Office) {

Open in new window

0
MilesLoganAuthor Commented:
Thank you so much !

I just created a new one going off this question if you are also able to assist .

https://www.experts-exchange.com/questions/29093729/Only-update-accounts-with-no-data-in-certain-fields-v2.html#questionAdd
0
Jose Gabriel Ortega CastroCEOCommented:
.... did you test my solution?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.