VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

param_block_powercli

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"

       

       

       

    )

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks iam checking this.

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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}}

}

}}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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}}}

            }}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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}}

}

}

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Next time it doesn't work, attach a screenshot.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

sure.thnks

Reply
0 Kudos