Solved

Need help adding content in to an array

Posted on 2013-01-23
17
291 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

760 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

22 Experts available now in Live!

Get 1:1 Help Now