Solved

Need help with misbehaving powercli script

Posted on 2013-02-07
4
236 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
  • 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
 

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 500 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 39

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Will try to explain how to use the VMware feature TAGs in the VMs and create Veeam Backup Jobs using TAGs. Since this article is too long, I will create second article for the Veeam tasks.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

20 Experts available now in Live!

Get 1:1 Help Now