VMware Cloud Community
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

How to Print if there is any output for the command in HTML File instead of all?

Hi All,

I have got this html code from Hostile Coding and I have given my input to this to get my desired output.Below Script Works fine.

Problem here is when I am executing below Script I am getting output in HTML File with all details.

Example: If any VM is not connected to CD ROM then also in the output html file I am seeing heading as "CD-ROM Connected to VM"- Which I doesn't require as there is no such VM.

Is there any way that I can print only if the command has output in HTML File.

Please suggest on this.

$OutputPath=Get-Date -UFormat "C:\users\$env:username\desktop\Reports\ /%B /%Y-%b-%d @ %I-%M%p.html"

$Css="<style>

body {

    font-family: Verdana, sans-serif;

    font-size: 14px;

  color: #666666;

  background: #FEFEFE;

}

#title{

  color:#90B800;

  font-size: 30px;

  font-weight: bold;

  padding-top:25px;

  margin-left:35px;

  height: 50px;

}

#subtitle{

  font-size: 11px;

  margin-left:35px;

}

#main {

  position:relative;

  padding-top:10px;

  padding-left:10px;

  padding-bottom:10px;

  padding-right:10px;

}

#box1{

  position:absolute;

  background: #F8F8F8;

  border: 1px solid #DCDCDC;

  margin-left:10px;

  padding-top:10px;

  padding-left:10px;

  padding-bottom:10px;

  padding-right:10px;

}

#boxheader{

  font-family: Arial, sans-serif;

  padding: 5px 20px;

  position: relative;

  z-index: 20;

  display: block;

  height: 30px;

  color: #777;

  text-shadow: 1px 1px 1px rgba(255,255,255,0.8);

  line-height: 33px;

  font-size: 19px;

  background: #fff;

  background: -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);

  background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));

  background: -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);

  background: -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);

  background: -ms-linear-gradient(top, #ffffff 1%,#eaeaea 100%);

  background: linear-gradient(top, #ffffff 1%,#eaeaea 100%);

  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );

  box-shadow:

  0px 0px 0px 1px rgba(155,155,155,0.3),

  1px 0px 0px 0px rgba(255,255,255,0.9) inset,

  0px 2px 2px rgba(0,0,0,0.1);

}

table{

  width:100%;

  border-collapse:collapse;

}

table td, table th {

  border:1px solid #98bf21;

  padding:3px 7px 2px 7px;

}

table th {

  text-align:left;

  padding-top:5px;

  padding-bottom:4px;

  background-color:#90B800;

color:#fff;

}

table tr.alt td {

  color:#000;

  background-color:#EAF2D3;

}

</style>"

#These are divs declarations used to properly style HTML using previously defined CSS

$PageBoxOpener="<div id='box1'>"

$one="<div id='boxheader'>vCenter Services</div>"

$BoxContentOpener="<div id='boxcontent'>"

$PageBoxCloser="</div>"

$br="<br>"

$two="<div id='boxheader'>DataStore</div>"

$three="<div id='boxheader'>DataStore Mapped With Hosts</div>"

$four="<div id='boxheader'>VM's Located on Local Storage</div>"

$five="<div id='boxheader'>VM's in Inconsistent Folders</div>"

$six="<div id='boxheader'>SCSI Bus Sharing(Virtual) in VM's</div>"

$seven="<div id='boxheader'>CD-ROM Connected to VM</div>"

$eight ="<div id='boxheader'>ISO Mounted to VM</div>"

$nine ="<div id='boxheader'>Physical Nic Card Below 1GB</div>"

$ten ="<div id='boxheader'>Host Configuration Issues</div>"

#Get vCenter Service Info

$vcservice = Get-Service -ComputerName srti003a vpxd,vctomcat,VMWareCertificateService,VMwareDirectoryService,VMwareIdentityMgmtService,VMwareKdcService,vmwarelogbrowser,VMwareSTS,vimQueryService,vmware-network-coredump,vmware-network-coredump-webserver,vimPBSM,vmware-ufad-vci,RpcEptMapper,DcomLaunch,RpcSs,VMTools | where {$_.Status -ne "Running"} | Select-Object Name,DisplayName,Status | ConvertTo-HTML -Fragment

#Get DataStore Issue Info

$dc = get-datastore | where {$_.State -eq "Unavailable"} | select-object Name,State | ConvertTo-HTML -Fragment

#Get DataStore Issue Mapped with Hosts Info

