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?
 
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
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
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
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.

 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
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
 
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 CEE Solution Guide - CEO Faru Bonon ITCommented:
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
 
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 CEE Solution Guide - CEO Faru Bonon ITCommented:
.... did you test my solution?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.