Hi Luc ,
Good morning ,
I had a discussion with you earlier to generate html report for a cluster .
i am trying to loop it on a list of clusters using import-csv.
However for some reasons not getting desired output .can you please check and modify it .
$vc=read-host "specify vcenter name"
$cred=Get-Credential
connect-viserver -server $vc -Credential $cred|out-null
$path = 'C:\Users\user1\Desktop\folder1'
$head = @'
<style>
body { background-color:#dddddd;
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>
'@
$clusters = Import-Csv -path "C:\users\user1\desktop\folder1\names.csv"
foreach($line in $clusters)
{
$cluster = Get-Cluster -name $line.clustername
$clu = Get-Cluster -name $cluster|select name|convertto-html -property name -Fragment -PreContent '<h2>clustername </h2>'|out-string
$ds_all=get-datastore -RelatedObject $cluster
$syslog_server="10.x.x.x"
$ds = Get-Datastore -RelatedObject $cluster |
Where-Object{($_.FreeSpaceGB)/($_.CapacityGB) -le 0.15} |
Select Name |
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>DATASTORE WITH LESS THAN 15 PERCENT SPACE </h2>' |
Out-String
$snap = Get-VM -location $cluster | Get-Snapshot |
Select Name,@{N='VM';E={$_.VM.Name}},Created |
ConvertTo-Html -Property Name,VM,Created -Fragment -PreContent '<h2>SNAPSHOT_INFO</h2>' |
Out-String
$powered_off=get-vm -Location $cluster|?{$_.PowerState -eq "poweredoff"}|select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>POWEREDOFF_VMS</h2>' |
Out-String
$vmkernel_vmotion = Get-VMHost -Location $cluster|select name,@{N='vmkernel';E={$_|get-vmhostnetworkadapter -VMKernel|?{$_.vmotionenabled -eq "true"}}}| ConvertTo-Html -Property name,vmkernel -Fragment -PreContent '<h2>VMKERNEL_PORT_VMOTION</h2>' |
Out-String
$syslog_server=Get-VMHost -Location $cluster|select name,@{N='syslogserver';E={Get-VMHostSysLogServer -VMHost $_}}|ConvertTo-Html -Property name,syslogserver -Fragment -PreContent '<h2>SYSLOGSERVER<h2>' | out-string
$windows_vm_tooldsupgrde_needed=get-vm -Location $cluster|?{$_.Guest.GuestFamily -eq 'windowsGuest' -and $_.ExtensionData.guest.toolsversionstatus -eq 'guesttoolsneedupgrade'}|select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>WINDOWS_VM_TOOLS_NEED_UPGRADE</h2>' |
Out-String
$ntp = get-vmhost -Location $cluster|Get-VMHostService|?{$_.key -eq 'ntpd'}|select vmhost,key,running|ConvertTo-Html -Property vmhost,key,running -Fragment -precontent '<h2>NTP<h2>'|Out-String
$version=get-vmhost -location $cluster|select name,build,version,model|ConvertTo-Html -Property name,build,version,model -Fragment -PreContent '<h2>VERSION<h2>'|Out-String
$cluster_properties=Get-Cluster $cluster|select drsenabled,haenabled,HAAdmissionControlEnabled|ConvertTo-Html -Property drsenabled,haenabled,HAAdmissionControlEnabled -Fragment -PreContent '<h2>CLUSTERPROPERTIES<h2>'|Out-String
$datastore_cluster=Get-DatastoreCluster -Location(Get-Datacenter -Cluster $cluster)|select name|ConvertTo-Html -Property name -Fragment -PreContent '<h2>DATASTORECLUSTER<h2>'|Out-String
}
ConvertTo-HTML -head $head -PostContent $clu,$ds,$snap,$powered_off,$vmkernel_vmotion,$windows_vm_tooldsupgrde_needed,$ntp,$version,$cluster_properties,$datastore_cluster,$syslog_server|
Out-String | Out-File -FilePath "$path\test_11feb.html"
You could do something like this
$cred = Get-Credential
Connect-VIServer -server $vc -Credential $cred|out-null
$path = 'C:\Users\user1\Desktop\folder1'
$clusters = Import-Csv -path "C:\users\user1\desktop\folder1\names.csv"
$fragments = @()
foreach ($line in $clusters) {
$cluster = Get-Cluster -name $line.clustername
$vms = Get-VM -location $cluster
$esx = Get-VMHost -Location $cluster
$fragments += Get-Cluster -name $cluster |
Select name |
ConvertTo-Html -Property name -Fragment -PreContent '<h2>clustername </h2>' |
Out-String
$ds_all = Get-Datastore -RelatedObject $cluster
$syslog_server = "10.x.x.x"
$fragments += Get-Datastore -RelatedObject $cluster |
Where-Object {($_.FreeSpaceGB) / ($_.CapacityGB) -le 0.15} |
Select Name |
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>DATASTORE WITH LESS THAN 15 PERCENT SPACE </h2>' |
Out-String
$fragments += $vms | Get-Snapshot |
Select Name, @{N = 'VM'; E = {$_.VM.Name}}, Created |
ConvertTo-Html -Property Name, VM, Created -Fragment -PreContent '<h2>SNAPSHOT_INFO</h2>' |
Out-String
$fragments += $vms | Where-Object {$_.PowerState -eq "poweredoff"}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>POWEREDOFF_VMS</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'vmkernel'; E = {$_ | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.vmotionenabled -eq "true"}}}|
ConvertTo-Html -Property name, vmkernel -Fragment -PreContent '<h2>VMKERNEL_PORT_VMOTION</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'syslogserver'; E = {Get-VMHostSysLogServer -VMHost $_}}|
ConvertTo-Html -Property name, syslogserver -Fragment -PreContent '<h2>SYSLOGSERVER<h2>' |
Out-String
$fragments += $vms | Where-Object {$_.Guest.GuestFamily -eq 'windowsGuest' -and $_.ExtensionData.guest.toolsversionstatus -eq 'guesttoolsneedupgrade'}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>WINDOWS_VM_TOOLS_NEED_UPGRADE</h2>' |
Out-String
$fragments += $esx | Get-VMHostService|? {$_.key -eq 'ntpd'}|
select vmhost, key, running|
ConvertTo-Html -Property vmhost, key, running -Fragment -precontent '<h2>NTP<h2>'|
Out-String
$fragments += $esx |
select name, build, version, model|
ConvertTo-Html -Property name, build, version, model -Fragment -PreContent '<h2>VERSION<h2>'|
Out-String
$fragments += $cluster|
select drsenabled, haenabled, HAAdmissionControlEnabled|
ConvertTo-Html -Property drsenabled, haenabled, HAAdmissionControlEnabled -Fragment -PreContent '<h2>CLUSTERPROPERTIES<h2>'|
Out-String
$fragments += Get-DatastoreCluster -Location(Get-Datacenter -Cluster $cluster)|
select name|ConvertTo-Html -Property name -Fragment -PreContent '<h2>DATASTORECLUSTER<h2>'|
Out-String
}
ConvertTo-HTML -head $head -PostContent $fragments |
Out-String | Out-File -FilePath "$path\test_11feb.html"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What are you getting?
And what is the desired output?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are only getting output for the last cluster in your CSV file, since you are overwriting the fragments in each loop.
How do you intend to display the results?
Each fragment contains all clusters or a separate HTML for each cluster?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
so with above for each_loop i was expecting itraring over clusters .
currently its giving info for one cluster only like below
That's what I just said.
How do you expect the HTML report to look?
- all fragments for a cluster, and then the next cluster
- all clusters together, fragment by fragment?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
all fragments for a cluster, and then the next cluster
But in the same HTML file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this
$cred = Get-Credential
Connect-VIServer -server $vc -Credential $cred|out-null
$path = 'C:\Users\user1\Desktop\folder1'
$clusters = Import-Csv -path "C:\users\user1\desktop\folder1\names.csv"
$fragments = @()
foreach ($line in $clusters) {
$cluster = Get-Cluster -name $line.clustername
$vms = Get-VM -location $cluster
$esx = Get-VMHost -Location $cluster
$fragments += Get-Cluster -name $cluster |
Select name |
ConvertTo-Html -Property name -Fragment -PreContent '<h2>clustername </h2>' |
Out-String
$ds_all = Get-Datastore -RelatedObject $cluster
$syslog_server = "10.x.x.x"
$fragments += Get-Datastore -RelatedObject $cluster |
Where-Object {($_.FreeSpaceGB) / ($_.CapacityGB) -le 0.15} |
Select Name |
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>DATASTORE WITH LESS THAN 15 PERCENT SPACE </h2>' |
Out-String
$fragments += $vms | Get-Snapshot |
Select Name, @{N = 'VM'; E = {$_.VM.Name}}, Created |
ConvertTo-Html -Property Name, VM, Created -Fragment -PreContent '<h2>SNAPSHOT_INFO</h2>' |
Out-String
$fragments += $vms | Where-Object {$_.PowerState -eq "poweredoff"}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>POWEREDOFF_VMS</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'vmkernel'; E = {$_ | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.vmotionenabled -eq "true"}}}|
ConvertTo-Html -Property name, vmkernel -Fragment -PreContent '<h2>VMKERNEL_PORT_VMOTION</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'syslogserver'; E = {Get-VMHostSysLogServer -VMHost $_}}|
ConvertTo-Html -Property name, syslogserver -Fragment -PreContent '<h2>SYSLOGSERVER<h2>' |
Out-String
$fragments += $vms | Where-Object {$_.Guest.GuestFamily -eq 'windowsGuest' -and $_.ExtensionData.guest.toolsversionstatus -eq 'guesttoolsneedupgrade'}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>WINDOWS_VM_TOOLS_NEED_UPGRADE</h2>' |
Out-String
$fragments += $esx | Get-VMHostService|? {$_.key -eq 'ntpd'}|
select vmhost, key, running|
ConvertTo-Html -Property vmhost, key, running -Fragment -precontent '<h2>NTP<h2>'|
Out-String
$fragments += $esx |
select name, build, version, model|
ConvertTo-Html -Property name, build, version, model -Fragment -PreContent '<h2>VERSION<h2>'|
Out-String
$fragments += $cluster|
select drsenabled, haenabled, HAAdmissionControlEnabled|
ConvertTo-Html -Property drsenabled, haenabled, HAAdmissionControlEnabled -Fragment -PreContent '<h2>CLUSTERPROPERTIES<h2>'|
Out-String
$fragments += Get-DatastoreCluster -Location(Get-Datacenter -Cluster $cluster)|
select name|ConvertTo-Html -Property name -Fragment -PreContent '<h2>DATASTORECLUSTER<h2>'|
Out-String
}
ConvertTo-HTML -head $head -PostContent $fragments |
Out-String | Out-File -FilePath "$path\test_11feb.html"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes
thank you very much .I m going to run this tomorrow morning.