Powershell:如何格式化 Get-Childitem 的电子邮件?
简而言之,我正在尝试从列出目录内容的 Powershell 脚本发送一封电子邮件。
为了做到这一点,我将文本存储在一个变量中,然后将该变量插入到 Send-MailMessage
中。
我的问题是这样的。当我按如下方式将对象通过管道传输到 Out-String
时,它不会插入换行符:
$mailbody += "<p>" + (get-childitem $path | select-object Name | Out-String -width 40) + "</P>"
显然,当在提示符下输入 Get-Childitem
时,输出的格式很好带有换行符,但是当存储到变量然后通过电子邮件发送(在 HTML 电子邮件中)时,没有换行符,这会导致无法读取的长文件名字符串。
我该怎么做?
Simply put, I'm trying to send an email from a Powershell script which lists the contents of a directory.
In order to do this, I'm storing text in a variable, then inserting this variable into Send-MailMessage
.
My problem is this. When I pipe the object to Out-String
as follows, it doesn't insert newlines:
$mailbody += "<p>" + (get-childitem $path | select-object Name | Out-String -width 40) + "</P>"
Obviously, when Get-Childitem
is entered at the prompt, the output is nicely formatted with newlines, however when stored to a variable then emailed (in an HTML email), there are no newlines, which results in an unreadable, long string of filenames.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ConvertTo-Html
cmdlet 来执行此操作:它将为您创建一个可以在 HTML 电子邮件中发送的漂亮表格。
-fragment
部分删除了头部和身体等,只给出了表格。You can use the
ConvertTo-Html
cmdlet to do this:It will create a nice table for you that can be sent in an HTML email. The
-fragment
part removes the head and body etc. and gives only the table.使用
ConvertTo-Html
cmdlet 怎么样?填写您的环境信息。编辑 - 这可行,但电子邮件看起来很糟糕。如果您希望它看起来与在 PowerShell 窗口中执行
dir
时完全相同,请使用以下命令:How about using the
ConvertTo-Html
cmdlet? Fill in your environment info.EDIT - This works, but the email looks like crap. If you want it to look exactly like it does when you do a
dir
in the PowerShell window use this: