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
}}}
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
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
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
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
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.
With the Get-Log cmdlet and the Bundle switch.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
is there any switch to exclude esxi hosts and include only the vpxd log in this command?
or any other way .
Try with
Get-Log -Bundle -VMHost $null -DestinationPath C:\Temp
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it is still not working :smileyconfused:
What is not working?
The Get-Log?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
}
Not sure what the problem is, works perfectly for me.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
: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 .
So what are you getting if you do the same?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No output .only command prompt.
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
well i checked with other vms it is working .
for the desired vm(which i can search using get-vm ) gives blank like below.
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
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.
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
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
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