Link to home
Start Free TrialLog in
Avatar of Thaidog
ThaidogFlag for United States of America

asked on

Need help adding content in to an array

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

Avatar of SubSun
SubSun
Flag of India image

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

Avatar of Thaidog

ASKER

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)
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

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

Avatar of Thaidog

ASKER

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

Hmm.. There is a comma (,) missing after @{n="vmhost";e={$vmhost}}
Select @{n="vmhost";e={$vmhost}},ScsiLun,State
Avatar of Thaidog

ASKER

Ok i corrected that but it now does not return anything in the terminal when I run it....
Are you getting data in email?
Avatar of Thaidog

ASKER

No...the script just "hangs" like it never gets to the point to send an email.
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

Avatar of Thaidog

ASKER

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)
Avatar of Thaidog

ASKER

Never mind.... it's the comma again lol...
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thaidog

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thaidog

ASKER

Eureka! It works!!!
Great!!.. Don't forget to mark answer..:-)