Automation

 View Only
  • 1.  header for each HTML - Vcenters reports

    Posted Mar 04, 2023 03:33 PM

    HI 

    I have a script that run an give me the all VMs that are powered off and more info like notes an so

    the report is an CSV file , and each column on the table i put the info abut the VMs like VMname, poweroff time,note ,Vcenter,,,

    I want to remove the Vcenter ( I have 5 Vcenter)  from column  and put on the column  in  HTNL file

    this is the final line that report to HTML but all in one table 

    Import-csv $excelfile |ConvertTo-Html -Property 'VM','Notes','Vcenter' -Head $Header |
    Out-File $FileHtml -Encoding utf8

     



  • 2.  RE: header for each HTML - Vcenters reports

    Posted Mar 04, 2023 05:01 PM

    And what is the actual question?
    Perhaps you could show a mockup of what you want to achieve?



  • 3.  RE: header for each HTML - Vcenters reports

    Posted Mar 04, 2023 06:08 PM

    I want the title of the vcenter to be above each table
    The name of the VCENTER is in column and I can take it from there
    Just put it in the title 



  • 4.  RE: header for each HTML - Vcenters reports

    Posted Mar 04, 2023 06:38 PM

    You could do something like this

     

    # Just a sample CSS style sheet
    # Adapt to your needs
    
    $head = @’
    <style>
    body { background-color:#cccccc;
           font-family:Tahoma;
           font-size:12pt; }
    td, th { border:1px solid black;
             border-collapse:collapse; }
    th { color:white;
         background-color:black; }
    table, tr, td, th { padding: 2px; margin: 0px }
    table { margin-left:50px; }
    </style>
    ‘@
    
    # Assuming the CSV looks something like this
    # Additional columns in the CSV are also included in the resulting report
    # If not needed, adapt the ExcludeProperty value on the Select-Object cmdlet
    #
    # vCenter,VM,Notes
    # vc1,VM1,'Note1'
    # vc1,VM2,'Note2'
    # vc2,VM3,'Note3'
    
    $infile = 'data.csv'
    $outFile = 'report.html'
    $fragments = Import-Csv -Path $infile -UseCulture |
    #$fragments = ConvertFrom-Csv -InputObject $data |
    Group-Object -property vCenter |
    ForEach-Object -Process {
        $_.Group | Select-Object -ExcludeProperty vCenter |
        ConvertTo-Html -PreContent "<h1>$($_.Name)</h1>" -Fragment
    }
    
    ConvertTo-Html -Head $head -Body $fragments |
    Out-File -FilePath $outFile
    
    Invoke-Item -Path $outFile

     



  • 5.  RE: header for each HTML - Vcenters reports

    Posted Mar 06, 2023 07:57 PM

    HI

    I fix the script and its work fine  - THX

    But now I want to change 2 things  ,

    1. how to remove the name vCenter from the table because I don't need it  ? (now it appears above as I wanted)

    Another thing,
    2. how can I change the title of the Vcenter instead of FQDN - venter1.vmware.com
    will be like - VC- prod
    and so ....

     

    Thanks again 



  • 6.  RE: header for each HTML - Vcenters reports

    Posted Mar 06, 2023 08:02 PM

     wrote:

    I fix the script and its work fine 


    What was wrong with my code?

    In my code the ExcludeProperty parameter should eliminate the vCenter in the table.

    To translate the FQDN to something else you will need a translate table.
    Do you have such a table?
    In which format?

     



  • 7.  RE: header for each HTML - Vcenters reports

    Posted Mar 07, 2023 06:03 AM

    you script its fine 
    I meant that I fix my script with yours 

    about the script that I fix together with yours

    $Header = @"
    <style>
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
    TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    </style>
    "@
    
    $excelfile = "D:\scripts\PowerOffVMs\TempReport\Report_PoweredOffVMs.csv"
    $FileHtml = "D:\scripts\PowerOffVMs\TempReport\PoweredOff.htm"
    $caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'
    
    $PoweredOFFVM = Get-VM| Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019|^sc-|^Windows10" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value.Split('.')[0] -as [int] -gt 20 }
    $PoweredOFFVM |ForEach-Object -Process {
            $obj = New-Object -TypeName PSObject -Property (
                [ordered]@{
                     VM               = $_.Name
                    'Powered Off At'  = ''
                     Notes            = $_.Notes
                    'Powered Off By'  = ''
                    'Powered Off For' = ''
                    "Folder name"     = $_.Folder.Name
                    'Last Backup'     = ''
                     vCenter           = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
                }
            )
            Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
            ForEach-Object -Process {
                    $obj."$($_.Name)" = $_.Value
            }
            $obj
        }
    ########################
    $fragments = Import-Csv -Path $excelfile -UseCulture -Encoding UTF8|
    Group-Object -property vCenter |
    ForEach-Object -Process {
        $_.Group | Select-Object -ExcludeProperty vCenter |
        ConvertTo-Html -PreContent "<h1>$($_.Name)</h1>" -Fragment
    }
    
    ConvertTo-Html -Head $Header -Body $fragments |
    Out-File -FilePath $FileHtml -Encoding utf8 |
    Invoke-Item -Path $FileHtml

     

    your line start after the ##########

    I know that I need to combine  ExcludeProperty vCenter 
    but where ?

     

    thanks again 



  • 8.  RE: header for each HTML - Vcenters reports

    Posted Mar 07, 2023 08:47 AM

    The ExcludeProperty parameter is there on the Select-Object cmdlet.

    You are reading the content of $excelFile but you never write anything to it.



  • 9.  RE: header for each HTML - Vcenters reports

    Posted Mar 07, 2023 12:08 PM

    so.. what I need to exclude the vCenter from table and translate table from FQDN to something else? 



  • 10.  RE: header for each HTML - Vcenters reports

    Posted Mar 07, 2023 12:17 PM

    Do you even read my replies?