Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with misbehaving powercli script

Posted on 2013-02-07
4
Medium Priority
?
261 Views
Last Modified: 2013-02-12
I wrote the following script to send out a report via email only if there are errors to report:

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

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

        Get-ScsiLunPath | `

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

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

}

if ( $deadpaths.count -gt 0 ) {

    $ol = New-Object -comObject Outlook.Application

    $mail = $ol.CreateItem(0)

    $Mail.Recipients.Add("name@user.com")

    $Mail.Subject = "vSphere Bad Paths Report"

    $Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)

    $Mail.Send()

}

Open in new window


The problem is it is still sending a blank report even if there is no problems - so basically the if statement in the second half is not working. Any ideas on how to do this correctly?
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
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38863683
add before the ForEach :

$deadpaths = @()

Open in new window


change line 3 to:
 $deadpaths += ,Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

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

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

Open in new window

0
 
LVL 1

Author Comment

by:Thaidog
ID: 38863777
Does not seem to like the comma:

Missing expression after unary operator ','.
At C:\Users\mcadamtt\SkyDrive\PowerShell\Scripts\get_bad_paths.ps1:9 char:20
+     $deadpaths += , <<<< Get-ScsiLun -vmhost $vmhost | `
    + CategoryInfo          : ParserError: (,:String) [], ParentContainsErrorR 
   ecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 38863795
that should do:

$deadpaths=@()
ForEach ($vmhost in (Get-Datacenter "operations" | Get-Vmhost | Sort))  { 

    Get-ScsiLun -vmhost $vmhost | `

        Get-ScsiLunPath | `

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

                Select @{n="vmhost";e={$vmhost}},ScsiLun,State |%{
				$deadpaths +=, $_
				}

}

Open in new window

0
 
LVL 41

Expert Comment

by:footech
ID: 38866633
@sedgwick - I'm not familiar with that syntax, what is the "+=," supposed to do or how does it work?

If that doesn't work, this should for the first part.
$deadpaths = @()
ForEach ($vmhost in (Get-Datacenter "operations" | Get-Vmhost | Sort))
{
  $Properties = Get-ScsiLun -vmhost $vmhost |
    Get-ScsiLunPath |
    where {$_.State -eq "Dead"} |
    Select @{n="vmhost";e={$vmhost}},ScsiLun,State
  If ($Properties)
  {
    $deadpaths += $Properties
  }
}

Open in new window

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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