VMware Cloud Community
vespavbb
Enthusiast
Enthusiast
Jump to solution

Get-vm with certian custom Attributes

Hi

I want to create an arry or csv of vm´s with certian attributes.

The script basically works, but the output is strange, i want to cut the output to the value only

$vms = Get-cluster C01-Test* | get-vm

$myCol=@()

foreach($vm in $vms)

{

$col =  "" | select Name, Att1,Att2, Att3

$col.name = $vm.name

$col.Att1 = $vm | select {$_.CustomFields.Item("test-APP")}

$col.Att2 = $vm | select {$_.CustomFields.Item("test-System")}

$col.Att3 = $vm | select {$_.CustomFields.Item("test-Produkt")}

$myCol += $col

}

$mycol

VCP4,VCP5,VCP6,VCP7,VCP8
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is due to the Select you use.
Try like this

$vms = Get-Cluster C01-Test* | Get-VM


$myCol = @()


foreach ($vm in $vms) {

    $col = "" | Select-Object Name, Att1, Att2, Att3

    $col.name = $vm.name

    $col.Att1 = $vm.CustomFields.Item("test-APP")

    $col.Att2 = $vm.CustomFields.Item("test-System")

    $col.Att3 = $vm.CustomFields.Item("test-Produkt")

    $myCol += $col

}


$mycol


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

View solution in original post

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

That is due to the Select you use.
Try like this

$vms = Get-Cluster C01-Test* | Get-VM


$myCol = @()


foreach ($vm in $vms) {

    $col = "" | Select-Object Name, Att1, Att2, Att3

    $col.name = $vm.name

    $col.Att1 = $vm.CustomFields.Item("test-APP")

    $col.Att2 = $vm.CustomFields.Item("test-System")

    $col.Att3 = $vm.CustomFields.Item("test-Produkt")

    $myCol += $col

}


$mycol


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

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

cool thanks

i have a secound question and secound script...

the oupt is fine, I get all VMs with all Attributes. but how can i modify the $valueList to select only certian Attributes, mostly like the script before. just different

$valueList =@()

Get-cluster C01-Test | get-vm  | % {

for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){

$row = “” | Select vmname, FieldKey, FieldValue

$row.VMname = $_.Name

$row.FieldKey = $_.CustomFields.Keys[$i]

$row.FieldValue = $_.CustomFields.Values[$i]

$valueList += $row

}

}

$valueList

### maybe it works if i can do a select from the $valueList and select only vmname and some fieldkeys I want to

$valueList  | select-object vmname,  Fieldkey where value like ""TEST-Value"...

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could use a Where-clause.
Something like this

$valueList  |

where{$_.FieldKey -eq 'Your-CA'} |

select-object -Property *

You can also select multiple keys by adapting the Where-clause

$valueList  |

where{'CA1','CA2','CA3' -contains $_.FieldKey} |

select-object -Property *

And you also use the Where-clause on the Value instead of the Key

$valueList  |

where{$_.FieldValue -eq 'Test-Value'} |

select-object -Property *


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

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

ah ok thanks, thats nice

but I think the first script is easier

now lets get deeper.

If I modify the csv file ( only the FieldValue) and I want to import the csv file back to overwrite the values of all vm´s

how could I do it?

a foreach loop with set-annotation ??

$vms = Get-Cluster C01-Test* | Get-VM

$myCol = @()

foreach ($vm in $vms) {

    $col = "" | Select-Object Name, Att1, Att2, Att3

    $col.name = $vm.name

    $col.Att1 = $vm.CustomFields.Item("test-APP")

    $col.Att2 = $vm.CustomFields.Item("test-System")

    $col.Att3 = $vm.CustomFields.Item("test-Produkt")

    $myCol += $col

}

$mycol | Export-Csv “C:\00-Scripts\vms-attributes-value.csv” -NoTypeInformation

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this.
Wherever a Custom Attribute value in the CSV is different from the actual CA value on the VM, the script will update the value.

Import-Csv -Path .\your.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $vm = Get-VM -Name $row.Name

    $row | Get-Member -MemberType NoteProperty |

    where{$_.Name -ne 'Name'} |

    ForEach-Object -Process {

        $ca = Get-Annotation -Entity $vm -CustomAttribute $_.Name

        if($ca.Value -ne $row."$($_.Name)"){

            Set-Annotation -Entity $vm -CustomAttribute $ca.Name -Value $row."$($_.Name)" -Confirm:$false

        }

    }

}


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

vespavbb
Enthusiast
Enthusiast
Jump to solution

thanks Luc,

I will try

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

an other question, here is an example to export all attributes of all vms´. the output looks different, but ok.

How could i import this list and set it on all vm´s. Like a backup and restore thing...

