locked
remove various accepted domains (exchange 2007) from a list file via powershell RRS feed

  • Question

  • hello!

    i'm a newbie with powershell and i'm trying to find if is it possible to remove a lot of exchange 2007 accepted domains from a csv file using powershell.

    I want to delete accepted domains contained in a csv, it has only domains name and powershell requires the name , that is different from the domain.

    example of accepted domain in my organization:

    Name                           DomainName                DomainType            Default
    ----                           ----------                     ----------                 -------
    Domain0001         domain1.com     Authoritative         False 

    Domain0002 hello1.com Authoritative False

    I've only a csv with domains name:

    domain.csv:

    Domain

    domain1.com

    hello1.com

    Deleting an accepted domain via powershell requires the name so I need to extract first the name of , i've tried with this command and it works:

    Get-AcceptedDomain | Where{$_.DomainName -eq 'domain1.com'}

    This works only for 1 domain, I've a lot of domains to delete so it's inviable

    Now, i'm trying to launch this command without success:

    import-csv domain.csv | foreach {Get-AcceptedDomain | Where{$_.DomainName -eq '$_.Domain'}}

    Probably there is a syntax error, or maybe I just can't do it. 

    Any help? 

    Many thanks in advance!!


    • Edited by kain80 Thursday, February 26, 2015 3:46 PM
    Thursday, February 26, 2015 2:33 PM

Answers

  • Not tested, but I think this should work:

    $DomainNames = 
    Import-CSV 'c:\somedir\domain.csv' |
    Select -ExpandProperty Domain
    
    
    
    foreach ($DomainName in $DomainNames)
    { Get-AcceptedDomain |
      Where {$_.DomainName -eq $DomainName } |
      foreach { Remove-AccceptedDomain -Identity $_.Name }
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by kain80 Thursday, February 26, 2015 6:36 PM
    Thursday, February 26, 2015 2:57 PM
  • Don't know what to tell you, then.  If I create a test file with that data, the Import-CSV works for me:

    @'
    Domain
    3414257440.domain.com
    domain1.domain.com
    '@ | set-content c:\testfiles\domain.csv
    
    $DomainNames = 
    Import-CSV 'c:\testfiles\domain.csv' |
    Select -ExpandProperty Domain
    
    $DomainNames
    
    3414257440.domain.com
    domain1.domain.com

     

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Thursday, February 26, 2015 5:53 PM
    • Marked as answer by kain80 Thursday, February 26, 2015 6:36 PM
    Thursday, February 26, 2015 5:53 PM

All replies

  • Not tested, but I think this should work:

    $DomainNames = 
    Import-CSV 'c:\somedir\domain.csv' |
    Select -ExpandProperty Domain
    
    
    
    foreach ($DomainName in $DomainNames)
    { Get-AcceptedDomain |
      Where {$_.DomainName -eq $DomainName } |
      foreach { Remove-AccceptedDomain -Identity $_.Name }
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by kain80 Thursday, February 26, 2015 6:36 PM
    Thursday, February 26, 2015 2:57 PM
  • hello mjolinor, thanks for your answer

    I get an error:

    Select-Object : Cannot expand property "Domain" because it has nothing to expand.
    At line:2 char:7
    + Select  <<<< -ExpandProperty Domain
    Select-Object : Cannot expand property "Domain" because it has nothing to expand.
    At line:2 char:7
    + Select  <<<< -ExpandProperty Domain

    Thursday, February 26, 2015 3:21 PM
  • Then your CSV file is not as described in your post.  Can you post the exact contents of that file?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 3:25 PM
  • you are right, i have done an error detailing the csv

    i've edited my initial post:

    the csv is :

    Domain
    3414257440.domain.com
    domain1.domain.com



    • Edited by kain80 Thursday, February 26, 2015 3:47 PM
    Thursday, February 26, 2015 3:43 PM
  • Are there any trailing spaces after 'Domain'?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 4:38 PM
  • nope! there are no spaces, double checkd!
    Thursday, February 26, 2015 5:26 PM
  • Don't know what to tell you, then.  If I create a test file with that data, the Import-CSV works for me:

    @'
    Domain
    3414257440.domain.com
    domain1.domain.com
    '@ | set-content c:\testfiles\domain.csv
    
    $DomainNames = 
    Import-CSV 'c:\testfiles\domain.csv' |
    Select -ExpandProperty Domain
    
    $DomainNames
    
    3414257440.domain.com
    domain1.domain.com

     

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Thursday, February 26, 2015 5:53 PM
    • Marked as answer by kain80 Thursday, February 26, 2015 6:36 PM
    Thursday, February 26, 2015 5:53 PM
  • maybe is something related with the powershell version i'm using? 

    powershell 1.0 is installed on the server

    Thursday, February 26, 2015 5:59 PM
  • If it is I can't test that.  Don't have anything that still has V1 on it.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 6:07 PM
  • mjolinor, i've managed to charge $DomainNames

    $DomainNames = Import-CSV 'c:\temp\ps\test\test.csv' | foreach {$_.Domain}

    [PS] C:\temp\ps\test>$Domainnames
    3414257440.domain.com
    domain1.domain.com

    now what? i can use the rest of the code as you have written?

    Thursday, February 26, 2015 6:18 PM
  • The rest of the code should work for that.

     If you want, just add -WhatIf to Remove-AcceptedDomain and test it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 6:21 PM
  • thanks mjolinor! that worked like a charm!

    it's a bit slow , but it's because of the enormous numbers of accepted domain that I have into exchange!

    many many thanks!!

    Thursday, February 26, 2015 6:36 PM
  • Glad to help!

    It could probably be optimized to speed it up, but I'd be afraid of running into compatibility issues again.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 6:38 PM