$dci = get-datastore | where {$_.State -eq "Unavailable"} | select-object Name,State

$maph = Get-VMHost -Datastore $dci.name | select Name | ConvertTo-HTML -Fragment

#Get VM Located on Local Storage which are registered in Inventory

$vmls = Get-Datastore  | where{$_.Name -match "local|stor"} | Get-VM | Get-HardDisk | select @{N='Name';E={$_.Parent}},@{N='Filename';E={$_.Filename.split('/')[0]}} | ConvertTo-HTML -Fragment

#Get VM Name Mismatch in Inventory and Folder Path

$vmi = Get-View -ViewType VirtualMachine |

where{$_.Name -ne $_.Summary.Config.VMPathName.Split('/] ')[2]} |

select Name,@{N='Path';E={$_.Summary.Config.VMPathName.Split('/')[0]}} | ConvertTo-HTML -Fragment

#SCSI Bus Sharing in VM

#$phy = Get-Cluster | Get-VMHost | Get-VM | Get-ScsiController | Where-Object {$_.BusSharingMode -eq ‘Physical’} | Select {$_.Parent.Name}, {$_.Parent.Host}, BusSharingMode | Sort {$_.Parent.Host} | ConvertTo-HTML -Fragment

$vir = Get-Cluster | Get-VMHost | Get-VM | Get-ScsiController | Where-Object {$_.BusSharingMode -eq ‘Virtual’} | Select {$_.Parent.Name}, {$_.Parent.Host}, BusSharingMode | Sort {$_.Parent.Host} | ConvertTo-HTML -Fragment

#CD-ROM Connected to VM

$cd = get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name | ConvertTo-HTML -Fragment

#ISO Mounted to VM

$iso = get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }} | ConvertTo-HTML -Fragment

#Physical NIC Card Speed below 1GB list

$nic = Get-VMHostNetworkAdapter | Where{$_.BitRatePerSec -ne "0" -and $_.BitRatePerSec -ne "1000" -and $_.BitRatePerSec -ne "10000"} | Where{$_.Name -match "vmnic"} | select Name,VMHost |  ConvertTo-HTML -Fragment

#Host Configuration Issues

$hi = Get-VMHost | select @{N='HostName';E={$_.Name}},@{N='Message';E={$_.ExtensionData.ConfigIssue.FullFormattedMessage}} | where{$_.Message -match "[string]"} | ConvertTo-HTML -Fragment

#Create HTML report

#-Head parameter could be omitted if header is declared in body

ConvertTo-Html -Title "vCheck" -Head "<div id='title'>PowerCLI Reporting</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>

" -Body " $Css $PageBoxOpener $one $BoxContentOpener $vcservice $PageBoxCloser $br $two $BoxContentOpener $dc $PageBoxCloser $br $three $BoxContentOpener $maph $PageBoxCloser $four $BoxContentOpener $vmls $PageBoxCloser $five $BoxContentOpener $vmi $PageBoxCloser $six $BoxContentOpener $vir $PageBoxCloser $seven $BoxContentOpener $cd $PageBoxCloser $Eight $BoxContentOpener $iso $PageBoxCloser $Nine $BoxContentOpener $nic $PageBoxCloser $Ten $PageBoxOpener $hi $PageBoxCloser

" | Out-File -filepath $OutputPath

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You seem to have blank lines, which shouldn't be there.

The last 3 lines of the script should be

ConvertTo-Html -Title "vCheck" -Head "<div id='title'>PowerCLI Reporting</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>" `

    -Body $Body |

Out-File -filepath $OutputPath


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
20 Replies
LucD
Leadership
Leadership
Jump to solution

Try changing the end of the script like this

$empty = "<table>$([char]13)$([char]10)</table>$([char]13)$([char]10)"

if($dc -eq $empty -and $maph -eq $empty -and $vmls -eq $empty -and $vmi -eq $empty -and $vir -eq $empty -and $cd -eq $empty -and $iso -eq $empty -and $nic -eq $empty -and $hi -eq $empty){

    Write-Output "No results found"

}

else{

    #Create HTML report

   

    #-Head parameter could be omitted if header is declared in body

    ConvertTo-Html -Title "vCheck" -Head "<div id='title'>PowerCLI Reporting</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>" `

        -Body " $Css $PageBoxOpener $one $BoxContentOpener $vcservice $PageBoxCloser $br $two $BoxContentOpener $dc $PageBoxCloser $br $three $BoxContentOpener $maph $PageBoxCloser $four $BoxContentOpener $vmls $PageBoxCloser $five $BoxContentOpener $vmi $PageBoxCloser $six $BoxContentOpener $vir $PageBoxCloser $seven $BoxContentOpener $cd $PageBoxCloser $Eight $BoxContentOpener $iso $PageBoxCloser $Nine $BoxContentOpener $nic $PageBoxCloser $Ten $PageBoxOpener $hi $PageBoxCloser" |

    Out-File -filepath $OutputPath

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Thanks for your support. Looks like else command is not getting recognized.