$valueList =@()

Get-cluster  | get-vm  | % {

for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){

$row = “” | Select Name, FieldKey, FieldValue

$row.name = $_.Name

$row.FieldKey = $_.CustomFields.Keys[$i]

$row.FieldValue = $_.CustomFields.Values[$i]

$valueList += $row

}

}

$valueList | Export-Csv “C:\00-Scripts\exported-attributes-value.csv” -NoTypeInformation

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

The try-catch makes sure that the Custom Attribute is created, shouldn't it exist yet.

Import-Csv -Path 'C:\00-Scripts\exported-attributes-value.csv' -UseCulture |

ForEach-Object -Process {

    try{

        $ca = Get-CustomAttribute -Name $row.FieldKey -TargetType VirtualMachine -ErrorAction Stop

    }

    catch{

        $ca = New-CustomAttribute -Name $row.FieldKey -TargetType VirtualMachine -Confirm:$false

    }

    Get-Vm -Name $row.Name | Set-Annotation -CustomAttribute $ca -Value $row.FieldValue -Confirm:$false

}


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

vespavbb
Enthusiast
Enthusiast
Jump to solution

 

Hi Luc,

this Script is great, but I have a new challenge, 

If a Attribute Value is empty this script will write an empty value to the Attribute, but I want to make sure, if it is empty , skip the SET

 

 

Import-Csv -Path .\your.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {

    $vm = Get-VM -Name $row.Name

    $row | Get-Member -MemberType NoteProperty |

    where{$_.Name -ne 'Name'} |

    ForEach-Object -Process {

        $ca = Get-Annotation -Entity $vm -CustomAttribute $_.Name

        if($ca.Value -ne $row."$($_.Name)"){

            Set-Annotation -Entity $vm -CustomAttribute $ca.Name -Value $row."$($_.Name)" -Confirm:$false

        }

    }

}

 

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can add this to the IF

if ($row."$($_.Name)" -ne '' -and $ca.Value -ne $row."$($_.Name)") {


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

vespavbb
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

is it better to create a new topic or just ask on an existing?

here is my question about attribute

I want to check the attributes of some vm´s, and if some item´s are empty, the array should output "empty" for this item.

but for this example with att3 the Syntax is not working.

 

$vms = Get-Cluster my-Test* | Get-VM

$myCol = @()

foreach ($vm in $vms) {

    $col = "" | Select-Object Name, Att1, Att2, Att3

    $col.name = $vm.name

    $col.Att1 = $vm.CustomFields.Item("test-APP")

    $col.Att2 = $vm.CustomFields.Item("test-System")

    $col.Att3 = if ($vm.CustomFields.Item("test-Produkt") -like $null) {$col.Att3  = "missing"} else {$col.Att3 = $vm.CustomFields.Item("test-Produkt")}
   
    $myCol += $col

}

$mycol 
VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Comparing with $null is mostly not a good idea.
Try the condition like this

-not ($vm.CustomFields.Item("test-Produkt"))

That way $null, but also an empty String, will succeed 


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

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

ok,  but I dont understand why this is not working. asuming the value  is empty it should, or?. If I check the "if" it says True

 $col.Att3 = if (!$vm.CustomFields.Item("test-Produkt")) {$col.Att3  = "missing"} 

 

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What actually is in there?

$vm.CustomFields.Item("test-Produkt") | Get-Member


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

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), int IComparable[string].CompareTo(string other)
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith(string value, bool ignoreCase, cultureinfo culture)
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison comparisonType), bool IEquatable[string].Equals(string other)
GetEnumerator Method System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(), System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex, int count), int IndexOf(string value), int IndexOf(string value, int s...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int startIndex, int count)
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int startIndex, int count), int LastIndexOf(string value), int LastIndexOf...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] anyOf, int startIndex, int count)
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[]...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool StartsWith(string value, bool ignoreCase, cultureinfo culture)
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower Method string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider)
ToString Method string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider provider)
ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property int Length {get;}

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This value is a string, when there is no value it is an empty string.
An empty string is not the same as $null.
Also, use the -eq operator, not the -like operator


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

Reply
0 Kudos
vespavbb
Enthusiast
Enthusiast
Jump to solution

still having problems here...

 

##work

$var1

if (-not $var1) {$var1 = "mytext"}

$var1


##doesn´t work

 if ( -not $vm.CustomFields.item("Size") ) { write-host "empty" $var1 = "mycomplextext"}

here the if jumps in and write´s 'empty' but the $var1 will not be created..? I´m struggling

VCP4,VCP5,VCP6,VCP7,VCP8
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is there CR missing in 

write-host "empty" $var1 = "mycomplextext"




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

Reply
0 Kudos