markdjones82
Expert
Expert

Answer file for host profile questions

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.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
14 Replies
markdjones82
Expert
Expert

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.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert

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

}

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
markdjones82
Expert
Expert

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!

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

Thanks, glad you found a solution


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert

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?

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert

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.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

Perhaps something like this might work.

Haven't been able to test it though Smiley Sad

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert

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?

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

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

That one is quite thorough and detailed.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
markdjones82
Expert
Expert

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

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
LucD
Leadership
Leadership

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())

....



Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JJUllah
Contributor
Contributor

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.

MACaddress.jpgMACaddress1.jpg

MACaddress2.jpg

0 Kudos