VMware Cloud Community
Munster99
Enthusiast
Enthusiast
Jump to solution

Trying to create a New-VIProperty object - having problems

Hello all

Just a quick one, but hopefully someone might be able to explain why this ISN'T working. I have the following New-VIproperty I would like to create but unfortunately it doesn't seem to be working ??!?!

New-VIProperty -ObjectType Cluster -Name FreeSpaceGB -Value {param ($cluster) ($cluster | get-vmhost | select -first 1 | get-datastore | where-object {$_.multiplehostaccess -eq $true} | measure-object -property FreespaceGB -sum).sum} -force

Things to mention:

- I am running the latest PowerCLI\Powershell versions

- the 'multiplehostaccess' property i am trying to call has already been created in another viproperty which works when i do a (Get-datastore | fl * )

- I have all of my viproperties in a separate file and when 'called' this viproperty is called after i've created all of my datastore viproperties. (if that makes sense)

I'm sure it might have something to do with the $_ variable but I cant seem to understand why its failing ?!??

As per usual any help would be greatly appreciated and Many thanks in advance.

Regards

Munster

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try with the actual multipleHostAccess property in the Where-clause, instead of the New-VIProperty one ?

New-VIProperty -ObjectType Cluster -Name FreeSpaceGB -Value {param ($cluster) ($cluster | get-vmhost | select -first 1 | get-datastore | where-object {$_.ExtensionData.Summary.multiplehostaccess -eq $true} | measure-object -property FreespaceGB -sum).sum} -force


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

View solution in original post

0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

Did you try with the actual multipleHostAccess property in the Where-clause, instead of the New-VIProperty one ?

New-VIProperty -ObjectType Cluster -Name FreeSpaceGB -Value {param ($cluster) ($cluster | get-vmhost | select -first 1 | get-datastore | where-object {$_.ExtensionData.Summary.multiplehostaccess -eq $true} | measure-object -property FreespaceGB -sum).sum} -force


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Yes, just tried. No joy I'm afraid. Also when creating the property I keep getting :

WARNING: The specified script block may contain a reference to the '$_' variable. If you want to reference the object being extended, please use '$args[0]' instead.

I dont know if this might be the problem but I still am not getting any value being returned

.

Smiley Sad

0 Kudos
Brian_Graf
Enthusiast
Enthusiast
Jump to solution

It's working fine for me. PowerCLI 5.5 r2. when you say "it's not working" what do you mean? what errors are you getting? If you are just getting that warning, it's ok...

Senior Product Manager - Distributed Resource Management | @vBrianGraf
0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Sorry Luc/Brian (My Bad) I was connecting to a older environment and the cluster hadn't been configured correctly. Just ran it on our staging clusters and it reports back like you said.  THankyou. But just out of curiosity the cluster I'm testing it on has only 4 hosts in it and my function took 48 seconds to complete. Is this normal ????

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you get the same duration when you run the code as-is, in other words, not in a New-VIProperty construct ?


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

0 Kudos
Brian_Graf
Enthusiast
Enthusiast
Jump to solution

This is probably not normal. I ran it against my 3-node cluster in seconds

Senior Product Manager - Distributed Resource Management | @vBrianGraf
0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

I've created my function as shown below. When I run it, the associated 'viprops' I reference are all situated in another file which gets loaded in as soon as my shell starts. Do you think it could be because of this it is running slow ??? (Come to think of it a lot of my scripts I've designed like this ). Is there a faster way ???? I take it I will have to strip out the values from my 'viproperties' script and repopulate them into the actual function - correct ??!??!?   :smileyconfused:

