More Scripting work! now for VCF Post tasks

I have been scripting alot of post tasks after creating a new cluster for a existing Workload domain in VCF. Here they are :

The first one is to join all your hosts to Active Directory for local authentication:

# Join Host to Active Directory

$adjoinusername = "adjoinusernamehere"
$adjoinpwd = Read-Host ("Please enter the password for user: $adjoinusername") -AsSecureString
$adjoinCredentials = New-Object System.Management.Automation.PSCredential($adjoinusername, $adjoinpwd)

$cluster = Get-Cluster | Out-GridView -OutputMode Single -Title "Select Cluster"
$hostsincluster = Get-Cluster $cluster | Get-VMHost | Sort-Object

foreach ($esxhost in $hostsincluster) {
  Get-VMHostAuthentication -VMHost $esxhost | Set-VMHostAuthentication -JoinDomain -Domain "YOURDOMAINHERE" -Credential $adjoinCredentials -Confirm:$false
}

This one will rename those nasty local datastores to their respective hostnames!

# rename local datastores

$datastoreNames = get-datastore datastore1*
foreach ($Datastore in $datastoreNames){
    write-host $Datastore.name "- " -NoNewline 
    $ESXHostFQDN = (get-ESXHost -id $(get-datastore $datastore).ExtensionData.host.key).name
    $ESXHostname = $ESXHostFQDN.Split(".")[0]
    $datastorenewname = $ESXHostname
    Get-datastore -name $datastore | Set-datastore -Name $datastorenewname
}

The next one will create a directory on the local datastore for the host which has the name of the host.

# create scratch dir on local datastores

$cluster = Get-Cluster | Out-GridView -OutputMode Single -Title "Select Cluster"
$hostsincluster = Get-Cluster $cluster | Get-VMHost | Sort-Object

foreach ($esxhost in $hostsincluster) {
  $datastore = Get-Datastore -Name $esxhost.Name.Substring(0, 14)
  New-PSDrive -Location $datastore -Name DS -PSProvider VimDatastore -Root "\"
  New-Item -Path DS:\scratch -ItemType Directory
  Remove-PSDrive -Name DS -Confirm:$false
}

and finally i needed to set the advanced setting to move the scratch dir to that local datastore and reboot the host, this one will do it for an entire cluster 1 host at a time.

# Set scratch location

$cluster = Get-Cluster | Out-GridView -OutputMode Single -Title "Select Cluster"
$hostsincluster = Get-Cluster $cluster | Get-VMHost | Sort-Object

foreach ($esxhost in $hostsincluster) {
  $location = "/vmfs/volumes/" + $esxhost.Name.Substring(0, 14) + "/scratch"
  Get-VMHost $esxhost | Get-AdvancedSetting ScratchConfig.ConfiguredScratchLocation | Set-AdvancedSetting -Value $location -Confirm:$false
  #enter maintenance mode
  Set-VMHost $esxhost -State Maintenance -Evacuate -VsanDataMigrationMode EnsureAccessibility
  #wait for maintenance mode/evacuate
  do {
    sleep 10
    $ConnectionState = (get-vmhost $esxhost).ConnectionState
  }
  while ($ConnectionState -eq "Connected")
  #reboot vmhost
  Restart-VMHost $esxhost -Confirm:$false | Out-Null
  #wait for not responding state
  do {
    sleep 10
    $ConnectionState = (get-vmhost $esxhost).ConnectionState
  }
  while ($ConnectionState -eq "Maintenance")

  #wait for maintenance mode state
  do {
    sleep 10
    $ConnectionState = (get-vmhost $esxhost).ConnectionState
  }
  while ($ConnectionState -eq "NotResponding")
  #exit maintenance mode
  Set-VMhost $esxhost -State Connected -Confirm:$false | Out-Null

}

Leave a Reply