profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, September 29, 2015

Make hostfile entries to ESXi with no sweat

So when we build datacenters for clients we sometimes make hostfile entries to the ESXi hosts. This was a tedious boring task . Imagine yourself
enabling SSH on 100 hosts
edit the /etc/hosts in the shell command
disable ssh on 100 hosts.
Even if you calculate 5 minutes a host it is 500
I thought of trying to find a lazy man's way around it since I consider to be one.
Once again plink to the rescue.
First of all we will use alan's plink script to download it if we dont have it.

#http://www.virtu-al.net/2013/01/07/ssh-powershell-tricks-with-plink-exe/
$PlinkLocation = $PSScriptRoot + "\Plink.exe"
If (-not (Test-Path $PlinkLocation)){
   Write-Host "Plink.exe not found, trying to download..."
   $WC = new-object net.webclient
   $WC.DownloadFile("http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe",$PlinkLocation)
   If (-not (Test-Path $PlinkLocation)){
      Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
      Exit
   } Else {
      $PlinkEXE = Get-ChildItem $PlinkLocation
      If ($PlinkEXE.Length -gt 0) {
         Write-Host "Plink.exe downloaded, continuing script"
      } Else {
         Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
         Exit
      }
   } 
}


It is better to have this plink copied manually to the folder where the script is located and run this script from a location where you can resolve the hostnames of the target hosts.

#If using in powershell then add snapins below for VMware ESXi.
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

#Disconnect from already connected viservers if any
Disconnect-VIServer * -ErrorAction SilentlyContinue

#Connect to the vcenter server
connect-viserver

#X server's credentials
$user = Read-Host "Host's username?"
$pass = Read-Host "Host's password?"

#copy plink & pscp to c:\ for now
Copy-Item $PSScriptRoot\plink.exe C:\

#variables
#put the name of the cluster between quotes. Enter multiple cluster's names & separate them by a comma (no space in between)
$cluster = ""
$VMHosts = get-cluster $cluster | Get-VMHost | sort

#Enable SSH on all hosts
get-cluster $cluster | Get-VMHost | Get-VMHostService | where {$_.Key -eq "TSM-SSH"} | Start-VMHostService

ForEach ($VMHost in $VMHosts)
{
echo y | C:\plink.exe -ssh $user@$VMHost -pw $pass "exit"

#replace the 'someline' with the value that you want.
#To add multiple lines repeat the below command multiple times with the respective values instead of someline (enclose entries within "")
C:\plink.exe -ssh -v -noagent $VMHost -l $user -pw $pass 'echo "someline" >> /etc/hosts'

#This line below displays the resulting contents of the file
C:\plink.exe -ssh -v -noagent $VMHost -l $user -pw $pass "cat /etc/hosts"
}

#delete plink from c:\
Remove-Item C:\plink.exe

#Disable SSH on all hosts
get-cluster $cluster | Get-VMHost | Get-VMHostService | where {$_.Key -eq "TSM-SSH"} | Stop-VMHostService -confirm:$false

#####additional sources#####


Hey don't blame me if I don't update it here much. I only udpate them on github for all.

Saturday, September 26, 2015

VMware Deployed (or existing) Windows is missing its desktop

So, when we did some rapid deployment of VMs recently with powercli sometimes some windows VMs came up with this ugly bizarre error
 







Dont know why but some claim Dism /Online /Cleanup-Image /RestoreHealth via cmd or powershell and then sfc /scannow to fix it. That is a good thing if it does but i am yet to see some results but the fix for me which worked is
powershell>diskmgmt
then extend the C drive. If you want to avoid this error then make sure your C drive of the template is big enough. Don't be thrifty with the C drive man, that is important.