Skip to content

Instantly share code, notes, and snippets.

View Dapacruz's full-sized avatar

David Cruz Dapacruz

View GitHub Profile
@Dapacruz
Dapacruz / get_vmhost_lldp_info.sh
Last active October 5, 2017 16:39
Get VMware ESXi vmnic LLDP info (get_vmhost_lldp_info.sh)
# VMware ESXi shell script
# Get LLDP info for vmnics (use the get_vmhost_lldp_info_raw function if output is not complete)
vmnics="vmnic4 vmnic5"
get_vmhost_lldp_info(){
for vmnic in $vmnics; do
printf "$vmnic [=]----[=] "
pktcap-uw --uplink $vmnic --ethtype 0x88cc -c 1 -o /tmp/vmnic_lldp.pcap > /dev/null
hexdump -C /tmp/vmnic_lldp.pcap | awk -F'|' '{printf $2}' |
sed -E 's|.+\.([etgETG][teiTEI][a-zA-Z]+ ?-?([0-9]+/)+[0-9]+)(.\.+.{1,2}\.+)+([a-zA-Z][a-zA-Z0-9]+)\..*|\1 (\4)\n|'
@Dapacruz
Dapacruz / ping_iscsi.sh
Last active October 5, 2017 16:54
Ping All VMware ESXi IP Addresses
# Get a list of IP addresses with PowerCLI
$Vmhosts = 'sac0esxi*'
$Vmks = 'vmk2', 'vmk3', 'vmk5', 'vmk6'
Get-VMHostNetworkAdapter -VMHost $Vmhosts -Name $Vmks | sort VMHost, Name | ft -auto VMHost, Name, IP
# ESXi host Bash function to ping all iSCSI addresses
ping_iscsi () {
mtu=8972
failures=0
vmks="vmk2
@Dapacruz
Dapacruz / Disable-VmCbt.ps1
Last active January 29, 2018 18:30
Enable/Disable Changed Block Tracking (CBT) on VMware Virtual Machines
# Disable CBT on virtual machines
$vms = (Get-VM | Get-View).Where{$_.ChangeTrackingEnabled -match 'True'}.Name
foreach ($vm in $vms) {
$vm_view = Get-vm $vm | get-view
$vm_config_spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vm_config_spec.changeTrackingEnabled = $false
$vm_view.reconfigVM($vm_config_spec)
$snap = New-Snapshot (Get-VM $vm) -Name "Disable CBT"
$snap | Remove-Snapshot -confirm:$false
@Dapacruz
Dapacruz / Get-VmRdm.ps1
Created February 2, 2018 23:27
Get VMware vSphere RDMs
# Get vSphere RDMs
Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName
@Dapacruz
Dapacruz / Enable-VmConsoleCopyPaste.ps1
Last active March 28, 2018 18:57
Enable Copy/Paste Between the Guest Operating System and VMware Virtual Machine Remote Console
$vm = 'vm1', 'vm2'
Get-VM $vm | New-AdvancedSetting -name "isolation.tools.paste.disable" -Value $false -Confirm:$false -Force
Get-VM $vm | New-AdvancedSetting -name "isolation.tools.copy.disable" -Value $false -Confirm:$false -Force
# Power off the VM and back on
Stop-VMGuest -VM $vm -Confirm:$false | Out-Null
while ((Get-VM -Name $vm).PowerState -ne 'PoweredOff') {Start-Sleep 2}
Start-VM -VM $vm -ErrorAction SilentlyContinue
@Dapacruz
Dapacruz / 1 Save-EncryptedPassword.ps1
Last active April 27, 2018 17:34
Save an encrypted password to a file and retrieve it later for use in a PowerShell script
# Save an encrypted password to a file and retrieve it later for use in a script
Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File password.txt
$user = 'user'
$password = Get-Content password.txt | ConvertTo-SecureString
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
@Dapacruz
Dapacruz / Copy-FilesToDatastore.ps1
Last active May 22, 2018 18:11
Copy Files to a VMware vSphere Datastore from PowerCLI
$datastore = Get-Datastore -Name 'ISOs' | Select-Object -First 1
$src = 'C:\Temp\VMware-VCSA-all-6.5.0-8307201.iso'
$ds = New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root '\'
Copy-DatastoreItem -Item $src -Destination ds:\
Remove-PSDrive $ds
@Dapacruz
Dapacruz / Configure-VCEmailAlerts.ps1
Last active May 30, 2018 16:50
Set Up VMware vCenter Email Alerts
$smtp_sender = 'vcenter@domain.com'
$smtp_recipients = 'support@domain.com'
$smtp_server = 'smtp.domain.com'
$smtp_user = ''
$smtp_password = ''
$smtp_port = '25'
$vcenter = $global:defaultviservers.Name
$alarms = @(
@{
alarm_definition = 'Datastore usage on disk'
@Dapacruz
Dapacruz / Configure-ADAuthentication.ps1
Created May 30, 2018 18:05
Configure Active Directory Authentication on VMware ESXi Hosts
$hosts = '*'
$domain = 'domain.local'
$ad_group = 'Domain Admins'
$user = 'user'
$password = 'password'
Get-VMHostAuthentication -VMHost $hosts | Set-VMHostAuthentication -JoinDomain -Domain $domain -User $user -Password $password
Get-AdvancedSetting -Entity $hosts -Name Config.HostAgent.plugins.hostsvc.esxAdminsGroup | Set-AdvancedSetting -Value $ad_group
@Dapacruz
Dapacruz / Create-NimbleDatastore.ps1
Created May 30, 2018 18:26
Create Nimble Volumes and VMware vSphere Datastores
$vmhost = 'esxi-01.domain.local'
$vcenter = 'vcenter.domain.local'
$vcenter_user = 'administrator@vsphere.local'
# Save encrypted password to a file
# Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File vcenter-password.txt
$vcenter_password = Get-Content vcenter-password.txt | ConvertTo-SecureString
$nimble_mgmt_address = '10.1.1.1'
$nimble_user = 'admin'
# Save encrypted password to a file