none
Matching E-Mail Address in variable RRS feed

  • Question

  • Hello,

    I am trying to remove certain addresses from my script which I have in a CSV file. I have imported them into a variable but I cant seem to work out how to get them to be excluded from the rest of the code. Any help is appreciated. 

    My code is below: 

    $Excluded = Import-Csv "C:\Temp_Storage\RetentionExclude.csv" | select Email
    
    get-Mailbox -ResultSize Unlimited | 
    Where-Object{ $_.primarySMTPAddress -notmatch "domain.com" } |
    Where-Object{ $_.name -notmatch "people" } | 
    Where-Object{ $_.primarySMTPAddress -notlike $Excluded} |
    select Name, Alias, primarySMTPAddress | 
    Export-Csv "C:\Temp_Storage\EMailRetentionTest.csv" -NoTypeInformation
    I have tried notmatch, notlike and notcontains but nothing appears to work and I am unsure why.

    Any help is appreciated.

    Thanks

    James

    • Moved by Bill_Stewart Friday, July 27, 2018 8:25 PM This is not "debug/fix/rewrite my script for me" forum
    Monday, May 21, 2018 10:45 AM

All replies

  • $excluded = Import-Csv C:\Temp_Storage\RetentionExclude.csv | 
        Select-Object -Expand Email
    Get-Mailbox -ResultSize Unlimited | 
        Where-Object{ 
            $_.primarySMTPAddress -notmatch 'domain\.com|people' -and
            $_.primarySMTPAddress -notin $excluded
        } | 
        Select-Object Name, Alias, primarySMTPAddress | 
        Export-Csv C:\Temp_Storage\EMailRetentionTest.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Monday, May 21, 2018 11:01 AM
    Monday, May 21, 2018 11:00 AM
  • Hello,

    Thank you for that however I seem to get an error for the -notin which is: "You must provide a value expression on the right-hand side of the '-'"

    I haven't heard of that one before is it part a new version of powershell?

    Thanks

    James

    Monday, May 21, 2018 2:21 PM
  • That is basic PowerShell since version 1.  Learning PowerShell would help you more than you know.

    If $excluded is null then there are no entries in your CSV.  Perhaps you do not have a CSV?


    \_(ツ)_/

    Monday, May 21, 2018 2:32 PM
  • Thank You.

    I have outputted the $Excluded variable and it shows the information from the CSV. 

    Here is what happens: 

    [PS] C:\>$excluded = Import-Csv C:\Temp_Storage\RetentionExclude.csv |
    >>     Select-Object -Expand Email
    >> Get-Mailbox -ResultSize Unlimited |
    >>     Where-Object{
    >>         $_.primarySMTPAddress -notmatch 'domain\.com|people' -and
    >>         $_.primarySMTPAddress -notin $excluded
    >>     } |
    >>     Select-Object Name, Alias, primarySMTPAddress |
    >>     Export-Csv C:\Temp_Storage\EMailRetentionTest.csv -NoTypeInformation
    >>
    - : You must provide a value expression on the right-hand side of the '-' operator.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : ExpectedValueExpression

    I have changed the 'domain\.com|people' to the relevant values. If I change the -notin to notmatch then it runs but doesn't do the exclude so it is something wrong with the -notin. 

    It looks like its not part of the Exchange Management Shell as I can run it with a Get-ADUser without error. Is there a way I could get it added to this shell or another way of doing it which would work in the Exchange Management Shell?  

    Thanks

    James

    Tuesday, May 22, 2018 9:26 AM
  • Your CSV is not a CSV.

    notepad C:\Temp_Storage\RetentionExclude.csv

    What do you see?


    \_(ツ)_/

    Tuesday, May 22, 2018 9:31 AM
  • I see a comma separated list.

    I have also opened it in Excel and saved it as a new CSV File deleted the old one and I still get the same issue with the -notin telling me to provide a value expression on the right-hand side of the '-' operator.

    The Exchange Management Shell doesn't recognise -notin and neither does PrimalScript which is the editor I am using. I am not saying its not usable as it works in the AD Powershell but the Exchange Management Shell doesn't recognise it so I think that either it needs adding in somehow or another method needs to be explored. 

    Thanks

    James

    Tuesday, May 22, 2018 10:37 AM
  • Does it have headers?  Is one header named "Email"?


    \_(ツ)_/

    Tuesday, May 22, 2018 10:38 AM
  • I was wrong.  "notin" was not available in PS v2.

    You should not be using PowerShell V2.  It has been deprecated and has serious security issues.  There is also nom need for it if you have correctly managed your scripts.

    Microsoft has recommended removing or blocking V2 on all systems.

    Upgrade to WMF 5.1 which runs on all current Windows versions.


    \_(ツ)_/

    Tuesday, May 22, 2018 10:51 AM
  • Hello,

    Thanks for the response I have Windows 10 1607 which apparently comes with it out of the box. 

    It appears to use Powershell V1 as the path is: 

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto"

    I would need to use the Exchange Management Shell for the exchange cmdlets to run.

    Thanks

    James

    Tuesday, May 22, 2018 1:45 PM
  • Ah dont worry I have it now working. 

    I found the snappin for the cmdlets and opened a new version of powershell and its fine now. 

    Thank you for your help and sorry it took a while!

    James

    Tuesday, May 22, 2018 1:50 PM
  • Windows 10 has "notin".  No snapins are required.  Exchange Management shell - if you are using the old one (you shouldn't) is PS V2.


    \_(ツ)_/

    Tuesday, May 22, 2018 2:36 PM
  • Consider using foreach.

    $Addresses = Import-Csv "C:\Temp_Storage\RetentionExclude.csv" | select Email
    
    ForEach $Address in $Addresses
    {
    get-Mailbox -ResultSize Unlimited | 
    Where-Object{ $_.primarySMTPAddress -notmatch "domain.com" } |
    Where-Object{ $_.name -notmatch "people" } | 
    Where-Object{ $_.primarySMTPAddress -notlike $Address} |
    select Name, Alias, primarySMTPAddress | 
    Export-Csv "C:\Temp_Storage\EMailRetentionTest.csv" -NoTypeInformation -Append
    }
    

    Thursday, August 16, 2018 9:42 PM