Automation

 View Only
  • 1.  VLAN reference table with Array

    Posted Jul 05, 2022 01:59 PM

    Hi LucD

    Please help me with the following script

    Currently you helped me to formulate this array with two fields and it works fine

    ###############################################################

    Reference Table

    Source_VLANDestination_VLAN
    X1-VLAN-110-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-110
    X1-VLAN-111-ZZZK-VMNabc01-w03-cl01--pg-vlan-111
    X1-VLAN-111-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-111
    X1-VLAN-112-ZZZK-VMNabc01-w03-cl01--pg-vlan-112
    X1-VLAN-112-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-112
    X1-VLAN-114-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-114
    X1-VLAN-114-ZZZK-VMNabc01-w03-cl01--pg-vlan-114
    X1-VLAN-115-ZZZK-VMNabc01-w03-cl01--pg-vlan-115
    X1-VLAN-115-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-115
    X1-VLAN-116-ZZZZZZ-VMNabc01-w03-cl01--pg-vlan-116

    Try {

    $tabvlan =$null
    $tabvlan = @{}
    Import-Csv -Path "$workingdir\vMotion_VLAN_Reference_Table\vLan_Reference.csv" -Delimiter ";" -PipelineVariable row |
    ForEach-Object -Process {
    if(-not $tabvlan.($row.SrcDvPG)) {$tabvlan.Add($row.SrcDvPG,$row.DstDvPG)}


    }

    }

    Catch
    {
    Write-Warning ""

    Write-Host "Error Ran into issues: $($PSItem.ToString()) " -ForegroundColor Red
    ""
    Write-Host "Error !!! Check above error for details" -ForegroundColor DarkYellow
    }

    ########################################################################

    However I now need reference four columns

    Reference Table

    Source_cluuster_NameSource_VLANDestination_cluuster_NameDestination_VLAN
    cluuster005_BAK_TestVLAN-752-BB-VMN-Testabc01-w03-clu02abc01-w03-clu02--pg-vlan-752
    cluuster005_BAK_TestVLAN-753-BB-VMN-Testabc01-w03-clu02abc01-w03-clu02--pg-vlan-753
    cluuster049_cluU_P2_WNNP2-VLAN-258-WNNabc01-w04-clu01

    abc01-w04-clu01--pg-vlan-258

     

     

    Please help me with the code to reference four columns

    I am using the following to create a template file

    #####################################
    ## Creating VM Template File ###
    #####################################

     

    Try {

    Get-cluster $selectedCLuster.name -ErrorAction Stop |Get-VMHost $TemplateDetails.vmHost -ErrorAction Stop |get-vm -ErrorAction Stop|`

    #Get-cluster $selectedCLuster.name |Get-VMHost -Name $TemplateDetails.vmHost |get-vm |`

    Select-Object @{N= 'Comment' ; E={ '#'}},

    @{N= 'VMName' ; E={$_.name }},

    @{N= 'NW_Adapter1' ; E={(Get-NetworkAdapter -VM $_)[0].Name }},

    @{N= 'Src_NW1' ; E={(get-networkadapter -VM $_)[0].NetworkName}},

    @{N= 'SDI_NW1' ; E={ $tabvlan[(Get-NetworkAdapter -VM $_)[0].NetworkName]}},

    @{N= 'NW_Adapter2' ; E={(Get-NetworkAdapter -VM $_)[1].Name }},

    @{N= 'Src_NW2' ; E={(get-networkadapter -VM $_)[1].NetworkName}},

    @{N= 'SDI_NW2' ; E={ $tabvlan[(Get-NetworkAdapter -VM $_)[1].NetworkName]}},


    @{N= 'NW_Adapter3' ; E={(Get-NetworkAdapter -VM $_)[2].Name }},

    @{N= 'Src_NW3' ; E={(get-networkadapter -VM $_)[2].NetworkName}},

    @{N= 'SDI_NW3' ; E= { $tabvlan[(Get-NetworkAdapter -VM $_)[2].NetworkName]}},

    @{N= 'NW_Adapter4' ; E={(Get-NetworkAdapter -VM $_)[3].Name }},

    @{N= 'Src_NW4' ; E={(get-networkadapter -VM $_)[3].NetworkName}} |`

     

    Export-csv -Path "$($FilesAndLogDir)\$vMotion_Template_Name" -NoTypeInformation -UseCulture -ErrorAction Stop


    Catch

    {
    ""
    Write-Warning ""
    Write-Host "Ran into an issue: $($PSItem.ToString())" -ForegroundColor Red

    }



  • 2.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 02:10 PM

    Not sure I see the question.
    Help you with what?



  • 3.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 02:22 PM

    Hi LucD

    I need to reference the following table

    Reference Table

    Source_cluuster_NameSource_VLANDestination_cluuster_NameDestination_VLAN
    cluuster005_BAK_TestVLAN-752-BB-VMN-Testabc01-w03-clu02abc01-w03-clu02--pg-vlan-752
    cluuster005_BAK_TestVLAN-753-BB-VMN-Testabc01-w03-clu02abc01-w03-clu02--pg-vlan-753
    cluuster049_cluU_P2_WNNP2-VLAN-258-WNNabc01-w04-clu01

    abc01-w04-clu01--pg-vlan-258

     

     



  • 4.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 02:23 PM

    Hi LucD

    The code below only works to reference two columns but i need to reference four columns

    ry {

    $tabvlan =$null
    $tabvlan = @{}
    Import-Csv -Path "$workingdir\vMotion_VLAN_Reference_Table\vLan_Reference.csv" -Delimiter ";" -PipelineVariable row |
    ForEach-Object -Process {
    if(-not $tabvlan.($row.SrcDvPG)) {$tabvlan.Add($row.SrcDvPG,$row.DstDvPG)}


    }

    }

    Catch
    {
    Write-Warning ""

    Write-Host "Error Ran into issues: $($PSItem.ToString()) " -ForegroundColor Red
    ""
    Write-Host "Error !!! Check above error for details" -ForegroundColor DarkYellow
    }



  • 5.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 03:40 PM

    I got that, but what do you need to do?
    Just add this to the same hash table?
    And how are you going to use that hash table?
    What are you looking up (of those 4 columns)?



  • 6.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 03:57 PM

    Hi LucD

    I am doing VM migrations across Clusters so I have the source cluster and Source portgroup, which i need to match with Destination cluster and destination portgroup

    Once its matched then i used the following script to create a Template file  and I use that template file for migrations

    Get-cluster $selectedCLuster.name -ErrorAction Stop |Get-VMHost $TemplateDetails.vmHost -ErrorAction Stop |get-vm -ErrorAction Stop|`

    #Get-cluster $selectedCLuster.name |Get-VMHost -Name $TemplateDetails.vmHost |get-vm |`

    Select-Object @{N= 'Comment' ; E={ '#'}},

    @{N= 'VMName' ; E={$_.name }},

    @{N= 'NW_Adapter1' ; E={(Get-NetworkAdapter -VM $_)[0].Name }},

    @{N= 'Src_NW1' ; E={(get-networkadapter -VM $_)[0].NetworkName}},

    @{N= 'SDI_NW1' ; E={ $tabvlan[(Get-NetworkAdapter -VM $_)[0].NetworkName]}},

    @{N= 'NW_Adapter2' ; E={(Get-NetworkAdapter -VM $_)[1].Name }},

    @{N= 'Src_NW2' ; E={(get-networkadapter -VM $_)[1].NetworkName}},

    @{N= 'SDI_NW2' ; E={ $tabvlan[(Get-NetworkAdapter -VM $_)[1].NetworkName]}},


    @{N= 'NW_Adapter3' ; E={(Get-NetworkAdapter -VM $_)[2].Name }},

    @{N= 'Src_NW3' ; E={(get-networkadapter -VM $_)[2].NetworkName}},

    @{N= 'SDI_NW3' ; E= { $tabvlan[(Get-NetworkAdapter -VM $_)[2].NetworkName]}},

    @{N= 'NW_Adapter4' ; E={(Get-NetworkAdapter -VM $_)[3].Name }},

    @{N= 'Src_NW4' ; E={(get-networkadapter -VM $_)[3].NetworkName}} |`

     

    Export-csv -Path "$($FilesAndLogDir)\$vMotion_Template_Name" -NoTypeInformation -UseCulture -ErrorAction Stop

     

    Many thanks

    RXJ



  • 7.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 04:12 PM

    Ok, if I understand it correctly you also need to find the destination cluster based on the current cluster (same concept as for the portgroup).
    Since a hash table contains a Key and a Value, it is impossible to look up something based on 2 different types of keys.
    In your case I would just create a 2nd hash table with the source and destination cluster information from the CSV.
    Then you can look up the destination cluster in the same way as you now do for the portgroup.



  • 8.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 04:21 PM

    Hi LucD

    No I need to match the entries of Source PortGroup and Destination Cluster to get the correct Destination Portgroup

    Thats is Source_Portgroup , Destination_Cluster ==> Destination_PortGroup

    So what i need to know is how can use two keys and find the value

    $tabvlan =$null
    $tabvlan = @{}
    Import-Csv -Path "$workingdir\vMotion_VLAN_Reference_Table\vLan_Reference.csv" -Delimiter ";" -PipelineVariable row |
    ForEach-Object -Process {

    if(-not $tabvlan.($row.Source_Portgroup)) {$tabvlan.Add($row.Source_Portgroup,$row.Source_Cluster , $row.Destination_Portgroup)} 

    Hope i have made it clear

     

    Many thanks 

    RXJ



  • 9.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 04:26 PM

    Then you will have to append the Source Portgroup and Destination Cluster as a unique key.
    But how do you know the Destination Cluster?

    In your code, for a VM, you can find the Source Portgroup and the Source Cluster.
    Which logic do you use to find the Destination Cluster?



  • 10.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 04:37 PM

    Hi LucD

    The destination Cluster and Destination VLAN are mapped from the CSV file 

    So basically I need to use the CSV file and determine the Destination VLAN ( to determine the destination VLAN i am using the Source_Portgroup and the destination Cluster ) 

    This worked perfectly when i had SOurce_Portgroup and Destination_Portgroup

    Now i need to take Destination Cluster into consideration and thats where I have no idea how to put this across

     

    Many thanks

    RXJ



  • 11.  RE: VLAN reference table with Array

    Posted Jul 05, 2022 04:46 PM

    It is still not clear to me from where you get the Destination Cluster.
    Is that based on the Source Portgroup?