Solved

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

Posted on 2013-01-28
4
450 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

758 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

21 Experts available now in Live!

Get 1:1 Help Now