Automation

 View Only
  • 1.  Need help with ForEach and Export-CSV loop

    Posted Apr 15, 2014 07:26 AM

    Hi Guys , I am total NOOB in CLI so please excuse stupid question.

    I need your expert advice for very large environment  , and will grant points .

    I have a working script that alerts on our ESXi hosts disconnecting , maintenance mode , and NotResponidng

    Its working well , but need to send one mail out , for all three of our VCenters

    Current script only works for one VC at a time . How do I do ForEach and still pipe to the same CSV and HTML body

    It would be nice to have a HTML and CSV output that looks something like this ( assuming there are current disconnects etc )

    I have one set of credentials , that work on all 3 my VC's

    VC1

    Host1 Disconnected

    Host2 NotResponding

    VC2

    Host3 Disconnected

    Host4 NotResponding

    VC3

    No hosts disconnected/maintemance/notresponding for VC3

    Here is my current code , with passwords etc hidden >

    # Clear old sessions
    Disconnect-VIServer VC01 -Confirm:$false

    # Check VC VC01 for Disconnects and Maintenance mode and Not Responding
    Connect-VIServer -Server VC01 -User FakeUSer -Password FakePassword

    $Report = Get-VMhost -State Disconnected,Maintenance,NotResponding | Select name ,connectionstate

    # parse info to be html ,and give it date stamp

    $ReportHtml = $report | ConvertTo-Html | out-String

    # Write out CSV file and give it a Data Stamp

    $filePath = "C:\PS\Output"
    $filename = "VC01disconnectedHosts"
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')

    $Report | Export-Csv "C:\PS\Output\VC_$Currentdate.csv"

    # Send info with Mail

    Send-MailMessage -To Almero.rademeyer@bcx.co.za -Subject VMwareMorningcheckVC01$CurrentDate `
      -SmtpServer mail.sanlam.co.za -From Almero.rademeyer@bcx.co.za `
      -BodyAsHtml -Body $reportHtml -Attachments "C:\PS\Output\VC_$CurrentDate.csv"

    Thank you in advance for help  .



  • 2.  RE: Need help with ForEach and Export-CSV loop
    Best Answer

    Posted Apr 15, 2014 09:15 AM

    You could use a ForEach loop through all vCenters and cumulate the results in 1 $report.

    Something like this

    $vCenters = "VC01","VC02","VC03"

    # Clear old sessions
    Disconnect-VIServer $vCenters -Confirm:$false

    $report = @()
    # Check VC VC01 for Disconnects and Maintenance mode and Not Responding
    foreach($vc in $vCenters){
     
    Connect-VIServer -Server $vc -User FakeUSer -Password FakePassword

     
    $Report += (Get-VMhost -State Disconnected,Maintenance,NotResponding | Select @{N="VC";E={$vc}},name ,connectionstate)
     
    Disconnect-VIServer $vc -Confirm:$false
    }

    # parse info to be html ,and give it date stamp

    $ReportHtml = ($report | ConvertTo-Html | out-String)

    # Write out CSV file and give it a Data Stamp

    $CurrentDate = Get-Date -Format 'MM-dd-yyyy_hh-mm-ss'
    $filename = "C:\PS\Output\DisconnectedHosts-$($CurrentDate).csv"

    $Report | Export-Csv -Path $filename -NoTypeInformation -UseCulture

    # Send info with Mail

    Send-MailMessage -To Almero.rademeyer@bcx.co.za -Subject VMwareMorningcheck$CurrentDate `
    -SmtpServer mail.sanlam.co.za -From Almero.rademeyer@bcx.co.za `
    -BodyAsHtml -Body $reportHtml -Attachments "C:\PS\Output\DisconnectedHosts-$($CurrentDate).csv"

    I added the vCenter property to each line in the report



  • 3.  RE: Need help with ForEach and Export-CSV loop

    Posted Apr 15, 2014 09:35 AM

    HI LucD

    Awesome , makes sense m will try it asap

    Can you please explain this Select  line ?

    What does += do , and Select @{N="VC";E={$vc}},    ( VC is my current variable for working VCenter , but what does the N and E do ?

    $Report += (Get-VMhost -State Disconnected,Maintenance,NotResponding | Select @{N="VC";E={$vc}},name ,connectionstate)



  • 4.  RE: Need help with ForEach and Export-CSV loop

    Posted Apr 15, 2014 11:21 AM

    That is a "calculated property".

    This allows you to specify properties that are not directly available on the input object that is fed to the Select-Object cmdlet.

    A calculated property is represented as a hash table with 2 fields:

    • The Name (N) field, which defines the name the calculated property will have
    • The Expression (E) field, which allows you to specify an expression whose result will be assigned to the property. In this expression you can go very far; you can reference variables that are not in the input object, and even execute complete script blocks. The value part of this field has to yield a value, which will be assigned to the calculated property


  • 5.  RE: Need help with ForEach and Export-CSV loop

    Posted Apr 15, 2014 12:15 PM

    Thank you very much , already running now .

    Max Points awarded.