Hi Luc,
is there any way of doing something like below in ornge line .i have created clusters as parameter but i need to pass list of clusters from csv file .
what needs to be changed in below .
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string[]]$clusters=Import-Csv "C:\users\clusters.csv"
)
Yes, [string] is the correct type in this case.
And yes, the function will prompt when FilePath parameter is missing (due to it being declared as required).
I would just place the path to the CSV between quotes.
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv = Import-Csv -Path "$filepath\clustersname.csv"
foreach($line in $csv)
{
Get-Cluster $line.cluster | Get-VMHost |
ForEach-Object -Process {
$per_mem=$_.MemoryUsageGB/$_.MemoryTotalGB*100
$per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100
$per_mem_round=[math]::Round($per_mem)
$per_cpu_round=[math]::Round($per_cpu)
$_ | Select @{N='VMHost';E={$_.name}},
@{N='connection';E={$_.connectionstate}},
@{N='memutilpercent';E={$per_mem_round}},
@{N='cpuutilpercent';E={$per_cpu_round}}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Afaik that is not possible, but you could use the $PSDefaultParameterValues, before you call the function the 1st time.
Something like this
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string[]]$clusters
)
$clusters
}
$PSDefaultParameterValues = @{
"Get-Supportedesxi:clusters" = $(Import-csv -Path .\default.csv -UseCulture)
}
Get-Supportedesxi
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks iam checking this.
i removed param block from function .my requirement was to have this in the form of command.
however i was not able to fit in below code what you suggested in last post.
function Get-Supportedesxi
{
$csv=Import-Csv "C:\users\cluster.csv"
foreach($cl in $csv)
{
$cluster=get-cluster $cl.cluster
Get-vmhost -Location $cluster|ForEach-Object -Process {
$per_mem=$_.MemoryUsageGB/$_.MemoryTotalGB*100
$per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100
$per_mem_round=[math]::Round($per_mem)
$per_cpu_round=[math]::Round($per_cpu)
$_ |
Select @{N='VMHost';E={$_.name}},@{N='connection';E={$_.connectionstate}},@{N='memutilpercent';E={$per_mem_round}},@{N='cpuutilpercent';E={$per_cpu_round}}
}
}}
Not sure what you mean by "...my requirement was to have this in the form of command"
But a more "PowerShell" way of doing this would be something like this
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$Cluster
)
Get-Cluster $Cluster | Get-VMHost |
ForEach-Object -Process {
$per_mem=$_.MemoryUsageGB/$_.MemoryTotalGB*100
$per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100
$per_mem_round=[math]::Round($per_mem)
$per_cpu_round=[math]::Round($per_cpu)
$_ | Select @{N='VMHost';E={$_.name}},
@{N='connection';E={$_.connectionstate}},
@{N='memutilpercent';E={$per_mem_round}},
@{N='cpuutilpercent';E={$per_cpu_round}}}
}
Import-Csv "C:\users\cluster.csv" | Get-Supportedesxi
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc iam checking this .
what i meant when i run my previous code without param block it worked .and i see that function displayed in powershell.
cant we define function without param block??
also if yu could tel me how is yur code color coded here.
Yes, you can definitely have a function without a parameter block.
There are a couple of ways for adding color to your code.
I described one in Some ways to enter PowerCLI code under the new forum SW
When you use VSC, you can just copy/paste from the editor window.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
for some reasons it is not working .
however i tried to change like below it works .could you check below and suggest if there is any problem in below code.
also i am declaring file path as parameter. is string the correct type ?
what i have been trying is to run Get-Supportedesxi only .if there is parameter defined it will ask .
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv=Import-Csv $filepath\clustersname.csv
foreach($line in $csv)
{
Get-Cluster $line.cluster | Get-VMHost |
ForEach-Object -Process {
$per_mem=$_.MemoryUsageGB/$_.MemoryTotalGB*100
$per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100
$per_mem_round=[math]::Round($per_mem)
$per_cpu_round=[math]::Round($per_cpu)
$_ | Select @{N='VMHost';E={$_.name}},
@{N='connection';E={$_.connectionstate}},
@{N='memutilpercent';E={$per_mem_round}},
@{N='cpuutilpercent';E={$per_cpu_round}}}
}}
Yes, [string] is the correct type in this case.
And yes, the function will prompt when FilePath parameter is missing (due to it being declared as required).
I would just place the path to the CSV between quotes.
function Get-Supportedesxi
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv = Import-Csv -Path "$filepath\clustersname.csv"
foreach($line in $csv)
{
Get-Cluster $line.cluster | Get-VMHost |
ForEach-Object -Process {
$per_mem=$_.MemoryUsageGB/$_.MemoryTotalGB*100
$per_cpu=$_.Cpuusagemhz/$_.CpuTotalMhz*100
$per_mem_round=[math]::Round($per_mem)
$per_cpu_round=[math]::Round($per_cpu)
$_ | Select @{N='VMHost';E={$_.name}},
@{N='connection';E={$_.connectionstate}},
@{N='memutilpercent';E={$per_mem_round}},
@{N='cpuutilpercent';E={$per_cpu_round}}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
I would just place the path to the CSV between quotes.
could you tell me what difference it makes here putting " ".
my understanding is to put when there is space or hypen inside name .
also i have a similar function for finding datastores wherin the out put is partially dispalyed .(though it works fine sometimes)
how is it possible if code works fine one time does not work properly the othertime.(and no changes to powershell view has been done)
The reason is that variables substitution happens in strings. Otherwise PS tries to interpret the string as a cmdlet or function.
On the other question, that is hard to say without actually seeing the code :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks .
for the other question code is below.dont know why sometimes datastores names are partially displayed.
function Get-Supporteddatastoresreg1_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string[]]$clusters
)
foreach($clu in $clusters)
{
$cluster=get-cluster $clu
$ds=Get-Datastore -relatedobject $cluster
foreach($d in $ds)
{
$d_per=$d.freespaceGB/$d.capacityGB*100
$d_round=[math]::Round($d_per)
#.extensiondata.configissue
$d|select name,@{N='freespacepercent';E={$d_round}},@{N='whetheravailable';E={$_.State}}
}
}
}
Seems to work for me each time I run it.
Could it be that your datastorenames are so long that it doesn't fit on screen?
Can you show an example of run with missing info?
function Get-Supporteddatastoresreg1_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string[]]$clusters
)
foreach($clu in $clusters)
{
$cluster=Get-Cluster $clu
$ds=Get-Datastore -RelatedObject $cluster
foreach($d in $ds)
{
$d_per=$d.freespaceGB/$d.capacityGB*100
$d_round=[math]::Round($d_per)
$d|select name,@{N='freespacepercent';E={$d_round}},@{N='whetheravailable';E={$_.State}}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well it worked for me sometimes and sometimes not .
i dont hve screenshot and i dontthink any properties change at powershell console has be done .
however it worked manytimesirresepective of the long lenght of datastores names.
Next time it doesn't work, attach a screenshot.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sure.thnks