VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

cannot get this working..

$VMHostProfile = Get-VMHostProfile -Entity *         
$Cluster = Get-Cluster -Name "cluster"
$array = @()
Import-Csv "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\Autodeploy.csv" -UseCulture | %{
$additionalConfiguration = @{
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.address' = $_.mgmt_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.subnetmask' = $_.mgmt_sm
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.address' = $_.vmotion_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.subnetmask' = $_.vmotion_sm
'network.dnsConfig.HostNamePolicy.hostName' = $_.Name
}
Apply-VMHostProfile -ApplyOnly -Profile $VMHostProfile -Entity $_.dhcpip -Variable $additionalConfiguration -Confirm:$false -RunAsync
$array += $_.mgmt_ip
}
Start-Sleep 60
Get-VMHost -Name $array | Set-VMHost -State Disconnected -RunAsync | Remove-VMHost -Confirm:$false
It works all the way up to start-sleep 60 but
get-vmhost gives this error
Remove-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 m
atch any of the parameters that take pipeline input.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\apply_host_profile.ps1:19 char:86
+ Get-VMHost -Name $allLines | Set-VMHost -State Disconnected -RunAsync | Remove-VMHost <<<<  -Confirm:$false
    + CategoryInfo          : InvalidArgument: (VMware.VimAutom...entSideTaskImpl:PSObject) [Remove-VMHost], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveVMHost
is there something wrong with the array?
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

With the -RunAsync parameter, the Set-VMHost cmdlet will not pass a VMHost object to the pipeline but a TaskObject.

Leave out the RunAsync parameter


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Where do you populate the $allLines variable ?

Or should that be the $array variable ?


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

tried running this line manually and it works

Get-VMHost -Name hostname | Set-VMHost -State Disconnected -RunAsync | Remove-VMHost

0 Kudos
tdubb123
Expert
Expert
Jump to solution

luc,

yes thats the array variable

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the -RunAsync parameter, the Set-VMHost cmdlet will not pass a VMHost object to the pipeline but a TaskObject.

Leave out the RunAsync parameter


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

yes that was the problem. thanks luc

0 Kudos
tdubb123
Expert
Expert
Jump to solution

trying to change this into a function so I can reuse but not workling

$VMHostProfile = Get-VMHostProfile -Entity *         
$Cluster = Get-Cluster -Name cluster
$array = @()
function apply-profile {
Import-Csv "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\Autodeploy.csv" -UseCulture | %{
$additionalConfiguration = @{
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.address' = $_.mgmt_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.subnetmask' = $_.mgmt_sm
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.address' = $_.vmotion_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.subnetmask' = $_.vmotion_sm
'network.dnsConfig.HostNamePolicy.hostName' = $_.Name
}
Apply-VMHostProfile -ApplyOnly -Profile $VMHostProfile -Entity $_.dhcpip -Variable $additionalConfiguration -Confirm:$false -RunAsync
$array += $_.mgmt_ip
}
}
apply-profile
Start-Sleep 60
Get-VMHost -Name $array | Set-VMHost -State Disconnected | Remove-VMHost -Confirm:$false
Start-Sleep 60
Add-VMHost -Name $_.Name -Location $Cluster -User root -Password xxxxx -Confirm:$false -force -RunAsync
Start-Sleep 60
apply-profile
0 Kudos
tdubb123
Expert
Expert
Jump to solution

hi any idea on this?

I am trying to make thi swa function but

looks like the array does not work if its not called from part of the function.

It works up to

apply-profile
Start-Sleep 60
--------------------------
but
anything below does not work because it does not know what $array is
Get-VMHost -Name $array | Set-VMHost -State Disconnected | Remove-VMHost -Confirm:$false
Start-Sleep 60
Add-VMHost -Name $_.Name -Location $Cluster -User root -Password xxxxx -Confirm:$false -force -RunAsync
Start-Sleep 60
apply-profile
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you're hitting a scope problem here.

As a quick bypass, change $array to $script:array everywhere.

That way the scope of $array is for the complete script, also inside the function.


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

luc,

it works! thanks but i just want to add one more thing. I added a second array $script:dnshosts to hold my dns names of my hosts so I can
readd them back into the cluster as actual dns names instead of ip addresses but it not working.



$VMHostProfile = Get-VMHostProfile -Entity *           
$Cluster = Get-Cluster -Name "cluster"

 $script:array = @()
$script:dnshosts = @()

  function apply-profile {

Import-Csv "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\Autodeploy.csv" -UseCulture | %{

$additionalConfiguration = @{
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.address' = $_.mgmt_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TESTMGMT-dvPG-RW-MGMT-management"].ipConfig.IpAddressPolicy.subnetmask' = $_.mgmt_sm
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.address' = $_.vmotion_ip
'network.dvsHostNic["key-vim-profile-host-DvsHostVnicProfile-dvS-RW-TestVmotion-dvPG-RW-testvmotion-vmotion"].ipConfig.IpAddressPolicy.subnetmask' = $_.vmotion_sm
'network.dnsConfig.HostNamePolicy.hostName' = $_.Name
}
Apply-VMHostProfile -ApplyOnly -Profile $VMHostProfile -Entity $_.dhcpip -Variable $additionalConfiguration -Confirm:$false -RunAsync
$script:array += $_.mgmt_ip
$script:dnshosts += $_.Name
}
}

apply-profile
Start-Sleep 60

Get-VMHost -Name $script:array | Set-VMHost -State Disconnected | Remove-VMHost -Confirm:$false
Start-Sleep 30

Add-VMHost -Name $script:dnshosts -Location $Cluster -User root -Password xxxxx -Confirm:$false -force -RunAsync
Start-Sleep 60
apply-profile

any idea why?

thank you

0 Kudos
LucD
Leadership
Leadership
Jump to solution

How does your CSV file look ?

Something like this

"Name","mgmt_ip","mgmt_sm","vmotion_ip","vmotion_sm"

"vm1","192.168.1.1","255.255.255.0","192.168.2.1","255.255.255.0"

"vm2","192.168.1.2","255.255.255.0","192.168.2.2","255.255.255.0"


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

yes exactly. It also has a column called

dhcpip

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you get any error messages ?

Is there anything in the array  $script:dnshosts just before you call Get-VMHost ?

Btw I think the value on the Entity parameter for the Apply-VMHostProfile cmdlet might be wrong.

Shouldn't you be giving that 1 or more VMHost objects instead of the dhcpip value from the CSV ?


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

0 Kudos