Jump to content

Recommended Posts

Posted (edited)

Hi Guys/Gals,

I'm hoping I've just missed something simple here. the first section of code runs fine to pull any user who has any of the $Titles in their job Title. But I also want to write a different attribute to everyone who doesn't. I know I could reverse the code and simple add the attribute to everyone then overwrite the "SLT" members, but this is risky due to who its affecting and if the code stopped halfway etc.

 

So essentially, the -Notlike $Titles in the All Staff $StaffUser filter doesn't seem to be applying... Any guesses?

 

$ICTs = "*ICT*"
$Pastorals = "*Pastoral*"
$DSLs = "*DSL*"
$MLTs = "*Subject Leader*", "*CAL - *", "*Second in*"
$SLTs = "*HeadTeacher*", "*Senior Leader*","*Business Manager*" 
$Titles = $SLTs+$MLTs+$DSLs+$Pastorals+$ICTs

#ADMINS
write-host
write-host "ADMINS"
write-host
foreach ($Title in $Titles)
   {$MatchUsers = get-aduser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -like $Title -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

       foreach ($MatchUser in $MatchUsers)
       #    {Set-ADUser –Identity $MatchUser -add @{"extensionAttribute10"="Retention-Email-Admin"}}
       {write-host $MatchUser 
        Write-Output $MatchUser | Out-File C:\Data\UserAttributesUpdate\Test-Admin.csv -append}
   }

#All Staff
write-host
write-host "STAFF"
write-host

foreach ($Title in $Titles)
   {$StaffUsers = get-aduser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -notLike $Title -and department -notLike "*pupil*" -and displayName -notLike "*Guest*" -and displayName -notLike "*Pupil*" -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

       foreach ($StaffUser in $StaffUsers)
       #    {Set-ADUser –Identity $MatchUser -add @{"extensionAttribute10"="Retention-Email-Staff"}}
       {write-host $StaffUser 
        Write-Output $StaffUser | Out-File C:\Data\UserAttributesUpdate\Test-Staff.csv -append}
   }

 

(PS, the actual attribute adding bit of the code I've commented out and I'm just exporting to txt lists during testing.)

Edited by smithson83
Posted

Running your same code, but with different csv path, I get zero results for the second half.

 

If I remove

 

-and department -notlike "*pupil*"

I then get results.

 

However if I instead remove your title filter, I still don't get results - which suggests the error is instead with the department/pupil not the title?

 

It can't be the double -notlike because it works with just the title filter.

 

A bit confused just like you are!

Posted

So I think I have it, but it feels like butchery...

 

I split the loops and built up the filter query using $StaffFilter then supplied this to Get-AdUser before running through the final loop. Like I said, it feels a little messy. Does anyone have a solution with a bit more finesse?

 

$StaffFilter = 'enabled -eq "true" '
foreach ($Title in $Titles)
   {$StaffFilter = $StaffFilter + '-and Title -notLike ' + '"'+$Title+'" '}
$StaffFilter = $StaffFilter + ' -and department -notLike "*pupil*" -and displayName -notLike "*Guest*" -and displayName -notLike "*Pupil*"'

$StaffUsers = get-aduser -properties Title,extensionAttribute10,Displayname,department -filter "$StaffFilter" | select DisplayName,Title,extensionAttribute10

   foreach ($StaffUser in $StaffUsers)
   #    {Set-ADUser –Identity $MatchUser -add @{"extensionAttribute10"="Retention-Email-Staff"}}
       {write-host $StaffUser 
        Write-Output $StaffUser | Out-File C:\Data\UserAttributesUpdate\Test-Staff.csv -append}

Posted
Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter 'department -notLike "*pupil*" -and displayName -notLike "*Guest*" -and displayName -notLike "*Pupil*" -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -notLike "Mr" -and displayName -notLike "*Guest*" -and displayName -notLike "*Pupil*" -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -notLike "Mr" -and department -notLike "*pupil*" -and displayName -notLike "*Pupil*" -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -notLike "Mr" -and department -notLike "*pupil*" -and displayName -notLike "*Guest*" -and enabled -eq "true"' | select DisplayName,Title,extensionAttribute10

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter 'Title -notLike "Mr" -and department -notLike "*pupil*" -and displayName -notLike "*Guest*" -and displayName -notLike "*Pupil*"' | select DisplayName,Title,extensionAttribute10

The only line that runs there is the 2nd one - where pupil department is not filtered.

Posted (edited)

A little more googling, I came across a site that suggested, the -notLike didnt like processing the array of titles. Which kinda makes sense, somewhere in my brain.

 

So I essentially expanded the Titles in to programatically adding them in one by one to build up the filter which would look like this if it was hand written

 

enabled -eq "true" -and Title -notLike "*HeadTeacher*" -and Title -notLike "*Senior Leader*" -and Title -notLike "*Business Manager*" -and Title -notLike "*Subject Leader*" -and Title -notLike 
"*CAL - *" -and Title -notLike "*Second in*" -and Title -notLike "*DSL*" -and Title -notLike "*Pastoral*" -and Title -notLike "*ICT*"  -and department -notLike "*pupil*" -and displayName -notLi
ke "*Guest*" -and displayName -notLike "*Pupil*"

 

Which is where the Butchered bit comes in

Edited by smithson83
Posted (edited)

Seems all overly convoluted to me can you not do something like this:-

 

$titleArray = @()
$titleArray = 'HeadTeacher','Senior Leader','Business Manager'

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter * | Where-Object {$titleArray -notlike $_.Title}

 

And I am pretty sure that you will be able to do -*contains -*match an all, ain't tested any of this so happy to be told otherwise.

Edited by HPlum78
  • Thanks 1
Posted (edited)

Oh and just for completeness you could also use the following as long as your using Win PS v4.0 or later and PS v 7.0 and later:

$titleArray = @()
$titleArray = 'HeadTeacher','Senior Leader','Business Manager'

Get-ADUser -properties Title,extensionAttribute10,Displayname,department -filter * | where-object -property Title -in $titleArray

This is the new way of using where-object that was introduced in later versions of PS and is more human readable than using the script block method.

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...