Search code examples
powershellcsvvmwarepowercli

Hardening VM-HOST Set-AdvancedSetting with PowerCLI from csv file


I would like to automate the configuration of the first HOST to create the Host Profiles with PowerCLI. I built the first HOST in ESXi 6.5 and did the fist Host Profiles from that HOST. But to create the first Host Profiles, I did a Edit Host Profiles line by line and it take so long. We have over 60 diffrents clusters and that means I will have to create one Host Profiles per Cluster.

I did an export from the first Host with PowerCLI with this line

Get-AdvancedSetting -Entity $HOST65 | Export-Csv -Path T:\_Evergreening_\Script\$clHOSTprofile-config.csv -NoTypeInformation

I got this in the csv file (Header)

Uid Value   Description Type    Entity  Id  Name    Client

I would to use the CSV file for a Set-AdvancedSetting using Name and Value column

Get-AdvancedSetting -Entity $HOST65 -Name 'from_csv_file' | Set-AdvancedSetting -Value 'from_csv_file'

I'm not sure how to set this ...


Solution

  • Checkout 'Import-Csv'

    You could do something like:

    # Obtain and export Advanced Settings from a specified host
    Get-AdvancedSetting -Entity $HOST65 | Export-Csv -Path T:\_Evergreening_\Script\$clHOSTprofile-config.csv -NoTypeInformation
    
    # Import CSV file into a PowerShell array object
    $csvInputs = Import-Csv -Path T:\_Evergreening_\Script\$clHOSTprofile-config.csv
    
    # Create a loop to address each item of the array, sourcedfrom the CSV 
    foreach ($csvInput in $csvInputs) {
    
         # Obtain and set the individual advanced setting on a specified host
         Get-AdvancedSetting -Entity $HOST6502 -Name $csvInput.Name | Set-AdvancedSetting -Value $csvInput.Value
    
    }