PowerCLI

 View Only
  • 1.  Hash Table from CSV not working for Invoke-VMHostProfile

    Posted Nov 11, 2017 03:33 AM

    Hi All I can't figure out what I'm doing wrong. I can get Host profile to remediate with this way:

    $hostdns = '192.168.1.24'

    $profile = 'vmlabs-master-hp'

    #step2

    $VMHost = Get-VMHost $hostdns

    $profilename = Get-VMHostProfile -Name $profile

    #step3

    Invoke-VMHostProfile -profile $profilename -entity $VMhost -AssociateOnly -confirm:$false

    $AdditionalConfiguration = Invoke-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

    $AdditionalConfiguration.GetEnumerator()| select Name

    #step4

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-MGMT-vmk0"].ipConfig.IpAddressPolicy.subnetmask'] = "255.255.255.0"                                                           

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VMOTION-vmk1"].ipConfig.IpAddressPolicy.address'] = "10.1.1.24"                                                           

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VMOTION-vmk1"].ipConfig.IpAddressPolicy.subnetmask'] = "255.255.255.0"                                                                                                                                     

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VSAN-vmk2"].ipConfig.IpAddressPolicy.address'] = "10.2.2.24"                                                          

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VSAN-vmk2"].ipConfig.IpAddressPolicy.subnetmask'] = "255.255.255.0"                                                         

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-NFS-vmk3"].ipConfig.IpAddressPolicy.subnetmask'] = "255.255.255.0"                                                           

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-NFS-vmk3"].ipConfig.IpAddressPolicy.address'] = "100.10.10.24"                                                               

    $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-MGMT-vmk0"].ipConfig.IpAddressPolicy.address'] = "192.168.1.24"

    #step5 - View hash table

    $AdditionalConfiguration | ft -AutoSize

    #step6 - Remediate

    Invoke-VMHostProfile -Entity $vmhost -Variable $AdditionalConfiguration -Confirm:$false

    But can't get it working when trying to import info from csv and hash table like this: (With method it attaches the profile but doesn't apply anything)

    $answerfile = Import-CSV C:\scripts\ProfileAnswers.csv

    #Loop through each row and apply host profile

    foreach($data in $answerfile) {

    $profile = Get-VMHostProfile $data.profile

    #Set VMhost variable to be able to pipe objects

    $VMhost = Get-VMhost $data.hostname

    Write-Host Starting ($VMhost.name)

    #Attach host profile to host

    Invoke-VMHostProfile -profile $profile -entity $VMhost -AssociateOnly -confirm:$false

    #Get hash table for answer file and assign to configuration

    Write-Host “Getting Hash Table Answer File”

    $AdditionalConfiguration = Invoke-VMHostProfile -profile $profile -entity $VMHost -ApplyOnly -confirm:$false

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

    $var = @{}

    If($AdditionalConfiguration){

    switch ($AdditionalConfiguration.GetEnumerator()){

    {$_.name -like ‘*MGMT*.address’ }{$var += @{$_.Name = $data.mgmtip}

    }

    {$_.name -like ‘*MGMT*.subnetmask’}{$var += @{$_.Name = $data.mgmtmask}

    }

    {$_.name -like ‘*VMOTION*.address’}{$var += @{$_.Name = $data.vmotionip}

    }

    {$_.name -like ‘*VMOTION*.subnetmask’}{$var += @{$_.Name = $data.vmotionmask}

    }

    {$_.name -like ‘*VSAN*.address’}{$var += @{$_.Name = $data.vsanip}

    }

    {$_.name -like ‘*VSAN*.subnetmask’}{$var += @{$_.Name = $data.vsanmask}

    }

    {$_.name -like ‘*NFS*.address’}{$var += @{$_.Name = $data.nfsip}

    }

    {$_.name -like ‘*NFS*.subnetmask’}{$var += @{$_.Name = $data.nfsmask}

    }

    }

    }

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

    Write-Host “Apply Profile to $VMHost

    #Set-VMHost -VMHost $VMhost -State ‘Maintenance’

    Invoke-VMHostProfile -Entity $vmhost -Variable $var -Confirm:$false #| Test-VMHostProfileCompliance

    }

    ######## CSV ##########

    "hostname","mgmtip","mgmtmask","vmotionip","vmotionmask","vsanip","vsanmask","nfsip","nfsmask","profile"

    "192.168.1.24","192.168.1.24","255.255.255.0","10.1.1.24","255.255.255.0","10.2.2.24","255.255.255.0","100.10.10.24","255.255.255.0","vmlabs-master-hp"



  • 2.  RE: Hash Table from CSV not working for Invoke-VMHostProfile

    Posted Nov 12, 2017 12:38 PM

    So not sure if there is some bug but I can't get the script to apply the profile unless I use the static style not the switch hash table approach, problem with that is if I've specified an answer which isn't required it will fail, so I've come up with this workaround which seems to work. I'm going to see if I can turn it into a function.

    $answerfile = Import-CSV C:\scripts\ProfileAnswers.csv

    foreach($data in $answerfile) {

              $VMHost = Get-VMHost $data.hostname

              $profilename = Get-VMHostProfile -Name $data.profile

              Invoke-VMHostProfile -profile $profilename -entity $VMhost -AssociateOnly -confirm:$false

              $AdditionalConfiguration = Invoke-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

              $find = $AdditionalConfiguration.GetEnumerator()| select Name

              #Create Answer Hash Table#

              if ($find | ? {$_.Name -like '*MGMT*.subnetmask'}) {

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-MGMT-vmk0"].ipConfig.IpAddressPolicy.subnetmask'] = $data.mgmtmask}

              if ($find | ? {$_.Name -like '*VMOTION*.address'}) {                                                           

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VMOTION-vmk1"].ipConfig.IpAddressPolicy.address'] = $data.vmotionip}

              if ($find | ? {$_.Name -like '*VMOTION*.subnetmask'}) {                                                           

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VMOTION-vmk1"].ipConfig.IpAddressPolicy.subnetmask'] = $data.vmotionmask}

              if ($find | ? {$_.Name -like '*VSAN*.address'}) {                                                                                                                                    

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VSAN-vmk2"].ipConfig.IpAddressPolicy.address'] = $data.vsanip}

              if ($find | ? {$_.Name -like '*VSAN*.subnetmask'}) {                                                          

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-VSAN-vmk2"].ipConfig.IpAddressPolicy.subnetmask'] = $data.vsanmask}

              if ($find | ? {$_.Name -like '*NFS*.subnetmask'}) {                                                         

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-NFS-vmk3"].ipConfig.IpAddressPolicy.subnetmask'] = $data.nfsmask}         

              if ($find | ? {$_.Name -like '*NFS*.address'}) {                                                          

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-NFS-vmk3"].ipConfig.IpAddressPolicy.address'] = $data.nfsip}

              if ($find | ? {$_.Name -like '*MGMT*.address'}) {                                                               

              $AdditionalConfiguration[‘network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-DSwitch-MGMT-vmk0"].ipConfig.IpAddressPolicy.address'] = $data.mgmtip}

              # Remediate

              Invoke-VMHostProfile -Entity $vmhost -Variable $AdditionalConfiguration -ApplyOnly -Confirm:$false

    }



  • 3.  RE: Hash Table from CSV not working for Invoke-VMHostProfile

    Posted Nov 13, 2017 01:14 PM

    Do you get any additional feedback when  you add the Verbose switch on that last Invoke-VMHostProfile?

    Is there anythinh in the vpdx log for that last Invoke-VMHostProfile?