Automation

 View Only
Expand all | Collapse all

Answer file for host profile questions

  • 1.  Answer file for host profile questions

    Posted Feb 06, 2013 09:39 PM

    All,

      Looking at creating a hash table from the powercli reference book (luc/alan's) but having some issues.

    I am running  this command to get the hash table:

    $AdditionalConfiguration = Apply-VMHostProfile -entity hostname -ApplyOnly

    I get output when i run

    $AdditionalConfiguration


    Name                           Value
    ----                           -----
    storage.iscsi_iscsiProfile_... bnx2i-bc305b770fd5
    storage.iscsi_iscsiProfile_... asdfasfas

    network.dvsHostNic["key-vim...
    network.dvsHostNic["key-vim...
    storage.iscsi_iscsiProfile_... iqn.1998-01.com.vmware:asdfasdfasf
    network.dvsHostNic["key-vim...
    network.dvsHostNic["key-vim...
    storage.iscsi_iscsiProfile_... adfasfasffsadfasd
    storage.iscsi_iscsiProfile_... basdfasfas
    network.dvsHostNic["key-vim...
    storage.iscsi_iscsiProfile_... adsfasfsafd

    network.dvsHostNic["key-vim...

    BUT when i try to select name it comes out blank?

    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $AdditionalConfiguration| Select-O
    bject Name

    Name
    ----

    My ultimate goal would be to have a spreadsheet with hostname, Vmotion ip for DVS, IP for Management, Subnet Masks and be able to read that in and then set the keys.  Not being able to even get this key info to pass is a bit troubling though.



  • 2.  RE: Answer file for host profile questions

    Posted Feb 06, 2013 09:50 PM

    Ok i found an answer, but i'm going to keep this post open as I hash out any other questions I have about passing the IP information with a csv.

    A little typo i think in the book on page 53: this $AdditionalConfiguration | Select-Object Name should be $additionalConfiguration.Getenumerator() | Select-Object Name.



  • 3.  RE: Answer file for host profile questions

    Posted Feb 06, 2013 11:13 PM

    The result of the Apply-VMHostProfile is a hash table, so you'll have to use the Get-Enumerator function to retrieve the key and the value.

    Something like this

    $hprof = Get-VMHostProfile -Name "My HostProfile"
    $AdditionalConfiguration = Apply-VMHostProfile -Profile $hprof -Entity MyEsx -ApplyOnly -Confirm:$false
    $AdditionalConfiguration
    .GetEnumerator() |
    Select
    Key |
    Export-Csv
    c:\hprof.csv -NoTypeInformation -UseCulture

    Thanks for drawing our attention to that mistake.



  • 4.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 03:22 PM

    Luc,

      Following your guide I have come up with this, but have a couple questions. Excuse the formatting it didn't paste in as expected

    1.  What is the reasoning for the Switch statement for the hash table to create a new one with keys?  Can I not do something like a wild card to modify the existing hashtable? $AdditionalConfiguration['network.dvsHostNic["*management*.address'] = $data.mgmtip and just set.

    2.This pipelien command is not working and i get this error

    Apply Profile to host.blah.com

    Set-VMHost : The input object cannot be bound to any parameters for the command either because the
    command does not take pipeline input or the input and its properties do not match any of the
    parameters that take pipeline input.
    At U:\powercli\hostprofile\hostprofile.ps1:50 char:107
    + ... nfirm:$false | Set-VMHost -State 'Connected' | Test-VMHostProfileCompliance
    +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (System.Collections.Hashtable:PSObject) [Set-VMHost
       ], ParameterBindingException
        + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.Set
       VMHost

    FUll script below

    #Import spreadsheet with hostname,mgmtip,mgmtsubnet,vmoip,vmosubnet for 1000v DVS input

    $answerfile= Import-CSV ProfileAnswers.csv

    #Loop through each row and apply host profile

    foreach

    ($data in $answerfile)

    {

    #Set VMhost variable to be able to pipe objects

    $VMhost = Get-VMhost $data.hostname

    Write-Host "Starting $VMhost.name"

    #Attach host profile to host

    Apply-VMHostProfile -profile $data.profile -entity $VMhost -AssociateOnly -confirm:$false | Out-Null

    #Get hash table for ansfer file and assign to configuraiton

    Write-Host "Getting Hash Table Answer File"

    $AdditionalConfiguration = Apply-VMHostProfile -profile $data.profile -entity $VMHost -ApplyOnly -confirm:$false

    #example below for variable names to create new hash table based on wildcards. Will remove in future iteration

    #$AdditionalConfiguration['network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-dell-management-management"].ipConfig.IpAddressPolicy.address'] = $data.mgmtip

    #$AdditionalConfiguration['network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-dell-management-management"].ipConfig.IpAddressPolicy.subnetmask'] = $data.mgmtsubnet

    #$AdditionalConfiguration['network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-dell-vmotion-vmotion"].ipConfig.IpAddressPolicy.address'] = $data.vmoip

    #$AdditionalConfiguration['network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-dell-vmotion-vmotion"].ipConfig.IpAddressPolicy.subnetmask'] = $data.vmosubnet

    #Switch Statement to read in hash table and then set keys for Vmotion IP and Management IP on DVS

    $var = @{}

    switch ($AdditionalConfiguration.GetEnumerator())

    {

    {

    $_.name -like '*management*.address' } {

    $var += @{$_.Name = $data.mgmtip}

    }

    {

    $_.name -like '*management*.subnetmask'} {

    $var += @{$_.Name = $data.mgmtsubnet}

    }

    {

    $_.name -like '*vmotion*.address'} {

    $var += @{$_.Name = $data.vmoip}

    }

    {

    $_.name -like '*vmotion*.subnetmask'} {

    $var += @{$_.Name = $data.vmosubnet}

    }

    #Default {

    #$value = Read-Host "Please provide a value for ${$_.Name}"

    # $var +=@{$_.Name = $value}

    # }

    }

    #Set Host in maintenance mode, apply profile with answer file, exit maint, test for compliance

    Write-Host "Apply Profile to $VMHost"

    Set-VMHost -VMHost $VMhost -State 'Maintenance' | Apply-VMHostProfile -Variable $var -Confirm:$false | Set-VMHost -State 'Connected' | Test-VMHostProfileCompliance

    }



  • 5.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 03:41 PM

    Nevermind, I got it figured out.  I was missing the mac address required key.  I set it to blank which is our normal response.

    Kudos to you all for this book, relaly helped me out on passing these key variables to the host profile and automating that piece with a CSV!



  • 6.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 03:46 PM

    Thanks, glad you found a solution



  • 7.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 05:12 PM

    Luc,

      I did have a question on why you use the switch to set a new hash table to $var.  I attempted to set the variables directly to $additionalvariable using wildcards but it did not accept the wildcards.  Maybe because it was in quotes?  Is it possible to do wildcards when setting the hash table values?



  • 8.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 06:01 PM

    Sorry, not following. Can you give me an example of what you mean ?



  • 9.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 08:53 PM

    So, instead of having to specify the exact key like so:

    $AdditionalConfiguration['network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-management-management"].ipConfig.IpAddressPolicy.address’]  = “10.10.10.10″

    I was wondering if you could do something like using a wildcard like this: $AdditionalConfiguration[*management*.address’]  = “10.10.10.10″ to set the hash table value.  I tried it but it just created a new key with like *management*.address with value 10.10.10.10

    I was wondering if it was possible to just modify the existing hash table value with wildcards instead of creating a new hash table $var and using the switch statement.



  • 10.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 09:24 PM

    Perhaps something like this might work.

    Haven't been able to test it though :smileysad:

    $AdditionalConfiguration.Keys | 
    where
    {$_ -like "*ipConfig.IpAddressPolicy.address"} | %{   $AdditionalConfiguration[$_] =  "10.10.10.10"
    }


  • 11.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 10:03 PM

    Cool thanks! I'll see if I can tinker with something like that when I get some time.  Thanks for the direction.  The Switch function works, was just trying to undersand the syntax of hash tables.

    Still trying to learn some of the more advance piplining and loop statements in powershell.  Any recommended sites for that?



  • 12.  RE: Answer file for host profile questions

    Posted Feb 07, 2013 10:30 PM

    On the My PS Library post there is a link to Tobias's free ebook Master-PowerShell.

    That one is quite thorough and detailed.



  • 13.  RE: Answer file for host profile questions

    Posted Feb 08, 2013 04:56 PM

    Luc,

      Thanks!  I am receiving this error when it runs, but it still works.  It is occuring at the $var = @{}, why would it do that when creating the blank hash table?

    Method invocation failed because [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]
    doesn't contain a method named 'GetEnumerator'.
    At U:\powercli\hostprofile\hostprofile.ps1:28 char:2
    +     $var = @{}
    +     ~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound



  • 14.  RE: Answer file for host profile questions

    Posted Feb 08, 2013 10:04 PM

    I suspect the actual error comes from the next line in the script

    switch ($AdditionalConfiguration.GetEnumerator())

    Then the error message about GetEnumerator seems to make sense, but the displayed line is not correct.

    I would guess that $AdditionalConfiguration is $null at this point.

    Add a line to test for this.

    If($AdditionalConfiguration){

       Switch ($AdditionalConfiguration.GetEnumerator())

    ....




  • 15.  RE: Answer file for host profile questions

    Posted Nov 14, 2017 11:35 PM

    Hi Mate,

    I really like your script to automate the Host Profile function but i'm having issue with the MAC address. In my organisation, we don't assign MAC address while creating the network. So, added extra

    line on your script to make the MAC field empty but it's not working. The MAC address field get generated. Could you please let me know, how you assign 'Null Value' to the MAC address field?

    Just to let you know, i'm fairly new to POWERCLI. Also, my CSV don't have any MAC field.

    Hope to hear from you soon.