1

I am exporting domains in a transport rule and the output does not contain a header so it does not export to CSV properly.

Trying this code:

$CurrentDomains = (Get-TransportRule "domain block").SenderDomainIs

This outputs:

PS C:\Users\user> $CurrentDomains
chess.com
jen.com
bob.com
joe.com
irish.com
ND.com
goirish.com
fieldnotes.com

As you see there is no "title" so when i run an export-csv it just shows up as numbers:

$currentdomains | export-csv c:\temp\domains.csv -notype

The output of the CSV looks like this:

Length
9
7
7
7
7
9
6
11
14

I need the output to look like this with Domain as the header:

Domain
chess.com
domain2.com
domain3.com
domain4.com
fieldnotes.com
goirish.com
mydomain.com

How do I make that happen?

3
  • 2
    $CurrentDomains = Get-TransportRule "domain block" | Select-Object -Property SenderDomainIs should do the trick actually. If you want to rename the property you can use a calculated property to change the header to whatever you like. ;-) Commented Aug 7, 2020 at 23:47
  • That does not give me what I need. it gives me the output SenderDomainIs chess.com jen.com bob.com joe.com irish.com ND.com goirish.com fieldnotes.com with SenderDomainIs as the header but the domains all in one row. Commented Aug 7, 2020 at 23:53
  • You still can use a loop and create a [PSCustomObject]. ;-) Commented Aug 8, 2020 at 0:02

2 Answers 2

2

Olaf's comments should have solved this fine. Here's another way, just different way of doing what he suggested.

Get-TransportRule -Identity 'domain block' | 
    select-Object -ExpandProperty SenderDomainIs | 
        Foreach {
            [pscustomobject]@{'Domain' = $_}
        } | Export-Csv c:\temp\domains.csv -notype

CSV contents

get-content C:\temp\domains.csv

"Domain"
"chess.com"
"jen.com"
"bob.com"
"joe.com"
"irish.com"
"ND.com"
"goirish.com"
"fieldnotes.com"
Sign up to request clarification or add additional context in comments.

Comments

1

Export like this:

$currentdomains | 
    ForEach-Object { return [PSCustomObject]@{Domain = $_}} | 
    Export-Csv ...

Explanation: Export-Csv exports object's properties, not objects itself.
Because your $currentdomains is list of System.Strings, and System.String contains only Length property, it is exported.

So, to export string value, you must export some object, which have property Domain (or anything else you want) and value of this property is string of interest.

The easiest way to create this object in PowerShell is to use covretion from System.Hashtable (also known as key-value collection @{ }) to PSCustomObject. Keys become properties of PSCustomObject and Values become values of it's properties.

So, with ForEach-Object { return [PSCustomObject]@{Domain = $_}} you wrap your string into PSCustomObject object, which gives you Object with single (maybe multiple in other case) property Domain.

Export-Csv enumerates properties of PSCustomObject, creates columns from their names (in this case you have only Domain property, so only one column Domain will be in CSV) and as a value, it uses property's .ToString() method.

Your Domain property has a type System.String, so System.String::ToString() returns string itself.


Alternative way:

(Get-TransportRule "domain block") | 
    ForEach-Object { return [PSCustomObject]@{Domain = $_.SenderDomainIs}} | 
    Export-Csv ...

Alternative way:

(Get-TransportRule "domain block") | 
    Select-Object -Property @( 
        @{ 
            Label = 'Domain'
            Expression = { return $_.SenderDomainIs }
        },
        'SenderDomainIs' # This line is for example:
        # There can be other properties from Get-TransportRule, by their name, like "SenderDomainIs" or by Label-Expression conversion
        ) | 
    Export-Csv ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.