This also works. (no wildcard needed)
(Get-ADUser SamAccountName -properties proxyaddresses).proxyaddresses | Where {$_ -NotMatch 'domain.com'}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello everyone,
I need to store proxy addresses into a variable, but with an exception of certain specific address. How do I pass a filter to proxyaddresses attribute?
For example, if I want to select only address that start with "SMTP:" and ends with "domain.com", I do it like this:
I define a user that I want to work with :
$users = get-aduser -filter "samaccountname -eq 'somebody'
I select proxyaddresses from his AD user profile:
$userProxy = get-aduser $user -Properties * | select proxyaddresses
And by using variable property StartsWith and EndsWith I am able to define what I need (and store it into variable):
$userProxyFiltered = $userProxy.Proxyaddresses.Where( { $.StartsWith( 'SMTP:' ) -AND $.EndsWith('@keyman .com')} )
But I also need an opposite thing to that - I need to store into a second variable all proxy addresses that do not end with domain.com, and do not start with X500
What are the other methods that I can use to filter a variable content apart from StartsWith and EndsWith? or what other way would you filter a specific content from a variable?
If I do it like this
$someVariable = ({$userProxy.proxyaddresses -notlike 'SIP:'} -AND {$userProxy.proxyaddresses -notlike 'X500:'})
All I get is
PS C:\WINDOWS\system32>> $someVariable
True
EDIT: Or, If I take a look at the Contains method here
https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-6.0
Is there a way to negate the method, to use something like .NotContains?
I hope I explained myself enough, thanks for any hints or comments
BR,
Tomas
This also works. (no wildcard needed)
(Get-ADUser SamAccountName -properties proxyaddresses).proxyaddresses | Where {$_ -NotMatch 'domain.com'}
First of all, you don't need to use the filter in Get-ADUser when using SamAccountName as this is an acceptable value for the identity parameter.
So you just need to use
$user = Get-ADUser SamAccountName -properties Proxyaddresses
Please don't use the * to retrieve all attributes which you then mostly drop. It affects the performance of scripts. Just bring back the properties you need.
To get your filter working correctly, you need to pipe the results into a Where-Object like you have in your first example. The way you have it will only return true or false.
$userProxyFiltered = $user.Proxyaddresses | Where xxxxx
Hello,
thank you for your answer. However, I dont understand how should I do what you are suggesting - if I go like this:
$userProxyFiltered = $userProxy.Proxyaddresses | where {$_.userProxy.Proxyaddresses -notmatch '@keyman .com'}
I still end up with all the proxyaddresses user has on his AD account
Can you elaborate a bit more please?
Thank you
Tomas
Your also missing the wildcards.
The following should work.
(Get-ADUser SamAccountName -properties proxyaddresses).proxyaddresses | Where {$_ -NotLike 'sip:*' -and $_ -NotLike 'X500*'}
Thats it, now I am able to move on with the script
Thank you very much!
Tomas