I have a script with a Get-VMHost command that is bringing all the information I need, but ...
1 - I need to come to the table below ' ; ' as opposed to space;
2 - If there is more than one space is not necessarily see two ' ; ' But only one;
3 - I want to continue with the return carrier at the end of each line;
4 - I want to convert the values to numbers with only two decimal places
comand in script:
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) { Add-PSSnapin VMware.VimAutomation.Core }
$Name= @{ Label="";
Expression={$_.Name};
Alignment="Left"
Width=29}
$NumCpu= @{Label="";
Expression={$_.NumCpu};
Alignment="Left"
Width=2}
$CpuUsageMhz= @{Label="";
Expression={$_.CpuUsageMhz};
Alignment="Left"
Width=5}
$CpuTotalMhz= @{Label="";
Expression={$_.CpuUsageMhz};
Alignment="Left"
Width=5}
$MemoryUsageGB= @{Label="";
Expression={$_.MemoryUsageGB};
Alignment="Left"
Width=5}
$MemoryTotalGB= @{Label="";
Expression={$_.MemoryTotalGB};
Alignment="Left"
Width=5}
clear
Connect-VIServer 10.8.3.87 -User Tolkien -Password 123456
get-VMHost -Location HP_Lab -State Connected| Format-Table -AutoSize -HideTableHeaders $Name, $NumCpu, $CpuUsageMhz, $CpuTotalMhz, $MemoryUsageGB, $MemoryTotalGB | Out-String -Width 100
Result:
dhp00905.df.intrabb.bb.com.br 32 14875 14875 114,048828125 255,989841461181640625
lhp00901.df.intrabb.bb.com.br 32 17759 17759 133,103515625 255,989841461181640625
lhp00902.df.intrabb.bb.com.br 32 14892 14892 209,2626953125 255,989841461181640625
hhp00906.df.intrabb.bb.com.br 32 12599 12599 138,599609375 255,989841461181640625
hhp00908.df.intrabb.bb.com.br 32 15748 15748 134,26171875 255,989841461181640625
hhp00907.df.intrabb.bb.com.br 32 16946 16946 142,251953125 255,989841461181640625
Desired result
dhp00905.df.intrabb.bb.com.br;32;14875;14875;114,04;255,98
lhp00901.df.intrabb.bb.com.br;32;17759;17759;133,10;255,98
lhp00902.df.intrabb.bb.com.br;32;14892;14892;209,26;255,98
hhp00906.df.intrabb.bb.com.br;32;12599;12599;138,59;255,98
hhp00908.df.intrabb.bb.com.br;32;15748;15748;134,26;255,98
hhp00907.df.intrabb.bb.com.br;32;16946;16946;142,25;255,98
That's because you are sending hash tables for values.
You should change these
$Name = $_.Name
....
Any alignment you want to do, can be done in the format strings.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try using the Format operator, like this
Get-VMHost -Location HP_Lab -State Connected| %{
"{0};{1};{2};{3};{4:n2};{5:n2}" -f $Name,$NumCpu,$CpuUsageMhz,$CpuTotalMhz,$MemoryUsageGB,$MemoryTotalGB
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) { Add-PSSnapin VMware.VimAutomation.Core }
$Name= @{ Label="";
Expression={$_.Name};
Alignment="Left"
.
.
.
Connect-VIServer 10.8.3.87 -User df\servpdides -Password pdidev123 >>null
Get-VMHost -Location HP_Lab -State Connected| %{
"{0};{1};{2};{3};{4:n2};{5:n2}" -f $Name,$NumCpu,$CpuUsageMhz,$CpuTotalMhz,$MemoryUsageGB,$MemoryTotalGB
} | Out-String
Result:
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;System.Collections.Hashtable;Sys
tem.Collections.Hashtable;System.Collections.Hashtable
That's because you are sending hash tables for values.
You should change these
$Name = $_.Name
....
Any alignment you want to do, can be done in the format strings.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect! Tanks a lot!
dhp00905;32;16526;72448;108,22;255,99
lhp00901;32;15516;72448;140,22;255,99
lhp00902;32;16253;72448;200,05;255,99
hhp00906;32;25404;72448;87,07;255,99
hhp00908;32;14790;72448;148,36;255,99
hhp00907;32;17342;72448;174,47;255,99