Solved

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

Posted on 2013-01-28
4
458 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
[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
  • 2
4 Comments
 
LVL 40

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 40

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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