?
Solved

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

Posted on 2013-01-28
4
Medium Priority
?
461 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 2000 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
 
LVL 1

Author Comment

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

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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

771 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