Solved

Need to add an if / then statement to a powercli script

Posted on 2013-01-28
4
451 Views
Last Modified: 2013-06-20
I have the following script:

$deadpaths = @()

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

$deadpaths += Get-ScsiLun -vmhost $vmhost | `
    
    Get-ScsiLunPath | `
    
        where {$_.State -ne "Dead"} | `
    
    Select @{n="vmhost";e={$vmhost}},ScsiLun,State

}

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("name@mail.com")
$Mail.Subject = "Report"
$Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)
$Mail.Send()

Open in new window

I would like to add a if / then that only sends the e-mail out if their are dead paths found. There should be the word Dead in the array generated that I guess could be used to check for content but I am not sure how to use that to create the statement.... any ideas?
0
Comment
Question by:Thaidog
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 38829089
To be honest, I'm not familiar with the commands relating to VMs and LUNs in the script above, but it kind of looks like $deadpaths is only added to when state doesn't equal "Dead", which to me says that $deadpaths includes paths that are alive.
Sorry if I've completely misunderstood.  If $deadpaths really does only include dead paths, here's an answer to your question.
$deadpaths = @()

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

$deadpaths += Get-ScsiLun -vmhost $vmhost | `
    
    Get-ScsiLunPath | `
    
        where {$_.State -ne "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@mail.com")
$Mail.Subject = "Report"
$Mail.HTMLBody = $($deadpaths | ConvertTo-HTML | Out-String)
$Mail.Send()
}

Open in new window

Basically it just checks whether $deadpaths has been added to, and if so sends the email.
0
 

Author Comment

by:Thaidog
ID: 38829187
Yeap that should work... I'll let you know tomorrow - thanks!
0
 

Author Comment

by:Thaidog
ID: 38829212
Actually now that I think about it the output has one line at the top that might be there regardless if there is any actual output - eg;

vmhost                                      ScsiLun                                                             State

server.domain.net naa.60060e8006d299000000d299000000e9 Dead

server.domain.net naa.60060e8006d299000000d299000000e9 Dead

server.domain.net naa.60060e8005723e000000723e00000057 Dead

[Modified by SouthMod]
0
 
LVL 39

Expert Comment

by:footech
ID: 38829349
Until there is a match, the $deadpaths array should be empty.  Those headers that you see don't exist as an object, they are just properties, and are displayed because of the default behavior of Format-Table, which you could change with the -hideTableHeaders parameter.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article will help you understand what HashTables are and how to use them in PowerShell.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 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

19 Experts available now in Live!

Get 1:1 Help Now