Unable to format Powershell foreach output and attach it to email body and sub

Welcome Forums General PowerShell Q&A Unable to format Powershell foreach output and attach it to email body and sub

Viewing 13 reply threads
  • Author
    Posts
    • #195125
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      I’m scheduling script if windows scheduled task is disabled from the given list then send email to me with Taskname in email subject and Taskinfo in body.

      Email part is working fine

      only I’m unable to format email body with disabled scheduler name and add disabled task name in to subject.

      Please help me to format email body and add disabled task name to subject.

      Here is my script-

      [/crayon]
    • #195137
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,321
      Helping Hand
      Rank: Community Hero

      Hello,

      Try this…

      Thank you.

    • #195161
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      Thank you for the reply !!

      getting error

      Send-MailMessage : Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘Body’. Specified method is not supported.

      Can not convert system.object to system.string

      Thanks,

    • #195164
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      $cred=(Get-Credential) #$tasknamelist = Import-Csv “C:\Users\kaargade\Documents\disabledtask.csv” $tasknamelist = Get-Content “C:\Users\kaargade\Documents\disabledtask.txt” $Body = @() foreach ($task in $tasknamelist) { $service = Get-ScheduledTask -TaskName $task if ($service.State -eq “Disabled”) { $Body += “$task is not running” } } if ($Body) { $From = “[email protected]” $To = “[email protected]” $Cc = “[email protected]” #$Attachment = “C:\temp\Some random file.txt” $computer = $env:computername $Subject = “Task Scheduler is disabled on $computer” $SMTPServer = “outlook.office365.com” $SMTPPort = “587” Send-MailMessage -From $From -to $To -Cc $Cc -Subject “Task Scheduler is disabled on $computer”` -Body ($Body | Out-String) -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl ` -Credential $cred }

      Thank you it’s working!! can you please help add disabled taskname to Email subject line, as body is fine.

    • #195176
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,321
      Helping Hand
      Rank: Community Hero

      Thank you it’s working!! can you please help add disabled taskname to Email subject line, as body is fine.

      Can you please elaborate a bit?

      Thank you.

    • #195179
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      Thank you for reply!!

       

      i want, if task is disabled from given list and condition true, email should trigger and in the email body, Task Name and Taskpath and task state should be added.

      and in email subject line Task name should be added.

      and if more than two task disabled, task name separated with comma or new line.

      Thanks,

    • #195185
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      1)Task Name should be added to the Email subject, if multiple schedulers matches, name with commas added to subject.
      2)and in the email body Disabled schedulers named added with new line.
      3)In Body Task name and state and taskpath shold be added.

      Thanks,

    • #195188
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,321
      Helping Hand
      Rank: Community Hero

      Okay good, where did you stuck? Did you try anything? In-fact by referring to the code above you can achieve what you want, please give it a try. Thank you.

    • #195206
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      I got it working subject part,

      i only stuck with comma between two scheduler names. Or new line between to two schedulers name. In body and subject

      Alert:Recovery-Check;

      Device-Sync;

      Automatic-Device-Join Task

      Scheduler is disabled on DESKTOP-

       

      This is I’m getting currently in subject.

      Alert:Recovery-Check Device-Sync Automatic-Device-Join Task Scheduler is disabled on DESKTOP-ARRSKL1

    • #195209
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      Is there any way to add comma or new. Line character in between two string in empty array.

      [email protected]()

    • #195212
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,321
      Helping Hand
      Rank: Community Hero

      Is there any way to add comma or new. Line character in between two string in empty array.

      [email protected]()

      After you added the required, you can join with -join, you can’t work with an empty array

      Thank you.

    • #195233
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      Thank you very much!!

      i managed to format body with -BodyAsHtml parameter.

      How ever i could not able implement your above solution.

      $Body -Join ‘,’

      please tell me where i have to -join.

      i tried to join in if satement it’s bot working.

    • #195266
      Participant
      Topics: 15
      Replies: 1761
      Points: 3,167
      Helping Hand
      Rank: Community Hero

      This can be simplified a bit. You have not mentioned how many tasks you are auditing or how many computers you are auditing. If it was 5 tasks and 10 computers, you would get up to 50 emails. The first suggestion is to send a single email per computer, so you could do something like this:

      This would give you a subject and body respectively:

      This could also be a single email. You can loop through each computer, create a CimSession and remotely get the tasks. The goal would be getting an output like this:

      Now you would have all of the results in a single object which may even alleviate the need to send email.

    • #195269
      Participant
      Topics: 1
      Replies: 8
      Points: 31
      Rank: Member

      I got everything, I wanted.

      Thank very much for the help!!

      Really appreciated!!

Viewing 13 reply threads
  • The topic ‘Unable to format Powershell foreach output and attach it to email body and sub’ is closed to new replies.