VMware Cloud Community
RichardBrown
Contributor
Contributor

Post autodeploy host powershell tasks

Hi,

I have the below script that runs after a bunch of servers have checked into vCenter. It changes the hostname connects locally and changes the IP address and then adds the host to the right vCenter and cluster. It gets all the values from the csv but the issue is that it always fails adding the host in at the end and then subsequent hosts seem to fail as if its still looking for the first s/n

foreach ($esxhost in $esxhosts) {

$getproperty = get-vmhost $esxhost | get-vmhosthardware

$serialNum = $getproperty.SerialNumber

import-csv "C:\Rich\Test06.csv" -UseCulture | where{$_.Serial -eq $serialNum} | foreach-object -process {

$Hostname = $_.Name + ".Domain.namet"

$esxcli = get-esxcli -vmhost $esxhost

$IPhost = Get-VMHostNetworkAdapter -vmhost $esxhost -Name vmk0 | select IP

$IPadd = $IPhost.IP

$esxcli.system.hostname.set($Domain,$null,$_.Name)

set-vmhost -VMHost $esxhost -State "Disconnected"

Get-VMhost $esxhost | Remove-VMhost -confirm:$false

Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false

sleep 10

connect-viserver -server $IPAdd -username root -password $rootpsd 

$vmk0 = Get-VMHostNetworkAdapter -vmhost $IPadd -Name vmk0

$vmk0 | set-vmhostNetworkAdapter -IP $_.IPAddr -SubnetMask $msk -confirm:$false

sleep 10

#Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false

write-host "Connect $_.vCenter"

Connect-Viserver -Server $vCenter -user $vC_admin -Password $vC_pass  

Add-VMHost $Hostname -Location $cluster -User "root" -Password $rootpsd -RunAsync -force

}

}

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership

What is the error message you are getting?


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

Reply
0 Kudos
RichardBrown
Contributor
Contributor

It times out at the end when adding the host into its ultimate vCenter saying "value cannot be found for the mandatory parameter location"

But i copied the name of the cluster from the cli.

I suspected it to be that the hostname is not FQDN so i added the domain name to a variable and tried passing that but the attribute still seems to be just the name.

Reply
0 Kudos
LucD
Leadership
Leadership

I'm not sure what you are trying to do here, but there is no IPAddr property on a vmk object.

$
vmk0 | Set-VMHostNetworkAdapter -IP $_.IPAddr -SubnetMask $msk -confirm:$false

Shouldn't that be

$vmk0 | Set-VMHostNetworkAdapter -IP $IPadd -SubnetMask $msk -confirm:$false

Btw, the variable $msk is nowhere populated either.


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

Reply
0 Kudos
RichardBrown
Contributor
Contributor

that bit works fine an $msk is define somewhere else..

the issue is that last bit where it add the host to the cluster it takes the cluster name from the csv $_.Cluster and when ever i test that bit of code its fine. but the error which is below suggests that something is missing or incorrect.

"add-host Value cannot be found for the mandatory parameter location...

This info is all in the CSV as there are 4 vcenters and a lot of hosts..

Reply
0 Kudos
LucD
Leadership
Leadership

That is not what the 1st error is saying.
You use a pipeline with $vmk and the property you use is not in the object in $vmk.

You might have that property in the CSV, but in this case the $_ variable does not contain a row from the CSV.


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

Reply
0 Kudos
RichardBrown
Contributor
Contributor

No not quite so $IPadd is the IP address assigned by DHCP which is required to connect directly to the host in order to change its IP to what it ultimately should  be, and the ultimate IP is found in the csv file as $_.IPaddr.

What we are trying to do is build hundreds of hosts with auto deploy so they boot up they have a DHCP address, then the script runs and gets all the final details from a csv file updates the host and moves it to the right vCenter and cluster . Where another scipt will run to add them all to the vds etc..

Reply
0 Kudos
RichardBrown
Contributor
Contributor

Sorry i should also note that the message is a bit misleading, it says it cant change the IP because there is an error talking to the host but thats because it has already changed the IP to the new one and that bit is successful.

The issue seems to be that the "Value cannot be found for the mandatory parameter location" which is right at the bottom of of the rubbish screen shot and what i have tried to do there is take the hostname from the csv and append the domain name to it so that the script can then add the host to the cluster etc.

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, but since I can't see what you stored in $cluster, it is difficult to determine why the Location error occurs.

If the code extract you posted is correct, then the $_ variable when you reach the Add-VMHost cmdlet would contain a row from the CSV you are using.

If Cluster is a column in your CSV, that value on the Location parameter should probably be $_.Cluster.

Unless you assigned something (that is not included in the posted code) to the $cluster variable earlier.


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

Reply
0 Kudos
RichardBrown
Contributor
Contributor

so i tried to break it down and isolate just the adding of the host to the cluster with the below code and it seems regardless of what i do with the import-csv (and i have tried a few methods) it doesnt pass the value for that column.. please see below and attached - every other value seems to work

if i try and do a write-host with a foreach it never displays the cluster value, the name has a hyphen ( - ) in which i cant find being an illegal character , i have also used $_.Cluster without $($_.Cluster)

script

import-csv "C:\Rich\Servers.csv" | foreach-object {

write-host "adding $($_.FQDN) to $($_.vCenter) cluster $($_.Cluster)"

Connect-Viserver -Server $($_.vCenter) -user $vC_admin -Password $vC_pass

Add-VMHost $($_.FQDN) -Location $($_.Cluster) -User "root" -Password $rootpsd -RunAsync -force

}

csv file

Name,FQDN,Serial,IPAddr,vCenter,Cluster

Hostname,Hostname.emea.cbre.net,serialnumber,1.1.1.1,vCenter,Cluster1

Reply
0 Kudos
LucD
Leadership
Leadership

Variable substitution happens in a string, place them between quotes.

Add-VMHost "$($_.FQDN)" -Location "$($_.Cluster)" -User "root" -Password $rootpsd -RunAsync -Force


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

Reply
0 Kudos