VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

whats_wrong_infollowingcode_powercli

HI Luc,

please suggest what is wrong in following.

this is to find max freespace percentage datastore fro a given vm .

$entity=read-host "provide entity"

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

    Write-Host "It's a VM" -ForegroundColor Yellow

    $vm = Get-VM -Name $entity

    $ds = Get-Datastore -RelatedObject $vm

    if($ds.count -gt 1)

    {

    write-host "vm is configured for "$ds.count "datastores"

    $datastores_freepercent=$ds|ForEach-Object -Process {

                                                $per_freespace=$_.freespaceGB/$_.capacityGB*100

  

                                                  $per_freespace_round=[math]::Round($per_freespace)

  

   

   

                                                $datastores=$ds|select name,@{N='freespacepercent';E={$per_freespace_round}}

                                                $datastores

  

}}}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The PS output engine gets confused after the Write-Host lines.

You can force the output to the console by using the Out-Default cmdlet.

Like this

$entity=Read-Host "provide entity"

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

   Write-Host "It's a VM" -ForegroundColor Yellow

   $vm = Get-VM -Name $entity

   $ds = Get-Datastore -RelatedObject $vm

   if($ds.count -gt 1)

   {

   Write-Host "vm is configured for "$ds.count "datastores"

   $datastores_freepercent = $ds|ForEach-Object -Process {

   $per_freespace=$_.freespaceGB/$_.capacityGB*100

   $per_freespace_round=[math]::Round($per_freespace)

   $datastores=$ds|select name,@{N='freespacepercent';E={$per_freespace_round}}

   $datastores | Out-Default

   }

   }

}


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

View solution in original post

26 Replies
LucD
Leadership
Leadership
Jump to solution

The PS output engine gets confused after the Write-Host lines.

You can force the output to the console by using the Out-Default cmdlet.

Like this

$entity=Read-Host "provide entity"

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

   Write-Host "It's a VM" -ForegroundColor Yellow

   $vm = Get-VM -Name $entity

   $ds = Get-Datastore -RelatedObject $vm

   if($ds.count -gt 1)

   {

   Write-Host "vm is configured for "$ds.count "datastores"

   $datastores_freepercent = $ds|ForEach-Object -Process {

   $per_freespace=$_.freespaceGB/$_.capacityGB*100

   $per_freespace_round=[math]::Round($per_freespace)

   $datastores=$ds|select name,@{N='freespacepercent';E={$per_freespace_round}}

   $datastores | Out-Default

   }

   }

}


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

jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks i m checking this .

can yu suggest if there is a way to generate vcsalog bundle usiing powercli .the way it did for esxi and vm.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the Get-Log cmdlet and the Bundle switch.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

is there any switch to exclude esxi hosts and include only the vpxd log in this command?

or any other way .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with

Get-Log -Bundle -VMHost $null -DestinationPath C:\Temp


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

it is still not working :smileyconfused:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What is not working?
The Get-Log?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i am refferingto original code which is not working .

even the following

$entity=Read-Host "provide entity"

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

   Write-host "It's a VM" -ForegroundColor Yellow

   }

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what the problem is, works perfectly for me.

works.jpg


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

:smileycry: many a timesi think that some sort of parrellel operation being performed by somone as i see some very strange results.

i dont know who is doing it and for what benefit .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So what are you getting if you do the same?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

No output .only command prompt.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you share a screenshot (similar to the one I did a minute ago)?
Perhaps stop/start your PS session first (to have a clean environment).


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

well i checked with other vms it is working .

for the desired vm(which i can search using get-vm ) gives blank like below.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is there perhaps a funny character (blank?) in the displayname of the VM?
What is the length of the name?
Does that correspond with the number of characters you see?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

for some reasons now it is working .stillnot sure what happened last time as there was no blank and the lenght corresponds to number of charaecters.

however it is working now.

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

if you can check following code one more time and suggest something for orange line .

i have been trying to sort using freespacepercent  note property .is it possible as its a custom property ?

$entity=Read-Host "provide entity"

$fragments = @()

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

   Write-host "It's a VM" -ForegroundColor Yellow

   }

 

   $vm = Get-VM -Name $entity

   $ds = Get-Datastore -RelatedObject $vm

   if($ds.count -gt 1)

   {

   Write-Host "vm is configured for "$ds.count "datastores"

   foreach($d in $ds)

   {

   $d_name=get-datastore -Name $d

   $per_freespace_datastore=$d_name.freespaceGB/$d_name.capacityGB*100

   $per_freespace_round=[math]::Round($per_freespace_datastore)

   $datastores=$d_name|select name,@{N='freespacepercent';E={$per_freespace_round}}

  $datastores|Sort-Object -Property 'freespacepercent' -Descending

   $fragments += $d_name.Name

   $fragments += $per_freespace_round 

   }}

  

   $fragments|out-file -FilePath 'C:\ttttttt.txt'

   notepad ttttttt.txt

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, in $datastores you have entries with 2 properties, Name and freespacepercent.

And Sort-Object can use any property to do a sort.

But you are not assigning the result to anything, so it will just be displayed on the console.


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

jvm2016
Hot Shot
Hot Shot
Jump to solution

Tx for your response Luc.However it is not sorting as desired (descending order)

also  somehow i want to use following working which we discussed seaparetly also for getting logs.

iam stuck where vm is configured for multiple datastores .what should i use for orange line in below code??

$targetpath="c:\users\folder1"

$entity=Read-Host "provide entity"

$fragments = @()

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

   Write-host "It's a VM" -ForegroundColor Yellow

   }

 

   $vm = Get-VM -Name $entity

   $ds = Get-Datastore -RelatedObject $vm

   if($ds.count -gt 1)

   {

   Write-Host "vm is configured for "$ds.count "datastores"

   foreach($d in $ds)

   {

   $d_name=get-datastore -Name $d

   $per_freespace_datastore=$d_name.freespaceGB/$d_name.capacityGB*100

   $per_freespace_round=[math]::Round($per_freespace_datastore)

   $datastores=$d_name|select name,@{N='freespacepercent';E={$per_freespace_round}}

   $datastores|Sort-Object -Property 'freespacepercent' -Descending

   $fragments += $d_name.Name

   $fragments += $per_freespace_round 

   }}

  

   $fragments|out-file -FilePath 'C:\Users\folder1\ttttttt.txt'

   notepad ttttttt.txt

   $Ds_desired=read-host "please provide ds name "

     New-PSDrive -Location $ds_desired -Name DS -PSProvider VimDatastore -Root '\' | Out-Null

    #Get-ChildItem -Path DS:\

    Set-Location DS:\

    cd $vm

    #Get-ChildItem vmware.log

    Copy-DatastoreItem -Item vmware.log -Destination $targetpath

    Remove-PSDrive -Name DS -Confirm:$false

0 Kudos