dhanu2k7
Enthusiast
Enthusiast

modifying array element

Jump to solution

I know its quite complicated, I have written some script to get the OnlineCapacityAnalytics|mem|recommendedSize key from VRA, basically this value gives recomended size but I need to put formulay if the size is above assigned mem then its undersize and if value below memory size then its overide, ( I have overide key in VRA key but that doesn't give me accurate value)

$rec is array element it has VM,key,Average

$Vms is get-VM array

everything is works well but $_.key[0] not able to assigned as string value in array element

    

`powershell

                            

        foreach($VirtualMachine in $vms)

            {

             

               $rec | Where{$_.key -like "*OnlineCapacityAnalytics|mem|recommendedSize*"}| %{if($_.average -le ($VirtualMachine.MemoryMB*1kb)) {{$_.Key[0] = " Overide"}}else{ {$_.Key[0] = "Undersize"}}}

               $subrec +=$Rec| ?{$virtualmachine.name -eq $_.VM -and $_.average -ne 0} | Select VM,@{N="Cluster";e={(Get-Cluster -vm $VirtualMachine).name}},@{N="Site";e={$site}},@{N="PowerState";e={($VirtualMachine).PowerState}},key,Average,time

            }

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I think we established you can not assign a new value to the Key property.

Use Add-Member to add a new property to the object.

And on that new property, you can assign whatever value you want.


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

What exactly is the issue?

What is in $rec? Is that a read-only variable?

What error are you getting?

Can you eventually do a

$rec | Format-Custom -Depth 2


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast

Thank you for the response

the issue i wanted to modify the array element of $rec.key using below if condition ( please see command marked in orrage color)

$rec | Where{$_.key -like "*OnlineCapacityAnalytics|mem|recommendedSize*"}| %{if($_.average -le ($VirtualMachine.MemoryMB*1kb)) {{$_.Key[0] = " Overide"}}else{ {$_.Key[0] = "Undersize"}}}

here is custom depath report for $rec.

PS C:\Users\i> $rec | Format-Custom -Depth 2

class PSCustomObject

{

  VM = xxxxxx

  Average = 3083312.76190476

  Key = summary|oversized|memory

}

class PSCustomObject

{

  VM = xxxx

  Average = 67108864

  Key = mem|guest_provisioned

}

class PSCustomObject

{

  VM = xxxxx

  Average = 16

  Key = config|hardware|num_Cpu

}

class PSCustomObject

{

  VM = xxxxx

  Average = 4971997.86666667

  Key = summary|undersized|memory

}

class PSCustomObject

{

  VM = xxxxx

  Average = 8

  Key = summary|oversized|vcpus

}

class PSCustomObject

{

  VM = xxxxx

  Average = 66341549.4899799

  Key = OnlineCapacityAnalytics|mem|recommendedSize

}

0 Kudos
LucD
Leadership
Leadership

Ok, got it.

Can you check if that key property can be changed (if it is not read-only)?

$rec[0].Key.GetType() | Select *


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Module                : CommonLanguageRuntimeLibrary
Assembly              : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=
TypeHandle            : System.RuntimeTypeHandle
DeclaringMethod       :
BaseType              : System.Object
UnderlyingSystemType  : System.String
FullName              : System.String
AssemblyQualifiedName : System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=
Namespace             : System
GUID                  : 296afbff-1b0b-3ff5-9d6c-
IsEnum                : False

GenericParameterAttributes :

IsSecurityCritical    : False
IsSecuritySafeCritical: False
IsSecurityTransparent : True
IsGenericTypeDefinition: False
IsGenericParameter    : False

GenericParameterPosition   :

IsGenericType         : False

IsConstructedGenericType   : False

ContainsGenericParameters  : False

StructLayoutAttribute : System.Runtime.InteropServices.StructLayoutAttribute
Name                  : String
MemberType            : TypeInfo
DeclaringType         :
ReflectedType         :
MetadataToken         : 33554547
GenericTypeParameters : {}
DeclaredConstructors  : {Void .ctor(Char*), Void .ctor(Char*, Int32, Int32), Void .ctor(SByte*), Void .ctor(SByte*, Int32, Int32)...}
DeclaredEvents        : {}
DeclaredFields        : {m_stringLength, m_firstChar, Empty, TrimHead...}
DeclaredMembers       : {System.String Concat(System.Object), System.String Concat(System.Object, System.Object), System.String Concat(System.Object, System.Object, System.Object), System.String Concat(System.Object,
System.Object, System.Object, System.Object, ...)...}
DeclaredMethods       : {System.String Concat(System.Object), System.String Concat(System.Object, System.Object), System.String Concat(System.Object, System.Object, System.Object), System.String Concat(System.Object,
System.Object, System.Object, System.Object, ...)...}
DeclaredNestedTypes   : {}
DeclaredProperties    : {Int32 Length, Char FirstChar, Char Chars [Int32]}
ImplementedInterfaces : {System.IComparable, System.ICloneable, System.IConvertible, System.Collections.IEnumerable...}
TypeInitializer       :
IsNested              : False
Attributes            : AutoLayout, AnsiClass, Class, Public, Sealed, Serializable, BeforeFieldInit
IsVisible             : True
IsNotPublic           : False
IsPublic              : True
IsNestedPublic        : False
IsNestedPrivate       : False
IsNestedFamily        : False
IsNestedAssembly      : False
IsNestedFamANDAssem   : False
IsNestedFamORAssem    : False
IsAutoLayout          : True
IsLayoutSequential    : False
IsExplicitLayout      : False
IsClass               : True
IsInterface           : False
IsValueType           : False
IsAbstract            : False
IsSealed              : True
IsSpecialName         : False
IsImport              : False
IsSerializable        : True
IsAnsiClass           : True
IsUnicodeClass        : False
IsAutoClass           : False
IsArray               : False
IsByRef               : False
IsPointer             : False
IsPrimitive           : False
IsCOMObject           : False
HasElementType        : False
IsContextful          : False
IsMarshalByRef        : False
GenericTypeArguments  : {}
CustomAttributes      : {[System.SerializableAttribute()], [System.Reflection.DefaultMemberAttribute("Chars")], [System.Runtime.InteropServices.ComVisibleAttribute((Boolean)True)], [__DynamicallyInvokableAttribute()]}

is it issealed =True? 

0 Kudos
LucD
Leadership
Leadership

Yes, looks like you can't update the content of that property.
Which sounds logical in a way, it is a 'key' after all.

The best option, imho, would be to add a new property whit the value you want.


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast

Thanks Lucd,

I am trying to add propery member but i am not getting any idea how to add it because i need to two values one is

the value of key OnlineCapacityAnalytics|mem|recommendedSize is less than memory size 'mem|guest_provisioned' then I need to set $_.key to" Oversize" otherwise it "undersize" below is the script where I am geting $rec from group

can you tell me how to achive solution in my case?

$size is VRA all  values

$Size | group-object -property vm,key

        $rec=$group |where {$_.name -like '*xxxx*' }| Foreach{

                        $avg = $_.group|measure -Property Value -Average|select -expand average

                        add-member -inputobject $_.group[0] -notepropertyname 'Average' -notepropertyvalue $avg -PassThru -Force

                      }|Select vm,Average,key

0 Kudos
LucD
Leadership
Leadership

I think we established you can not assign a new value to the Key property.

Use Add-Member to add a new property to the object.

And on that new property, you can assign whatever value you want.


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

View solution in original post

0 Kudos
dhanu2k7
Enthusiast
Enthusiast

OK thanks Lucd

0 Kudos