Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Sending each item of array as new line.

$
0
0

I want to send each item in that array as new line:

$str = Get-Service -DisplayName Windows* | foreach {$_.DisplayName} | Out-String

if I run that script:

$str = Get-Service -DisplayName Windows* | foreach {$_.DisplayName} | Out-String

Write-Host $str

Here is a result:

Windows Audio Endpoint Builder
Windows Audio
Windows Event Log
Windows Presentation Foundation Font Cache 3.0.0.0
Windows CardSpace
Windows Firewall
Windows Installer
Windows Modules Installer
Windows Time
Windows Color System
Windows Event Collector
Windows Error Reporting Service
Windows Management Instrumentation
Windows Remote Management (WS-Management)
Windows Update
Windows Driver Foundation - User-mode Driver Framework

But when I run script which sending email:

$str = Get-Service -DisplayName Windows* | foreach {$_.DisplayName} | Out-String

$emailFrom = "email@test1.com"

$emailTo = "email@test2.com"

$subject = "Powershell Test"

$body = $str

$smtpServer = "mail server"

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($emailFrom, $emailTo, $subject, $body)

Result is different:

Windows Audio Endpoint Builder

Windows Audio

Windows Event Log

Windows Presentation Foundation Font Cache 3.0.0.0 Windows CardSpace Windows Firewall Windows Installer Windows Modules Installer Windows Time Windows Color System Windows Event Collector Windows Error Reporting Service Windows Management Instrumentation Windows Remote Management (WS-Management) Windows Update Windows Driver Foundation - User-mode Driver Framework

But when I change my script to don't display that line:

Windows Presentation Foundation Font Cache 3.0.0.0

My new script:

$str = Get-Service -DisplayName "Windows [A-O]*" | foreach {$_.DisplayName} | Out-String

$emailFrom = "email@test1.com"

$emailTo = "email@test2.com"

$subject = "Powershell Test"

$body = $str

$smtpServer = "mail server"

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($emailFrom, $emailTo, $subject, $body)

Works correct:

Windows Audio Endpoint Builder
Windows Audio
Windows Event Log
Windows CardSpace
Windows Firewall
Windows Installer
Windows Modules Installer
Windows Color System
Windows Event Collector
Windows Error Reporting Service
Windows Management Instrumentation
Windows Driver Foundation - User-mode Driver Framework

Where is the problem and how fix it ?

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles