Solved

Need help with misbehaving powercli script

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Last article we focus in how to VMware: How to create and use VMs TAGs – Part 1 so before follow this article and perform the next tasks, you should read the first article how to create the TAG before using them in Veeam Backup Jobs.
In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
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 Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

920 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

16 Experts available now in Live!

Get 1:1 Help Now