Solved

Need help adding content in to an array

Posted on 2013-01-23
17
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

728 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