Solved

Need help with misbehaving powercli script

Posted on 2013-02-07
4
248 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 40

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If we need to check who deleted a Virtual Machine from our vCenter. Looking this task in logs can be painful and spend lot of time, so the best way to check this is in the vCenter DB. Just connect to vCenter DB(default DB should be VCDB and using…
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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.
This video shows you how easy it is to boot from ISO images for virtual machines with the ISO images stored on a local datastore on the ESXi host.

749 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