Function Get-ClusterSummary {

[CmdletBinding()]

PARAM

(

[Parameter(Mandatory=$true,

           ValueFromPipeline=$true,

           ValueFromPipelineByPropertyName=$true,

           HelpMessage = "Please enter a cluster Name",

           Position=0)]

[ValidateNotNullOrEmpty()]

[String[]]$ClusterName

)   

BEGIN {  $OutputObj = @()   }                                  # initialising output object array  

PROCESS {

Try {

    Foreach ($cluster in $ClusterName){

        Write-Verbose "Cluster is $cluster"

        If ($cluster.GetType().Name -eq "string"){

            Try {

                $cluster = Get-Cluster $cluster -ErrorAction Stop

                }

            Catch [Exception]{

                Write-Warning "cluster $cluster does not exist"

                continue

                }

        }

        Elseif ($cluster -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl]){

                Write-Warning "You did not pass a string or a Cluster object"

                continue

        }

            # --- Insert Get code here -------------

            $ClusterVMs = $Cluster | Get-VM

            $props = [ordered]@{'ClusterName'=$cluster.name;

                                'NumOfHosts'=$cluster._NumOfHosts;

                                'NumOfCPUCores'=$cluster._NumCPUCores;

                                'EffectiveMemory_MB'=$cluster._EffectiveMemory;

                                'NumOfVMs'=$cluster._NumVMs;

                                'AllocatedvCPUs'=$cluster._AllocatedvCPUs;

                                'AllocatedMemory_GB'=$cluster._AllocatedMemoryGB;

                                'vCPUpCPURatio_%'=([math]::round($cluster._AllocatedvCPUs / $cluster._NumCPUCores));

                                'ActiveMemory_%'=$cluster._ActiveMemoryPercentage;

                                'FreeSpace_GB'=$cluster._FreeDiskSpaceGB

                                }

            $Obj = New-Object -TypeName PSObject -Property $props                   

            $OutputObj += $Obj           

           

             

            # --- END Get code here -------------

        }

}

Catch [Exception]{

    THROW "Unable to get Cluster details, please confirm the cluster exists."

}   

}

END {   Write-Output $OutputObj   }                              # Outputting OUTPUT objects to pipeline

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Well, a New-VIProperty is by definition somewhat slower than executing the actual code directly.

But I can't imagine that it would make such a huge difference.

I you place the code directly in your function or not is ultimately your choice, but it depends a lot on what you want to use it for.

A collection of New-VIProperty definitions probably has a much higher re-usability score, compared to code snippets in a functions.

So if you are going to use that FreeSpaceGB property in several of your functions/scripts, a New-VIProperty would be better imho (re-usability, easier maintenance)

As always, in PowerShell there are many roads that lead to a solution.

Depending on the intended usage, one method might be better fitted than another.


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Thanks for the advice Luc. My main aim for these types or functions was to help in the general running of my  role, which means creating functions to use primarily as cmdlets. I love the fact that i can pipe from one cmdlet into another but not at the detriment of speed unfortunately.( which is what I'm seeing more and more as our estate gets bigger). I just ran Jonathan Medds old script which had a lot of the same info I required and it took 31 seconds. I think it could also be the fact that my viproperties script is getting larger and has numerous properties that require querying a lot more objects. (Maybe???)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Once you have executed all the New-VIproperty lines, the definitions are kept in memory in the session.

I wonder how much workspace memory increase you see when you do that.


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

I just checked and its funny you should say that because i have 5 powershell sessions open and each one of them loaded my viproperties file. So yes the memory increase is quite substantial when you see it like that. !!! I was thinking maybe just incorporate properties that are simple 'note properties' and anything that required querying the estate as a whole put those into my functions\scripts so they might load quicker. What do you think ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I fully agree with that, code behind New-VIproperty definitions shouldn't be too complex or too elaborate.

Remember that the code needs to be called for each object that is passed to a Select-Object cmdlet, where the New-VIProperty is referenced.

But also note that there is a middle way, you can optimise the execution time of New-VIProperty code by using either of the Extension parameters, and using Get-View and MoRefs to access linked objects, instead of the regular PowerCLI cmdlets.

Such code based on Get-View will practically always be faster compared to a regular PowerCLI cmdlet that does more or less the same thing.

There are some examples in my New-VIProperty module.


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

In all honesty Luc. I've always had problems getting my head around the Get-view Cmdlet. Any resources you could recommend are gratefully received. I'll start looking at the Get-View Cmdlet and delve deeper into Mo-refs. Thankyou very much for your help Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are a couple of good blog posts on the subject:

Performance difference between GET-VM and GET-VIEW
PowerCLI on steroids – Custom attributes

Even Faster PowerCLI Code with Get-View, UpdateViewData() and LinkedViews

And there are quite a number of threads on the subject in this community.

And you can always start a new thread if something is not clear :smileygrin:


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

As usual ...... LIFESAVER !!!

Thanks a million Luc - I've also got your book "VMware vSphere PowerCLI Reference" and found the section on 'Managed Objects ..... and the like. Very interesting read. I'll start going through the blogs you've given me and hopefully get a deeper understanding.

Munster99

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, happy the book is proving to be useful.

If you have a question, don't hesitate to contact me in this community or via my blog.


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

0 Kudos