I am getting below error.

else : The term 'else' is not recognized as the name of a cmdlet, function,

script file, or operable program. Check the spelling of the name, or if a path

was included, verify that the path is correct and try again.

At line:1 char:1

+ else{

+ ~~~~

    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoun

   dException

    + FullyQualifiedErrorId : CommandNotFoundException

Please suggest on this.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that your copy had a problem.

I attach the code as a file, try with that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Now Script is executing but still It is listing all.

Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach the script you are currently using as a file attachment ?

Remove all sensitive data first


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Please find the script in the attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with the attached update


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have tried and still it is displaying all. Please find the script in the attachment.

Please suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Seems to be working for me, are you sure there is no output generated for one of the components ?

You could try to comment out the components one by one, to check which one might be causing this.

For example

#Get vCenter Service Info

$vcservice = $null | ConvertTo-HTML -Fragment


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

As there is no output for some components as I have cross checked it by executing individually.

As per your suggestion I am trying to find out which one is causing the problem.

I will keep you posted, thanks.

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

tHi Lucd,

I have tried one by one but I am not seeing any output while executing in Powercli and I am getting output in html as in attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

But there is output in the HTML file.

But wait a minute, my script only suppresses the creation of the HTML file , when none of the components produces any output.

Are you perhaps looking for a way to suppress a specific component, when that component has no output ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution


Yes Lucd, I am looking for the same.

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

I am looking for the same Lucd. Is there any way to supress specific component if it's output is Empty.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What you can do is produce each fragment conditionally, and collect the ones with content in a variable ($Body)

Something like this

$Body = "$Css $PageBoxOpener"

#Get vCenter Service Info

$vcservice = Get-Service -ComputerName srti003a vpxd,vctomcat,VMWareCertificateService,VMwareDirectoryService,VMwareIdentityMgmtService,VMwareKdcService,vmwarelogbrowser,VMwareSTS,vimQueryService,vmware-network-coredump,vmware-network-coredump-webserver,vimPBSM,vmware-ufad-vci,RpcEptMapper,DcomLaunch,RpcSs,VMTools | where {$_.Status -ne "Running"} | Select-Object Name,DisplayName,Status

if($vcservice){

    $Body += "$one $BoxContentOpener $($vcservice | ConvertTo-HTML -Fragment) $PageBoxCloser"

}

#Get DataStore Issue Info

$dc = get-datastore | where {$_.State -eq "Unavailable"} | select-object Name,State | ConvertTo-HTML -Fragment

if($dc){

    $Body += "$br $two $BoxContentOpener $($dc | ConvertTo-HTML -Fragment) $PageBoxCloser"

}

At the end you then need to do something like this

#Create HTML report

#-Head parameter could be omitted if header is declared in body

ConvertTo-Html -Title "vCheck" -Head "<div id='title'>PowerCLI Reporting</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>" `

    -Body $Body |

Out-File -filepath $OutputPath

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

I have tried in the same way but the problem is now none of the component output is displaying. When I am executing some components individually I am having output but in HTML it doesn't print any output.

Please find the script in the attachment.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you 'll have the check the variables that are used in the If-statements.

Is any of these having a value ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Below 3 Values has an output.

#Get Datastore issue info

#Get Datastore issue mapped with Hosts Info

#Get VM Name Mismatch in inventory and Folder Path.

But Still it is not printing these 3 output in HTML. As I have executed these 3 cmdlets individually in Powercli and output comes.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have blank lines, which shouldn't be there.

The last 3 lines of the script should be

ConvertTo-Html -Title "vCheck" -Head "<div id='title'>PowerCLI Reporting</div>$br<div id='subtitle'>Report generated: $(Get-Date)</div>" `

    -Body $Body |

Out-File -filepath $OutputPath


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos