VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

adding functionalities_powercli

Hi Luc ,

i thought of adding few additional functionalities to attached script which we discussed some days back .

can you suggest if this follows proper power shell script structure ??

i am getting some strange results especially in ntp section .

i have attached the script.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The following might also help understanding what happens when you convert a [string] to a [Boolean].

If the string is not empty, it will always convert to $true, whatever is in the string.

$t = 'false'

[Boolean]$t


$t = 'true'

[Boolean]$t


$t = ''

[Boolean]$t


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure what happened with that attached script, but it's hardly readable.
You seem to have an extra blank before each character.

Is that perhaps an Unicode issue?

Try saving the file as a plan ASCII text


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i copied here .iam trying to add orange color code at each conditions .so that i can fix the non desired conditions while generating the report.

it worked for some conditions but during ntp it gave false results.if that works i will add more conditions as per requirement .

$name=read-host "whats your name "

write-host "Hi " $name "Iam automated HEALTH CHECKER ,i will generate report and will try to remediate based on yes and no and also give recommendations" -ForegroundColor Green

$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\in0079d6\Desktop\Technicolor_script\EU_tc_CLUSTER.csv"

$fragments = @()

$esxi_names_EU = @()

foreach ($line in $clusters) {

   $cluster = Get-Cluster -name $line.clustername

   $vms = Get-VM -location $cluster

   $esx = Get-VMHost -Location $cluster

   $esxi_names_EU += $esx.name

   #$ssh_running_remote = Get-VMHostService -VMHost $esx | Where-Object { $_.key -eq "TSM-SSH" }

   #$fragments += $ssh_running_remote|ConvertTo-Html -Property name -Fragment -PreContent '<h2>remote_ssh</h2>'|out-string

   $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 = 'memoryusagepercent'; E = {

                                                  $per_mem=$_.memoryusageGB/$_.memorytotalGB*100

                                                  $per_mem_round=[math]::Round($per_mem)

                                                   $per_mem_round                              }}|

   ConvertTo-Html -Property name,memoryusagepercent  -Fragment -PreContent '<h2>MEMORY_USAGE_PERCENT</h2>' |

   Out-String

   foreach ($e in $esx)

   {

   $per_mem=$e.memoryusageGB/$e.memorytotalGB*100

   $per_mem_round=[math]::Round($per_mem)

                                                  

if($per_mem_round -ge "90"){

write-host "check  MEMORY usage on host" $e.name -ForegroundColor Yellow

   }}

  

    $fragments += $esx |

  

   select name, @{N = 'cpuusagepercent'; E = {

                                                  $per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100

                                                  $per_cpu_round=[math]::Round($per_cpu)

                                                  $per_cpu_round                              }}|

   ConvertTo-Html -Property name,cpuusagepercent  -Fragment -PreContent '<h2>CPU_USAGE_PERCENT</h2>' |

   Out-String

  

  

   <#%{

   $vm_host=get-vmhost $_

   $per_mem=$vm_host.MemoryUsageGB/$vm_host.MemoryTotalGB*100

   $per_cpu=$vm_host.Cpuusagemhz/$vm_host.CpuTotalMhz*100

   $per_mem_round=[math]::Round($per_mem)

   $per_cpu_round=[math]::Round($per_cpu)

   $vm_host|select name,@{N='memoryusagepercent';E={$per_mem_round}},@{N='cpuusagepercent';E={$per_cpu_round}}|ConvertTo-Html -Property memusgaepercent,cpuusagepercent -Fragment -PreContent '<h2>memory and cpu usage percent</h2>' |

   Out-String

   }#>

 

   $fragments += $esx |

  select name, @{N = 'remote_ssh'; E = { (Get-VMHostService -VMHost $_|?{$_.key -eq "tsm-ssh"}).Running}}|

   ConvertTo-Html -Property name,remote_ssh -Fragment -PreContent '<h2>REMOTESSH_RUNNING</h2>' |

   Out-String

      foreach ($e in $esx)

   {

   $ssh_service=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"})

   $ssh_running_status=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"}).Running

                                                  

if($ssh_running_status -eq "true"){

write-host "i found remote_ssh is enabled on  " $e.name -ForegroundColor Yellow

$response=read-host "would you like to turn it off?"

if($response -eq "yes"){

write-host "based on your response turning off ssh on"$e.Name -ForegroundColor Green

Stop-VMHostService -HostService $ssh_service

}

   }}

   $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

       foreach ($e in $esx)

   {

   $ntp=(Get-VMHostService -VMHost $e|?{$_.key -eq "ntpd"})

   $ntp_running_status=(Get-VMHostService -VMHost $e|?{$_.key -eq "ntpd"}).Running

                                                  

if($ntp_running_status -eq "false"){

write-host "i found ntp  is disabled  on  " $e.name -ForegroundColor Yellow

$response=read-host "would you like to enbale ntp?"

if($response -eq "yes"){

write-host "based on your response enabling ntp on "$e.Name -ForegroundColor Green

Start-VMHostService -HostService $ntp -WhatIf

}

   }}

   $fragments += $esx |

  select name, build, version, model,connectionstate|

   ConvertTo-Html -Property name, build, version, model,connectionstate -Fragment -PreContent '<h2>VERSION$CONNECTION<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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You shouldn't compare a Boolean to a string.
When there is anything in the string, it will always be considered $true.
Only with an empty string will it return $false.

The NTP snippet should be

   foreach ($e in $esx)

   {

   $ntp = (Get-VMHostService -VMHost $e | ? { $_.key -eq "ntpd" })

   $ntp_running_status = (Get-VMHostService -VMHost $e | ? { $_.key -eq "ntpd" }).Running

   if (-not $ntp_running_status)

   {

   Write-Host "I found ntp is disabled on  " $e.name -ForegroundColor Yellow

   $response = Read-Host "Would you like to enbale ntp?"

   if ($response -eq "yes")

   {

   Write-Host "based on your response enabling ntp on "$e.Name -ForegroundColor Green

   Start-VMHostService -HostService $ntp -WhatIf

   }

   }

   }

PS: the same is also true for the snippet where you test the SSH service


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks.it is working but i am stillthinking how the same code works fine for ssh snippet.

also $ntp_running_status  will always have some boolean value either true or false .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You might think that the SSH test is working, but I'm pretty sure it is not.
Because when there is something in the string (in $ssh_running_status) you compare with, it will always return $true.

Try starting the SSH service on one ESXi node and then do the comparison as $ssh_running_status -eq "false"

You will see that the test will not do what you want it to do.

The problem is not what is in $ssh_running_status (beit $true or $false), when you compare it with a string, there will be type casting.
What type of casting is done, is determined by the left operand of the comparison.

Try this example (you might be surprised by the result)

$t = $true

$t -eq 'false'

'false' -eq $t


$t = $false

$t -eq 'false'

'false' -eq $t


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

iamgoing to check this one more time ,Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following might also help understanding what happens when you convert a [string] to a [Boolean].

If the string is not empty, it will always convert to $true, whatever is in the string.

$t = 'false'

[Boolean]$t


$t = 'true'

[Boolean]$t


$t = ''

[Boolean]$t


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaksLuc.

i changed ssh snipet to following .i think this is fine .

however in previous experssion if($ssh_running_status -eq "true")  are we not comparing two strings ?as it is not $true but "true"

  $fragments += $esx |

  select name, @{N = 'remote_ssh'; E = { (Get-VMHostService -VMHost $_|?{$_.key -eq "tsm-ssh"}).Running}}|

   ConvertTo-Html -Property name,remote_ssh -Fragment -PreContent '<h2>REMOTESSH_RUNNING</h2>' |

   Out-String

      foreach ($e in $esx)

   {

   $ssh_service=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"})

   $ssh_running_status=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"}).Running

                                                  

#if($ssh_running_status -eq "true"){

if($ssh_running_status){

write-host "i found remote_ssh is enabled on  " $e.name -ForegroundColor Yellow

$response=read-host "would you like to turn it off?"

if($response -eq "yes"){

write-host "based on your response turning off ssh on"$e.Name -ForegroundColor Green

Stop-VMHostService -HostService $ssh_service

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid not, the Running property is of type Boolean.

As I showed earlier, the left operand of the comparison determines what type of casting will occur.
If the string is on the left, the right one, a Boolean, will be converted to a string.
The other way round, the casting will be to Boolean.
And that is where the issue occurs.

Any string, as long as there is anything in there, beit 'true' or 'flase' or even 'whatever', will convert to $true.

Only an empty string will convert to $false.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

but below is fine?

  $fragments += $esx |

  select name, @{N = 'remote_ssh'; E = { (Get-VMHostService -VMHost $_|?{$_.key -eq "tsm-ssh"}).Running}}|

   ConvertTo-Html -Property name,remote_ssh -Fragment -PreContent '<h2>REMOTESSH_RUNNING</h2>' |

   Out-String

      foreach ($e in $esx)

   {

   $ssh_service=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"})

   $ssh_running_status=(Get-VMHostService -VMHost $e|?{$_.key -eq "tsm-ssh"}).Running

                                                  

#if($ssh_running_status -eq "true"){

if($ssh_running_status){

write-host "i found remote_ssh is enabled on  " $e.name -ForegroundColor Yellow

$response=read-host "would you like to turn it off?"

if($response -eq "yes"){

write-host "based on your response turning off ssh on"$e.Name -ForegroundColor Green

Stop-VMHostService -HostService $ssh_service

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks Luc.

i am planning to add more conditions in the same htmlreport .

i m going to open new threads.

Reply
0 Kudos