Solved

Need help with misbehaving powercli script

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Advanced tutorial on how to run the esxtop command to capture a batch file in csv format in order to export the file and use it for performance analysis. He demonstrates how to download the file using a vSphere web client (or vSphere client) and exp…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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