Enable SSH Service on ESXi hosts using PowerShell

I found myself wanting to enable the SSH service on my ESXi hosts. I could use Host Profiles to enable it but I decided to PowerShell script it! To enable SSH there are three parts to it:

You will need to start the SSH service and set it to Start and Stop with Host:

manually-start-ssh-service

And you will need to suppress the SSH is enabled warning message:

esxi-hosts-ssh-warning

This script does all of the above to an entire cluster. Let’s see it in action!

######################################################################
# Start SSH Service, change Startup Policy, and Suppress SSH Warning #
######################################################################
 
#Variables
$vCenter = "LABVC01.virtuallyboring.com"
$Cluster = "Nested ESXi Cluster"
 
### Start of Script
# Load VMware Cmdlet and connect to vCenter
Add-PSSnapin vmware.vimautomation.core
connect-viserver -server $vCenter
 
$VMHost = Get-Cluster -Name $Cluster | Get-VMhost
 
# Start SSH Server on a Cluster
ForEach ($VMhost in $Cluster){
Write-Host -ForegroundColor GREEN "Starting SSH Service on " -NoNewline
Write-Host -ForegroundColor YELLOW "$VMhost"
Get-VMHost | Get-VMHostService | ? {($_.Key -eq "TSM-ssh") -and ($_.Running -eq $False)} | Start-VMHostService
}
 
# Change Startup Policy
ForEach ($VMhost in $Cluster){
Write-Host -ForegroundColor GREEN "Setting Startup Policy on " -NoNewline
Write-Host -ForegroundColor YELLOW "$VMhost"
Get-VMHost | Get-VMHostService | where { $_.key -eq "TSM-SSH" } | Set-VMHostService -Policy "On" -Confirm:$false -ea 1
}
 
# Surpress SSH Warning
ForEach ($VMhost in $Cluster){
Write-Host -ForegroundColor GREEN "Setting UserVar to supress Shell warning on " -NoNewline
Write-Host -ForegroundColor YELLOW "$VMhost"
Get-VMhost | Get-AdvancedSetting | Where {$_.Name -eq "UserVars.SuppressShellWarning"} | Set-AdvancedSetting -Value "1" -Confirm:$false
}
### End of Script

Read more…

How to Add ESXi Hosts to vCenter using PowerShell

There are quite a few clicks needed to add a host to vCenter. If you are deploying multiple hosts to your environment you have many clicks ahead. You should script it! This PowerShell script will make adding multiple hosts to vCenter easy!

Let’s see it in action!

#####################################################################
# Load VMware Plugins and connect to vCenter
#####################################################################

Add-PSSnapin vmware.vimautomation.core
## Enter your vCenter here
connect-viserver -server LABVC01.virtuallyboring.com

########################################################################
# Add Multiple Hosts to vCenter
######################################################################## 

# Variables
## You can use comma separated names or change to pull from a text file. Your pick.
$ESXiHosts = "ESXi01.virtuallyboring.com" , "ESXi02.virtuallyboring.com" , "ESXi03.virtuallyboring.com"
## Enter the name of a Data Center or Host Cluster
$ESXiLocation = "Nested ESXi Cluster"

# Start Script
$credentials = Get-Credential -UserName root -Message "Enter the ESXi root password"

Foreach ($ESXiHosts in $ESXiHosts) { 
 Add-VMHost -Name $ESXiHosts -Location $ESXiLocation -User $credentials.UserName -Password $credentials.GetNetworkCredential().Password -RunAsync -force
 Write-Host -ForegroundColor GREEN "Adding ESXi host $ESXiHosts to vCenter"
 } 
# End Script 

Read more…

Add Virtual Machine Templates (.VMTX) to Inventory using PowerShell

Here is a script that will scan a datastore for VM templates (.VMTX) and add them to inventory. This is a modified version of the Add Virtual Machines (.VMX) to Inventory script.

You must have VMware PowerCLI installed in order to have the cmdlets required for PowerShell to run the script.

Add VMTX (Virtual Machine Templates) to Inventory from a Datastore:

#####################################################################
#          Load VMware Plugins and vCenter Connect                  #
#####################################################################

Add-PSSnapin vmware.vimautomation.core

connect-viserver -server ENTER VCENTER FQDN HERE
#####################################################################
#       Add .VMTX (VM Templates) to Inventory from Datastore        #
#####################################################################

# Variables: Update these to the match the environment
$Cluster = "ENTER CLUSTER NAME HERE"
$Datastore = "ENTER DATASTORE NAME HERE"
$VMFolder = "ENTER FOLDER NAME HERE"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreach($Datastore in get-datastore $Datastore)
{
 # Collect .vmtx paths of registered VMs on the datastore
    $registered = @{}
    Get-Template -Datastore $Datastore | ForEach-Object -Process {
    $registered.Add($_.Name+'.vmtx',$true)
 }

 # Set up Search for .VMTX Files in Datastore(s)
    $null = New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\'
    $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
    Where-Object -FilterScript {
    $_.FolderPath -notmatch '.snapshot' -and $_.Name -like '*.vmtx' -and !$registered.ContainsKey($_.Name)
 }
 )
 Remove-PSDrive -Name TgtDS

 #Register all .vmtx Files as VMs on the datastore
 foreach($vmtxFile in $unregistered)
 {
 New-Template -Name $vmtxFile.Name -TemplateFilePath $vmtxFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
 }
}

Let’s see it in action!

Read more…

Add Virtual Machines (.VMX) to Inventory using PowerShell

There are quite a few clicks to add a virtual machine from a datastore to inventory. If you were to consolidate an environment and had to add multiple virtual machines this would be quite a manual task. Easier solution? Script it! The below script makes scanning a datastore and registering all virtual machines with vCenter a breeze.

You must have VMware PowerCLI installed in order to have the cmdlets required for PowerShell to run the script.

Add VMX (Virtual Machines) to Inventory from a Datastore:

#####################################################################
#          Load VMware Plugins and vCenter Connect                  #
#####################################################################

Add-PSSnapin vmware.vimautomation.core

connect-viserver -server ENTER VCENTER FQDN HERE
#####################################################################
#      Add .VMX (Virtual Machines) to Inventory from Datastore      #
#####################################################################

# Variables: Update these to the match the environment
$Cluster = "ENTER CLUSTER NAME HERE"
$Datastore = "ENTER DATASTORE NAME HERE"
$VMFolder = "ENTER FOLDER NAME HERE"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreach($Datastore in $Datastore) {
# Searches for .VMX Files in datastore variable
$ds = Get-Datastore -Name $Datastore | %{Get-View $_.Id}
$SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$SearchSpec.matchpattern = "*.vmx"
$dsBrowser = Get-View $ds.browser
$DatastorePath = "[" + $ds.Summary.Name + "]"

# Find all .VMX file paths in Datastore variable and filters out .snapshot
$SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch ".snapshot"} | %{$_.FolderPath + ($_.File | select Path).Path}

# Register all .VMX files with vCenter
foreach($VMXFile in $SearchResult) {
New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -RunAsync
 }
}

Let’s see it in action!

Read more…