Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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