尝试将回车符添加到powershell脚本中

时间:2015-04-03 14:01:49

标签: powershell

这是我的第一个powershell脚本,它运行良好,它找到30分钟的文件并通过电子邮件发送结果,我只想添加回车符,以便输出更具可读性。

输出如下: J:\ folder \ lap001 \ testfile J:\ folder \ lap001 \ testfile2 J:\ folder \ lap001 \ testfile3 J:\ folder \ lap002 \ testfile

#Command to get list of folders with logfiles where the logfile is at least 30 minutes old.
$varlogfile = Get-ChildItem -Path "j:\folder" -Recurse -Include "testfile*" | Where-Object {$_.LastWriteTime -le ((Get-Date).AddMinutes(-30))}

#Email setup
$SMTPServer = "server"
$From = "Administrator"
$To = "something"
$Subject = "A Logfile older than 30 minutes has been detected"
$Body = "Logfile(s) older than 30 minutes have been detected in the following folders:

$varlogfile

Please login and attempt to process the files manually, if the manual process fails, open a ticket with something.

From the Admin
"

#Command to send email
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer

exit 0;

1 个答案:

答案 0 :(得分:0)

好的,这一行后不应该太难了

$varlogfile = Get-ChildItem -Path ....

添加另一行以获取换行符分隔文字

$varlogfile =  $varlogfile -join "`r`n"

当数组被转换为字符串时,它们只是插入了一个空格。 -join添加换行符