I have multiple duplicated lines of code, where only "office" name should be different, but criteria which are in the $paste variable should be the same.
$paste = "extensionAttribute -eq 123"
$users1 = get-aduser -Properties canonicalName -filter {(office -like "ROFL") -and ( $paste )}
$users2 = get-aduser -Properties canonicalName -filter {(office -like "BOFL") -and ( $paste )}
While I'm trying to do so (insert the content of $paste variable) I'm receiving this kind of error:
get-aduser : Error parsing query: '(office -like "ROFL") -and ( $paste )' Error Message: 'syntax error'
at position: '47'.
At line:1 char:10
+ $users = get-aduser -Properties canonicalName...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
How can I avoid this manual inserting every time when I need to change only part of the criteria?