Skip navigation
VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
109 Views 1 Reply Last post: Feb 5, 2012 11:57 PM by RvdNieuwendijk RSS
tdubb123 Expert 1,640 posts since
Feb 15, 2008
Currently Being Moderated

Feb 5, 2012 7:16 PM

how to improve this script

how can i improve this script. It works but seems awkward. basically it takes old hosts registered with ip addresses (vmhosts.txt) in vcenter out of a cluster and reregister them with dns names (vmhosts1.txt) instead of ip addresses

 

 

Connect-VIServer vc
$array = @()
$MyESXHosts = Get-Content -Path "C:\vmhosts1.txt"
Get-VMHost -Name  (Get-Content -Path "C:\vmhosts.txt") |`
ForEach-Object {
Set-VMHost -VMHost $_ -State Disconnected -RunAsync
$array += $_
}
Start-Sleep 10
Remove-VMHost -VMHost $array -Confirm:$false
Start-Sleep 10
Foreach ($hosts in $MyESXHosts)
{
Get-Cluster -Name cluster | Add-VMHost -Name $hosts -User root -Password xxxxx -Confirm:$false -force -RunAsync
}
RvdNieuwendijk Virtuoso User Moderators vExpert 1,261 posts since
Aug 3, 2009
Currently Being Moderated
1. Feb 5, 2012 11:57 PM in response to: tdubb123
Re: how to improve this script

I did a little reorganization of your script. I could not test it, but at least it will give you an idea how to organize it in a PowerShell way of doing things.

 

Connect-VIServer vc
Get-Content -Path "C:\vmhosts.txt" | ForEach-Object {
  Get-VMHost -Name $_ | Set-VMHost -State Disconnected | Remove-VMHost -Confirm:$false
}
$Cluster = Get-Cluster -Name cluster
Get-Content -Path "C:\vmhosts.txt" | ForEach-Object {
  Add-VMHost -Name $_ -Location $Cluster -User root -Password xxxxx -Confirm:$false -force -RunAsync
}
Disconnect-VIServer vc -Confirm:$false

 

Regards, Robert

 

Message was edited by: RvdNieuwendijk Made an improvement to the script.

Bookmarked By (0)

Share This Page

Communities