Automation

 View Only
Expand all | Collapse all

Get-vm with certian custom Attributes

  • 1.  Get-vm with certian custom Attributes

    Posted Mar 27, 2020 04:10 PM

    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



  • 2.  RE: Get-vm with certian custom Attributes
    Best Answer

    Posted Mar 27, 2020 04:26 PM

    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



  • 3.  RE: Get-vm with certian custom Attributes

    Posted Mar 27, 2020 04:46 PM

    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"...



  • 4.  RE: Get-vm with certian custom Attributes

    Posted Mar 27, 2020 05:07 PM

    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 *



  • 5.  RE: Get-vm with certian custom Attributes

    Posted Mar 27, 2020 05:21 PM

    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



  • 6.  RE: Get-vm with certian custom Attributes

    Posted Mar 27, 2020 06:18 PM

    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

            }

        }

    }



  • 7.  RE: Get-vm with certian custom Attributes

    Posted Mar 27, 2020 07:30 PM

    thanks Luc,

    I will try



  • 8.  RE: Get-vm with certian custom Attributes

    Posted Apr 30, 2020 08:29 AM

    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



  • 9.  RE: Get-vm with certian custom Attributes

    Posted Apr 30, 2020 09:31 AM

    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

    }



  • 10.  RE: Get-vm with certian custom Attributes

    Posted Apr 22, 2021 08:04 AM

     

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

     



  • 11.  RE: Get-vm with certian custom Attributes

    Posted Apr 22, 2021 08:19 AM

    You can add this to the IF

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


  • 12.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 11:02 AM

    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 


  • 13.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 11:05 AM

    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 



  • 14.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 12:47 PM

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

     



  • 15.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 12:59 PM

    What actually is in there?

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


  • 16.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 01:31 PM

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



  • 17.  RE: Get-vm with certian custom Attributes

    Posted Jan 14, 2022 04:41 PM

    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



  • 18.  RE: Get-vm with certian custom Attributes

    Posted Jan 17, 2022 03:34 PM

    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



  • 19.  RE: Get-vm with certian custom Attributes

    Posted Jan 17, 2022 03:44 PM

    Is there CR missing in 

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