Solved

Need help adding content in to an array

Posted on 2013-01-23
17
292 Views
Last Modified: 2013-06-25
The following script is sending out an email for each esx server I have. What I would like to do is send one big email with all the hosts in it... I know it needs to go in an array but I am not certain how to do it.... any ideas?

ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))  { 

    $deadpaths = Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

            where {$_.State -eq "Dead"} | `

                Select ScsiLun,State; 

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("me@email.com")
$Mail.Subject = "Report"
$Mail.Body = "
$vmhost $deadpaths
"
$Mail.Send()

}

Open in new window

0
Comment
Question by:Thaidog
  • 9
  • 8
17 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38810603
Try this script..
I am using Send-MailMessage to send email..
$Body = @()
ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))  { 

    $Body += Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

            where {$_.State -eq "Dead"} | `

                Select @{n="vmhost";e={$vmhost}}ScsiLun,State 

}
Send-MailMessage `
-Subject "Report" `
-Body $($Body | ConvertTo-HTML | Out-String) `
-From "From@email.com" `
-To "me@email.com" `
-SmtpServer "smtp.mail.com" `
-BodyAsHtml

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38813856
Not sure if mine is working lol - I do not have any bad paths right now... mine looks like this:

$deadpaths = @()

ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))  { 

    $deadpaths += Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

            where {$_.State -ne "Dead"} | `

                Select @{n="vmhost";e={$vmhost}}ScsiLun,State

}

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("person@mail.com")
$Mail.Subject = "Report"
$Mail.Body = "
$($Body | ConvertTo-HTML | Out-String) 
"
$Mail.Send()

Open in new window


I changed the -eq to -ne to hopefully get some output but my email just came back:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>HTML TABLE</title> 
</head><body> 
<table> 
</table> 
</body></html> 

Open in new window


(I need to use the email style I am now)
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38813872
Do you get result for following code?
$deadpaths = @()

ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))  { 

    $deadpaths += Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

            where {$_.State -ne "Dead"} | `

                Select @{n="vmhost";e={$vmhost}}ScsiLun,State

}
$deadpaths

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38813900
If you get result then try this..

$deadpaths = @()

ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))  { 
$deadpaths += Get-ScsiLun -vmhost $vmhost |
   Get-ScsiLunPath |
	where {$_.State -ne "Dead"} |
	   Select @{n="vmhost";e={$vmhost}}ScsiLun,State
}

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("person@mail.com")
$Mail.Subject = "Report"
$Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)
$Mail.Send()

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38814446
Getting an error:

Select-Object : A positional parameter cannot be found that accepts argument 'S
ystem.Object[]'.
At C:\Users\mcadamtt\SkyDrive\PowerShell\Scripts\get_bad_paths.ps1:15 char:23
+                 Select <<<<  @{n="vmhost";e={$vmhost}}ScsiLun,State
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB 
   indingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell 
   .Commands.SelectObjectCommand

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38814466
Hmm.. There is a comma (,) missing after @{n="vmhost";e={$vmhost}}
Select @{n="vmhost";e={$vmhost}},ScsiLun,State
0
 

Author Comment

by:Thaidog
ID: 38815730
Ok i corrected that but it now does not return anything in the terminal when I run it....
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38815749
Are you getting data in email?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Thaidog
ID: 38817194
No...the script just "hangs" like it never gets to the point to send an email.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38818357
Hmm... not seeing any issue for script.. Run this code and see if you get the result..
$deadpaths = @()
ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))
{ 
$deadpaths += Get-ScsiLun -vmhost $vmhost | 
Get-ScsiLunPath | 
where {$_.State -ne "Dead"} |
Select @{n="vmhost";e={$vmhost}}ScsiLun,State
}
$deadpaths

Open in new window


If above code gives you result.. then try this...
$deadpaths = @()
ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))
{ 
$deadpaths += Get-ScsiLun -vmhost $vmhost | 
Get-ScsiLunPath | 
where {$_.State -ne "Dead"} |
Select @{n="vmhost";e={$vmhost}}ScsiLun,State
}
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("person@mail.com")
$Mail.Subject = "Report"
$Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)
$Mail.Send()

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38818545
Getting an error:

Select-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At line:1 char:171
+ ForEach ($vmhost in (Get-Datacenter "operations" | Get-Vmhost | Sort)) {$deadpaths += Get-ScsiLun -vmhost $vmhost | G
et-ScsiLunPath | where {$_.State -ne "Dead"} | Select <<<<  @{n="vmhost";e={$vmhost}}ScsiLun,State}
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Open in new window


(Edit: Redacted - Modulus_Twelve)
0
 

Author Comment

by:Thaidog
ID: 38818554
Never mind.... it's the comma again lol...
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38818558
Ahhh.. sorry.. try this.. :-)

$deadpaths = @()
ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort))
{ 
$deadpaths += Get-ScsiLun -vmhost $vmhost | 
Get-ScsiLunPath | 
where {$_.State -ne "Dead"} |
Select @{n="vmhost";e={$vmhost}},ScsiLun,State
}
$deadpaths

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38819699
Ok this is working:

ForEach ($vmhost in (Get-Datacenter "Operations" | Get-Vmhost | Sort)) {$deadpaths += Get-ScsiLun -vmhost $vmhost | Get-ScsiLunPath | where {$_.State -ne "Dead"} | Select @{n="vmhost";e={$vmhost}},ScsiLun,State}

And it puts the information in the array just fine... but this is not sending a body:
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("name@mail.com")
$Mail.Subject = "Report"
$Mail.Body = "$deadpaths"
$Mail.Send()

Open in new window

Nor is:
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("name@mail.com")
$Mail.Subject = "Report"
$Mail.Body = $deadpaths
$Mail.Send()

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 38819868
Try
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("name@mail.com")
$Mail.Subject = "Report"
$Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)
$Mail.Send()

Open in new window

0
 

Author Comment

by:Thaidog
ID: 38820073
Eureka! It works!!!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38820565
Great!!.. Don't forget to mark answer..:-)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now