Solved

Need help adding content in to an array

Posted on 2013-01-23
17
293 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
This is about my first experience with programming Arduino.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

831 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