Reputation: 856
I am studying the mailkit library, I found just such a construction in one line in c#
msg.Body = new TextPart("html") { Text = "<b>html content</b>" };
on Powershell I can do as many as three lines
$TextPart = [MimeKit.TextPart]::new("html")
$TextPart.Text = "<b>html content</b>"
$msg.Body = $TextPart
Is it possible in powershell to also write this on one line?
Upvotes: 2
Views: 144
Reputation: 437638
To complement Daniel's helpful answer with a more convenient PSv3+ alternative, where you can cast a hashtable @{ ... }
or custom object ([pscustomobject] @{ ... }
) to the target type:
[MimeKit.TextPart] @{ Text = '<b>html content</b>' }
See this answer for a comprehensive discussion of the prerequisites for and constraints on this technique (equally applies to use of New-Object
).
Upvotes: 3
Reputation: 5114
It is possible to also simplify this in PowerShell
$msg.Body = New-Object MimeKit.TextPart -ArgumentList 'html' -Property @{Text = '<b>html content</b>' }
The -Property parameter of New-Object will accept a hashtable of property names:property values where you can specify as many properties as you like.
Upvotes: 2